1 | |
1 | /* |
2 | /* T I N Y S C H E M E 1 . 4 1 |
2 | * µscheme |
|
|
3 | * |
|
|
4 | * Copyright (C) 2015 Marc Alexander Lehmann <uscheme@schmorp.de> |
|
|
5 | * do as you want with this, attribution appreciated. |
|
|
6 | * |
|
|
7 | * Based opn tinyscheme-1.41 (original credits follow) |
3 | * Dimitrios Souflis (dsouflis@acm.org) |
8 | * Dimitrios Souflis (dsouflis@acm.org) |
4 | * Based on MiniScheme (original credits follow) |
9 | * Based on MiniScheme (original credits follow) |
5 | * (MINISCM) coded by Atsushi Moriwaki (11/5/1989) |
10 | * (MINISCM) coded by Atsushi Moriwaki (11/5/1989) |
6 | * (MINISCM) E-MAIL : moriwaki@kurims.kurims.kyoto-u.ac.jp |
11 | * (MINISCM) E-MAIL : moriwaki@kurims.kurims.kyoto-u.ac.jp |
7 | * (MINISCM) This version has been modified by R.C. Secrist. |
12 | * (MINISCM) This version has been modified by R.C. Secrist. |
… | |
… | |
23 | #endif |
28 | #endif |
24 | #if USE_MATH |
29 | #if USE_MATH |
25 | # include <math.h> |
30 | # include <math.h> |
26 | #endif |
31 | #endif |
27 | |
32 | |
|
|
33 | #include "ecb.h" |
|
|
34 | |
28 | #include <sys/types.h> |
35 | #include <sys/types.h> |
29 | #include <sys/stat.h> |
36 | #include <sys/stat.h> |
30 | #include <fcntl.h> |
37 | #include <fcntl.h> |
|
|
38 | |
|
|
39 | #include <string.h> |
|
|
40 | #include <stdlib.h> |
31 | |
41 | |
32 | #include <limits.h> |
42 | #include <limits.h> |
33 | #include <inttypes.h> |
43 | #include <inttypes.h> |
34 | #include <float.h> |
44 | #include <float.h> |
35 | //#include <ctype.h> |
45 | //#include <ctype.h> |
… | |
… | |
52 | |
62 | |
53 | #define BACKQUOTE '`' |
63 | #define BACKQUOTE '`' |
54 | #define DELIMITERS "()\";\f\t\v\n\r " |
64 | #define DELIMITERS "()\";\f\t\v\n\r " |
55 | |
65 | |
56 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
66 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
57 | #define S_T (&SCHEME_V->xT) |
67 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
58 | #define S_F (&SCHEME_V->xF) |
68 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
59 | #define S_SINK (&SCHEME_V->xsink) |
69 | #define S_SINK (&SCHEME_V->xsink) |
60 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
70 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
61 | |
|
|
62 | /* |
|
|
63 | * Basic memory allocation units |
|
|
64 | */ |
|
|
65 | |
|
|
66 | #define banner "TinyScheme 1.41-s" |
|
|
67 | |
|
|
68 | #include <string.h> |
|
|
69 | #include <stdlib.h> |
|
|
70 | |
71 | |
71 | #if !USE_MULTIPLICITY |
72 | #if !USE_MULTIPLICITY |
72 | static scheme sc; |
73 | static scheme sc; |
73 | #endif |
74 | #endif |
74 | |
75 | |
… | |
… | |
134 | c += 'a' - 'A'; |
135 | c += 'a' - 'A'; |
135 | |
136 | |
136 | return c; |
137 | return c; |
137 | } |
138 | } |
138 | |
139 | |
139 | #if USE_STRLWR |
140 | static int |
|
|
141 | xisdigit (char c) |
|
|
142 | { |
|
|
143 | return c >= '0' && c <= '9'; |
|
|
144 | } |
|
|
145 | |
|
|
146 | #define toupper(c) xtoupper (c) |
|
|
147 | #define tolower(c) xtolower (c) |
|
|
148 | #define isdigit(c) xisdigit (c) |
|
|
149 | |
|
|
150 | #if USE_IGNORECASE |
140 | static const char * |
151 | static const char * |
141 | strlwr (char *s) |
152 | xstrlwr (char *s) |
142 | { |
153 | { |
143 | const char *p = s; |
154 | const char *p = s; |
144 | |
155 | |
145 | while (*s) |
156 | while (*s) |
146 | { |
157 | { |
… | |
… | |
148 | s++; |
159 | s++; |
149 | } |
160 | } |
150 | |
161 | |
151 | return p; |
162 | return p; |
152 | } |
163 | } |
153 | #endif |
|
|
154 | |
164 | |
|
|
165 | #define stricmp(a,b) strcasecmp (a, b) |
|
|
166 | #define strlwr(s) xstrlwr (s) |
|
|
167 | |
|
|
168 | #else |
155 | #define stricmp(a,b) strcmp (a, b) |
169 | # define stricmp(a,b) strcmp (a, b) |
156 | #define strlwr(s) (s) |
170 | # define strlwr(s) (s) |
157 | #define toupper(c) xtoupper(c) |
171 | #endif |
158 | #define tolower(c) xtolower(c) |
|
|
159 | |
172 | |
160 | #ifndef prompt |
173 | #ifndef prompt |
161 | # define prompt "ts> " |
174 | # define prompt "ts> " |
162 | #endif |
175 | #endif |
163 | |
176 | |
… | |
… | |
169 | # define FIRST_CELLSEGS 3 |
182 | # define FIRST_CELLSEGS 3 |
170 | #endif |
183 | #endif |
171 | |
184 | |
172 | enum scheme_types |
185 | enum scheme_types |
173 | { |
186 | { |
174 | T_NULL, |
187 | T_FREE, |
175 | T_STRING, |
188 | T_STRING, |
176 | T_NUMBER, |
189 | T_NUMBER, |
177 | T_SYMBOL, |
190 | T_SYMBOL, |
178 | T_PROC, |
191 | T_PROC, |
179 | T_PAIR, |
192 | T_PAIR, |
… | |
… | |
184 | T_PORT, |
197 | T_PORT, |
185 | T_VECTOR, |
198 | T_VECTOR, |
186 | T_MACRO, |
199 | T_MACRO, |
187 | T_PROMISE, |
200 | T_PROMISE, |
188 | T_ENVIRONMENT, |
201 | T_ENVIRONMENT, |
|
|
202 | /* one more... */ |
189 | T_NUM_SYSTEM_TYPES |
203 | T_NUM_SYSTEM_TYPES |
190 | }; |
204 | }; |
191 | |
205 | |
192 | #define T_MASKTYPE 31 /* 0000000000011111 */ |
206 | #define T_MASKTYPE 0x000f |
193 | #define T_SYNTAX 4096 /* 0001000000000000 */ |
207 | #define T_SYNTAX 0x0010 |
194 | #define T_IMMUTABLE 8192 /* 0010000000000000 */ |
208 | #define T_IMMUTABLE 0x0020 |
195 | #define T_ATOM 16384 /* 0100000000000000 */ /* only for gc */ |
209 | #define T_ATOM 0x0040 /* only for gc */ |
196 | #define CLRATOM 49151 /* 1011111111111111 */ /* only for gc */ |
210 | #define T_MARK 0x0080 /* only for gc */ |
197 | #define MARK 32768 /* 1000000000000000 */ |
|
|
198 | #define UNMARK 32767 /* 0111111111111111 */ |
|
|
199 | |
211 | |
|
|
212 | enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; |
200 | |
213 | |
201 | static num num_add (num a, num b); |
214 | static num num_op (enum num_op op, num a, num b); |
202 | static num num_mul (num a, num b); |
|
|
203 | static num num_div (num a, num b); |
|
|
204 | static num num_intdiv (num a, num b); |
215 | static num num_intdiv (num a, num b); |
205 | static num num_sub (num a, num b); |
|
|
206 | static num num_rem (num a, num b); |
216 | static num num_rem (num a, num b); |
207 | static num num_mod (num a, num b); |
217 | static num num_mod (num a, num b); |
208 | static int num_eq (num a, num b); |
|
|
209 | static int num_gt (num a, num b); |
|
|
210 | static int num_ge (num a, num b); |
|
|
211 | static int num_lt (num a, num b); |
|
|
212 | static int num_le (num a, num b); |
|
|
213 | |
218 | |
214 | #if USE_MATH |
219 | #if USE_MATH |
215 | static double round_per_R5RS (double x); |
220 | static double round_per_R5RS (double x); |
216 | #endif |
221 | #endif |
217 | static int is_zero_rvalue (RVALUE x); |
222 | static int is_zero_rvalue (RVALUE x); |
218 | |
223 | |
219 | static INLINE int |
224 | ecb_inline int |
220 | num_is_integer (pointer p) |
225 | num_is_integer (pointer p) |
221 | { |
226 | { |
222 | return num_is_fixnum (p->object.number); |
227 | return num_is_fixnum (p->object.number); |
223 | } |
228 | } |
224 | |
229 | |
… | |
… | |
228 | /* macros for cell operations */ |
233 | /* macros for cell operations */ |
229 | #define typeflag(p) ((p)->flag + 0) |
234 | #define typeflag(p) ((p)->flag + 0) |
230 | #define set_typeflag(p,v) ((p)->flag = (v)) |
235 | #define set_typeflag(p,v) ((p)->flag = (v)) |
231 | #define type(p) (typeflag (p) & T_MASKTYPE) |
236 | #define type(p) (typeflag (p) & T_MASKTYPE) |
232 | |
237 | |
233 | INTERFACE INLINE int |
238 | INTERFACE int |
234 | is_string (pointer p) |
239 | is_string (pointer p) |
235 | { |
240 | { |
236 | return type (p) == T_STRING; |
241 | return type (p) == T_STRING; |
237 | } |
242 | } |
238 | |
243 | |
239 | #define strvalue(p) ((p)->object.string.svalue) |
244 | #define strvalue(p) ((p)->object.string.svalue) |
240 | #define strlength(p) ((p)->object.string.length) |
245 | #define strlength(p) ((p)->object.string.length) |
241 | |
246 | |
242 | INTERFACE int is_list (SCHEME_P_ pointer p); |
247 | INTERFACE int is_list (SCHEME_P_ pointer p); |
|
|
248 | |
243 | INTERFACE INLINE int |
249 | INTERFACE int |
244 | is_vector (pointer p) |
250 | is_vector (pointer p) |
245 | { |
251 | { |
246 | return type (p) == T_VECTOR; |
252 | return type (p) == T_VECTOR; |
247 | } |
253 | } |
248 | |
254 | |
|
|
255 | #define vecvalue(p) ((p)->object.vector.vvalue) |
|
|
256 | #define veclength(p) ((p)->object.vector.length) |
249 | INTERFACE void fill_vector (pointer vec, pointer obj); |
257 | INTERFACE void fill_vector (pointer vec, pointer obj); |
|
|
258 | INTERFACE uint32_t vector_length (pointer vec); |
250 | INTERFACE pointer vector_elem (pointer vec, int ielem); |
259 | INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); |
251 | INTERFACE void set_vector_elem (pointer vec, int ielem, pointer a); |
260 | INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); |
252 | |
261 | |
|
|
262 | INTERFACE uint32_t |
|
|
263 | vector_length (pointer vec) |
|
|
264 | { |
|
|
265 | return vec->object.vector.length; |
|
|
266 | } |
|
|
267 | |
253 | INTERFACE INLINE int |
268 | INTERFACE int |
254 | is_number (pointer p) |
269 | is_number (pointer p) |
255 | { |
270 | { |
256 | return type (p) == T_NUMBER; |
271 | return type (p) == T_NUMBER; |
257 | } |
272 | } |
258 | |
273 | |
259 | INTERFACE INLINE int |
274 | INTERFACE int |
260 | is_integer (pointer p) |
275 | is_integer (pointer p) |
261 | { |
276 | { |
262 | if (!is_number (p)) |
277 | if (!is_number (p)) |
263 | return 0; |
278 | return 0; |
264 | |
279 | |
… | |
… | |
266 | return 1; |
281 | return 1; |
267 | |
282 | |
268 | return 0; |
283 | return 0; |
269 | } |
284 | } |
270 | |
285 | |
271 | INTERFACE INLINE int |
286 | INTERFACE int |
272 | is_real (pointer p) |
287 | is_real (pointer p) |
273 | { |
288 | { |
274 | return is_number (p) && !num_is_fixnum (p->object.number); |
289 | return is_number (p) && !num_is_fixnum (p->object.number); |
275 | } |
290 | } |
276 | |
291 | |
277 | INTERFACE INLINE int |
292 | INTERFACE int |
278 | is_character (pointer p) |
293 | is_character (pointer p) |
279 | { |
294 | { |
280 | return type (p) == T_CHARACTER; |
295 | return type (p) == T_CHARACTER; |
281 | } |
296 | } |
282 | |
297 | |
283 | INTERFACE INLINE char * |
298 | INTERFACE char * |
284 | string_value (pointer p) |
299 | string_value (pointer p) |
285 | { |
300 | { |
286 | return strvalue (p); |
301 | return strvalue (p); |
287 | } |
302 | } |
288 | |
303 | |
289 | INLINE num |
304 | ecb_inline num |
290 | nvalue (pointer p) |
305 | nvalue (pointer p) |
291 | { |
306 | { |
292 | return (p)->object.number; |
307 | return (p)->object.number; |
293 | } |
308 | } |
294 | |
309 | |
295 | INTERFACE long |
310 | static IVALUE |
|
|
311 | num_get_ivalue (const num n) |
|
|
312 | { |
|
|
313 | return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n); |
|
|
314 | } |
|
|
315 | |
|
|
316 | static RVALUE |
|
|
317 | num_get_rvalue (const num n) |
|
|
318 | { |
|
|
319 | return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n); |
|
|
320 | } |
|
|
321 | |
|
|
322 | INTERFACE IVALUE |
296 | ivalue (pointer p) |
323 | ivalue (pointer p) |
297 | { |
324 | { |
298 | return num_is_integer (p) ? num_ivalue ((p)->object.number) : (long) num_rvalue ((p)->object.number); |
325 | return num_get_ivalue (p->object.number); |
299 | } |
326 | } |
300 | |
327 | |
301 | INTERFACE RVALUE |
328 | INTERFACE RVALUE |
302 | rvalue (pointer p) |
329 | rvalue (pointer p) |
303 | { |
330 | { |
304 | return num_is_integer (p) ? (RVALUE) num_ivalue ((p)->object.number) : num_rvalue ((p)->object.number); |
331 | return num_get_rvalue (p->object.number); |
305 | } |
332 | } |
306 | |
333 | |
307 | #define ivalue_unchecked(p) ((p)->object.number.value.ivalue) |
334 | #define ivalue_unchecked(p) ((p)->object.number.value.ivalue) |
308 | #if USE_FLOAT |
335 | #if USE_REAL |
309 | # define rvalue_unchecked(p) ((p)->object.number.value.rvalue) |
336 | # define rvalue_unchecked(p) ((p)->object.number.value.rvalue) |
310 | # define set_num_integer(p) (p)->object.number.is_fixnum=1; |
337 | # define set_num_integer(p) (p)->object.number.is_fixnum=1; |
311 | # define set_num_real(p) (p)->object.number.is_fixnum=0; |
338 | # define set_num_real(p) (p)->object.number.is_fixnum=0; |
312 | #else |
339 | #else |
313 | # define rvalue_unchecked(p) ((p)->object.number.value.ivalue) |
340 | # define rvalue_unchecked(p) ((p)->object.number.value.ivalue) |
314 | # define set_num_integer(p) 0 |
341 | # define set_num_integer(p) 0 |
315 | # define set_num_real(p) 0 |
342 | # define set_num_real(p) 0 |
316 | #endif |
343 | #endif |
|
|
344 | |
317 | INTERFACE long |
345 | INTERFACE long |
318 | charvalue (pointer p) |
346 | charvalue (pointer p) |
319 | { |
347 | { |
320 | return ivalue_unchecked (p); |
348 | return ivalue_unchecked (p); |
321 | } |
349 | } |
322 | |
350 | |
323 | INTERFACE INLINE int |
351 | INTERFACE int |
324 | is_port (pointer p) |
352 | is_port (pointer p) |
325 | { |
353 | { |
326 | return type (p) == T_PORT; |
354 | return type (p) == T_PORT; |
327 | } |
355 | } |
328 | |
356 | |
329 | INTERFACE INLINE int |
357 | INTERFACE int |
330 | is_inport (pointer p) |
358 | is_inport (pointer p) |
331 | { |
359 | { |
332 | return is_port (p) && p->object.port->kind & port_input; |
360 | return is_port (p) && p->object.port->kind & port_input; |
333 | } |
361 | } |
334 | |
362 | |
335 | INTERFACE INLINE int |
363 | INTERFACE int |
336 | is_outport (pointer p) |
364 | is_outport (pointer p) |
337 | { |
365 | { |
338 | return is_port (p) && p->object.port->kind & port_output; |
366 | return is_port (p) && p->object.port->kind & port_output; |
339 | } |
367 | } |
340 | |
368 | |
341 | INTERFACE INLINE int |
369 | INTERFACE int |
342 | is_pair (pointer p) |
370 | is_pair (pointer p) |
343 | { |
371 | { |
344 | return type (p) == T_PAIR; |
372 | return type (p) == T_PAIR; |
345 | } |
373 | } |
346 | |
374 | |
347 | #define car(p) ((p)->object.cons.car + 0) |
375 | #define car(p) ((p)->object.cons.car + 0) |
348 | #define cdr(p) ((p)->object.cons.cdr) /* find_consecutive_cells uses &cdr */ |
376 | #define cdr(p) ((p)->object.cons.cdr + 0) |
349 | |
377 | |
350 | #define caar(p) car (car (p)) |
378 | static pointer caar (pointer p) { return car (car (p)); } |
351 | #define cadr(p) car (cdr (p)) |
379 | static pointer cadr (pointer p) { return car (cdr (p)); } |
352 | #define cdar(p) cdr (car (p)) |
380 | static pointer cdar (pointer p) { return cdr (car (p)); } |
353 | #define cddr(p) cdr (cdr (p)) |
381 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
354 | |
382 | |
355 | #define cadar(p) car (cdr (car (p))) |
383 | static pointer cadar (pointer p) { return car (cdr (car (p))); } |
356 | #define caddr(p) car (cdr (cdr (p))) |
384 | static pointer caddr (pointer p) { return car (cdr (cdr (p))); } |
357 | #define cdaar(p) cdr (car (car (p))) |
385 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
358 | |
386 | |
359 | INTERFACE void |
387 | INTERFACE void |
360 | set_car (pointer p, pointer q) |
388 | set_car (pointer p, pointer q) |
361 | { |
389 | { |
362 | p->object.cons.car = q; |
390 | p->object.cons.car = q; |
… | |
… | |
378 | pair_cdr (pointer p) |
406 | pair_cdr (pointer p) |
379 | { |
407 | { |
380 | return cdr (p); |
408 | return cdr (p); |
381 | } |
409 | } |
382 | |
410 | |
383 | INTERFACE INLINE int |
411 | INTERFACE int |
384 | is_symbol (pointer p) |
412 | is_symbol (pointer p) |
385 | { |
413 | { |
386 | return type (p) == T_SYMBOL; |
414 | return type (p) == T_SYMBOL; |
387 | } |
415 | } |
388 | |
416 | |
389 | INTERFACE INLINE char * |
417 | INTERFACE char * |
390 | symname (pointer p) |
418 | symname (pointer p) |
391 | { |
419 | { |
392 | return strvalue (car (p)); |
420 | return strvalue (car (p)); |
393 | } |
421 | } |
394 | |
422 | |
395 | #if USE_PLIST |
423 | #if USE_PLIST |
396 | SCHEME_EXPORT INLINE int |
424 | SCHEME_EXPORT int |
397 | hasprop (pointer p) |
425 | hasprop (pointer p) |
398 | { |
426 | { |
399 | return typeflag (p) & T_SYMBOL; |
427 | return typeflag (p) & T_SYMBOL; |
400 | } |
428 | } |
401 | |
429 | |
402 | # define symprop(p) cdr(p) |
430 | # define symprop(p) cdr(p) |
403 | #endif |
431 | #endif |
404 | |
432 | |
405 | INTERFACE INLINE int |
433 | INTERFACE int |
406 | is_syntax (pointer p) |
434 | is_syntax (pointer p) |
407 | { |
435 | { |
408 | return typeflag (p) & T_SYNTAX; |
436 | return typeflag (p) & T_SYNTAX; |
409 | } |
437 | } |
410 | |
438 | |
411 | INTERFACE INLINE int |
439 | INTERFACE int |
412 | is_proc (pointer p) |
440 | is_proc (pointer p) |
413 | { |
441 | { |
414 | return type (p) == T_PROC; |
442 | return type (p) == T_PROC; |
415 | } |
443 | } |
416 | |
444 | |
417 | INTERFACE INLINE int |
445 | INTERFACE int |
418 | is_foreign (pointer p) |
446 | is_foreign (pointer p) |
419 | { |
447 | { |
420 | return type (p) == T_FOREIGN; |
448 | return type (p) == T_FOREIGN; |
421 | } |
449 | } |
422 | |
450 | |
423 | INTERFACE INLINE char * |
451 | INTERFACE char * |
424 | syntaxname (pointer p) |
452 | syntaxname (pointer p) |
425 | { |
453 | { |
426 | return strvalue (car (p)); |
454 | return strvalue (car (p)); |
427 | } |
455 | } |
428 | |
456 | |
429 | #define procnum(p) ivalue(p) |
457 | #define procnum(p) ivalue (p) |
430 | static const char *procname (pointer x); |
458 | static const char *procname (pointer x); |
431 | |
459 | |
432 | INTERFACE INLINE int |
460 | INTERFACE int |
433 | is_closure (pointer p) |
461 | is_closure (pointer p) |
434 | { |
462 | { |
435 | return type (p) == T_CLOSURE; |
463 | return type (p) == T_CLOSURE; |
436 | } |
464 | } |
437 | |
465 | |
438 | INTERFACE INLINE int |
466 | INTERFACE int |
439 | is_macro (pointer p) |
467 | is_macro (pointer p) |
440 | { |
468 | { |
441 | return type (p) == T_MACRO; |
469 | return type (p) == T_MACRO; |
442 | } |
470 | } |
443 | |
471 | |
444 | INTERFACE INLINE pointer |
472 | INTERFACE pointer |
445 | closure_code (pointer p) |
473 | closure_code (pointer p) |
446 | { |
474 | { |
447 | return car (p); |
475 | return car (p); |
448 | } |
476 | } |
449 | |
477 | |
450 | INTERFACE INLINE pointer |
478 | INTERFACE pointer |
451 | closure_env (pointer p) |
479 | closure_env (pointer p) |
452 | { |
480 | { |
453 | return cdr (p); |
481 | return cdr (p); |
454 | } |
482 | } |
455 | |
483 | |
456 | INTERFACE INLINE int |
484 | INTERFACE int |
457 | is_continuation (pointer p) |
485 | is_continuation (pointer p) |
458 | { |
486 | { |
459 | return type (p) == T_CONTINUATION; |
487 | return type (p) == T_CONTINUATION; |
460 | } |
488 | } |
461 | |
489 | |
462 | #define cont_dump(p) cdr (p) |
490 | #define cont_dump(p) cdr (p) |
463 | #define set_cont_dump(p,v) set_cdr ((p), (v)) |
491 | #define set_cont_dump(p,v) set_cdr ((p), (v)) |
464 | |
492 | |
465 | /* To do: promise should be forced ONCE only */ |
493 | /* To do: promise should be forced ONCE only */ |
466 | INTERFACE INLINE int |
494 | INTERFACE int |
467 | is_promise (pointer p) |
495 | is_promise (pointer p) |
468 | { |
496 | { |
469 | return type (p) == T_PROMISE; |
497 | return type (p) == T_PROMISE; |
470 | } |
498 | } |
471 | |
499 | |
472 | INTERFACE INLINE int |
500 | INTERFACE int |
473 | is_environment (pointer p) |
501 | is_environment (pointer p) |
474 | { |
502 | { |
475 | return type (p) == T_ENVIRONMENT; |
503 | return type (p) == T_ENVIRONMENT; |
476 | } |
504 | } |
477 | |
505 | |
478 | #define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT) |
506 | #define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT) |
479 | |
507 | |
480 | #define is_atom(p) (typeflag (p) & T_ATOM) |
508 | #define is_atom(p) (typeflag (p) & T_ATOM) |
481 | #define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) |
509 | #define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) |
482 | #define clratom(p) set_typeflag ((p), typeflag (p) & CLRATOM) |
510 | #define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM) |
483 | |
511 | |
484 | #define is_mark(p) (typeflag (p) & MARK) |
512 | #define is_mark(p) (typeflag (p) & T_MARK) |
485 | #define setmark(p) set_typeflag ((p), typeflag (p) | MARK) |
513 | #define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) |
486 | #define clrmark(p) set_typeflag ((p), typeflag (p) & UNMARK) |
514 | #define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) |
487 | |
515 | |
488 | INTERFACE INLINE int |
516 | INTERFACE int |
489 | is_immutable (pointer p) |
517 | is_immutable (pointer p) |
490 | { |
518 | { |
491 | return typeflag (p) & T_IMMUTABLE; |
519 | return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; |
492 | } |
520 | } |
493 | |
521 | |
494 | INTERFACE INLINE void |
522 | INTERFACE void |
495 | setimmutable (pointer p) |
523 | setimmutable (pointer p) |
496 | { |
524 | { |
|
|
525 | #if USE_ERROR_CHECKING |
497 | set_typeflag (p, T_IMMUTABLE); |
526 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
|
|
527 | #endif |
498 | } |
528 | } |
499 | |
529 | |
500 | #if USE_CHAR_CLASSIFIERS |
530 | #if USE_CHAR_CLASSIFIERS |
501 | static INLINE int |
531 | ecb_inline int |
502 | Cisalpha (int c) |
532 | Cisalpha (int c) |
503 | { |
533 | { |
504 | return isascii (c) && isalpha (c); |
534 | return isascii (c) && isalpha (c); |
505 | } |
535 | } |
506 | |
536 | |
507 | static INLINE int |
537 | ecb_inline int |
508 | Cisdigit (int c) |
538 | Cisdigit (int c) |
509 | { |
539 | { |
510 | return isascii (c) && isdigit (c); |
540 | return isascii (c) && isdigit (c); |
511 | } |
541 | } |
512 | |
542 | |
513 | static INLINE int |
543 | ecb_inline int |
514 | Cisspace (int c) |
544 | Cisspace (int c) |
515 | { |
545 | { |
516 | return isascii (c) && isspace (c); |
546 | return isascii (c) && isspace (c); |
517 | } |
547 | } |
518 | |
548 | |
519 | static INLINE int |
549 | ecb_inline int |
520 | Cisupper (int c) |
550 | Cisupper (int c) |
521 | { |
551 | { |
522 | return isascii (c) && isupper (c); |
552 | return isascii (c) && isupper (c); |
523 | } |
553 | } |
524 | |
554 | |
525 | static INLINE int |
555 | ecb_inline int |
526 | Cislower (int c) |
556 | Cislower (int c) |
527 | { |
557 | { |
528 | return isascii (c) && islower (c); |
558 | return isascii (c) && islower (c); |
529 | } |
559 | } |
530 | #endif |
560 | #endif |
… | |
… | |
591 | #endif |
621 | #endif |
592 | |
622 | |
593 | static int file_push (SCHEME_P_ const char *fname); |
623 | static int file_push (SCHEME_P_ const char *fname); |
594 | static void file_pop (SCHEME_P); |
624 | static void file_pop (SCHEME_P); |
595 | static int file_interactive (SCHEME_P); |
625 | static int file_interactive (SCHEME_P); |
596 | static INLINE int is_one_of (char *s, int c); |
626 | ecb_inline int is_one_of (char *s, int c); |
597 | static int alloc_cellseg (SCHEME_P_ int n); |
627 | static int alloc_cellseg (SCHEME_P_ int n); |
598 | static long binary_decode (const char *s); |
|
|
599 | static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); |
|
|
600 | static pointer xget_cell (SCHEME_P_ pointer a, pointer b); |
628 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
601 | static pointer reserve_cells (SCHEME_P_ int n); |
|
|
602 | static pointer get_consecutive_cells (SCHEME_P_ int n); |
|
|
603 | static pointer find_consecutive_cells (SCHEME_P_ int n); |
|
|
604 | static void finalize_cell (SCHEME_P_ pointer a); |
629 | static void finalize_cell (SCHEME_P_ pointer a); |
605 | static int count_consecutive_cells (pointer x, int needed); |
630 | static int count_consecutive_cells (pointer x, int needed); |
606 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
631 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
607 | static pointer mk_number (SCHEME_P_ num n); |
632 | static pointer mk_number (SCHEME_P_ const num n); |
608 | static char *store_string (SCHEME_P_ int len, const char *str, char fill); |
633 | static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); |
609 | static pointer mk_vector (SCHEME_P_ int len); |
634 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
610 | static pointer mk_atom (SCHEME_P_ char *q); |
635 | static pointer mk_atom (SCHEME_P_ char *q); |
611 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
636 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
612 | |
637 | |
613 | #if USE_PORTS |
638 | #if USE_PORTS |
614 | static pointer mk_port (SCHEME_P_ port *p); |
639 | static pointer mk_port (SCHEME_P_ port *p); |
… | |
… | |
625 | static int basic_inchar (port *pt); |
650 | static int basic_inchar (port *pt); |
626 | static int inchar (SCHEME_P); |
651 | static int inchar (SCHEME_P); |
627 | static void backchar (SCHEME_P_ int c); |
652 | static void backchar (SCHEME_P_ int c); |
628 | static char *readstr_upto (SCHEME_P_ char *delim); |
653 | static char *readstr_upto (SCHEME_P_ char *delim); |
629 | static pointer readstrexp (SCHEME_P); |
654 | static pointer readstrexp (SCHEME_P); |
630 | static INLINE int skipspace (SCHEME_P); |
655 | ecb_inline int skipspace (SCHEME_P); |
631 | static int token (SCHEME_P); |
656 | static int token (SCHEME_P); |
632 | static void printslashstring (SCHEME_P_ char *s, int len); |
657 | static void printslashstring (SCHEME_P_ char *s, int len); |
633 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
658 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
634 | static void printatom (SCHEME_P_ pointer l, int f); |
659 | static void printatom (SCHEME_P_ pointer l, int f); |
635 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
660 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
… | |
… | |
639 | static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); |
664 | static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); |
640 | static pointer revappend (SCHEME_P_ pointer a, pointer b); |
665 | static pointer revappend (SCHEME_P_ pointer a, pointer b); |
641 | static pointer ss_get_cont (SCHEME_P); |
666 | static pointer ss_get_cont (SCHEME_P); |
642 | static void ss_set_cont (SCHEME_P_ pointer cont); |
667 | static void ss_set_cont (SCHEME_P_ pointer cont); |
643 | static void dump_stack_mark (SCHEME_P); |
668 | static void dump_stack_mark (SCHEME_P); |
644 | static pointer opexe_0 (SCHEME_P_ enum scheme_opcodes op); |
669 | static int opexe_0 (SCHEME_P_ enum scheme_opcodes op); |
|
|
670 | static int opexe_1 (SCHEME_P_ enum scheme_opcodes op); |
645 | static pointer opexe_2 (SCHEME_P_ enum scheme_opcodes op); |
671 | static int opexe_2 (SCHEME_P_ enum scheme_opcodes op); |
646 | static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); |
672 | static int opexe_3 (SCHEME_P_ enum scheme_opcodes op); |
647 | static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); |
673 | static int opexe_4 (SCHEME_P_ enum scheme_opcodes op); |
648 | static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); |
674 | static int opexe_5 (SCHEME_P_ enum scheme_opcodes op); |
649 | static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); |
675 | static int opexe_6 (SCHEME_P_ enum scheme_opcodes op); |
650 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
676 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
651 | static void assign_syntax (SCHEME_P_ char *name); |
677 | static void assign_syntax (SCHEME_P_ const char *name); |
652 | static int syntaxnum (pointer p); |
678 | static int syntaxnum (pointer p); |
653 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, char *name); |
679 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
654 | |
680 | |
655 | static num |
681 | static num |
656 | num_add (num a, num b) |
682 | num_op (enum num_op op, num a, num b) |
657 | { |
683 | { |
658 | num ret; |
684 | num ret; |
659 | |
685 | |
660 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
686 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
661 | |
687 | |
662 | if (num_is_fixnum (ret)) |
688 | if (num_is_fixnum (ret)) |
663 | num_set_ivalue (ret, a.value.ivalue + b.value.ivalue); |
689 | { |
|
|
690 | IVALUE av = num_get_ivalue (a); |
|
|
691 | IVALUE bv = num_get_ivalue (b); |
|
|
692 | |
|
|
693 | switch (op) |
|
|
694 | { |
|
|
695 | case NUM_ADD: av += bv; break; |
|
|
696 | case NUM_SUB: av -= bv; break; |
|
|
697 | case NUM_MUL: av *= bv; break; |
|
|
698 | case NUM_INTDIV: av /= bv; break; |
|
|
699 | } |
|
|
700 | |
|
|
701 | num_set_ivalue (ret, av); |
|
|
702 | } |
664 | else |
703 | else |
665 | num_set_rvalue (ret, num_rvalue (a) + num_rvalue (b)); |
704 | { |
|
|
705 | RVALUE av = num_get_rvalue (a); |
|
|
706 | RVALUE bv = num_get_rvalue (b); |
666 | |
707 | |
667 | return ret; |
708 | switch (op) |
668 | } |
709 | { |
|
|
710 | case NUM_ADD: av += bv; break; |
|
|
711 | case NUM_SUB: av -= bv; break; |
|
|
712 | case NUM_MUL: av *= bv; break; |
|
|
713 | case NUM_INTDIV: av /= bv; break; |
|
|
714 | } |
669 | |
715 | |
670 | static num |
716 | num_set_rvalue (ret, av); |
671 | num_mul (num a, num b) |
717 | } |
672 | { |
|
|
673 | num ret; |
|
|
674 | |
|
|
675 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
|
|
676 | |
|
|
677 | if (num_is_fixnum (ret)) |
|
|
678 | num_set_ivalue (ret, a.value.ivalue * b.value.ivalue); |
|
|
679 | else |
|
|
680 | num_set_rvalue (ret, num_rvalue (a) * num_rvalue (b)); |
|
|
681 | |
718 | |
682 | return ret; |
719 | return ret; |
683 | } |
720 | } |
684 | |
721 | |
685 | static num |
722 | static num |
686 | num_div (num a, num b) |
723 | num_div (num a, num b) |
687 | { |
724 | { |
688 | num ret; |
725 | num ret; |
689 | |
726 | |
690 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && a.value.ivalue % b.value.ivalue == 0); |
727 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); |
691 | |
728 | |
692 | if (num_is_fixnum (ret)) |
729 | if (num_is_fixnum (ret)) |
693 | num_set_ivalue (ret, a.value.ivalue / b.value.ivalue); |
730 | num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); |
694 | else |
731 | else |
695 | num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); |
732 | num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); |
696 | |
|
|
697 | return ret; |
|
|
698 | } |
|
|
699 | |
|
|
700 | static num |
|
|
701 | num_intdiv (num a, num b) |
|
|
702 | { |
|
|
703 | num ret; |
|
|
704 | |
|
|
705 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
|
|
706 | |
|
|
707 | if (num_is_fixnum (ret)) |
|
|
708 | num_set_ivalue (ret, a.value.ivalue / b.value.ivalue); |
|
|
709 | else |
|
|
710 | num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); |
|
|
711 | |
|
|
712 | return ret; |
|
|
713 | } |
|
|
714 | |
|
|
715 | static num |
|
|
716 | num_sub (num a, num b) |
|
|
717 | { |
|
|
718 | num ret; |
|
|
719 | |
|
|
720 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
|
|
721 | |
|
|
722 | if (num_is_fixnum (ret)) |
|
|
723 | num_set_ivalue (ret, a.value.ivalue - b.value.ivalue); |
|
|
724 | else |
|
|
725 | num_set_rvalue (ret, num_rvalue (a) - num_rvalue (b)); |
|
|
726 | |
733 | |
727 | return ret; |
734 | return ret; |
728 | } |
735 | } |
729 | |
736 | |
730 | static num |
737 | static num |
… | |
… | |
732 | { |
739 | { |
733 | num ret; |
740 | num ret; |
734 | long e1, e2, res; |
741 | long e1, e2, res; |
735 | |
742 | |
736 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
743 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
737 | e1 = num_ivalue (a); |
744 | e1 = num_get_ivalue (a); |
738 | e2 = num_ivalue (b); |
745 | e2 = num_get_ivalue (b); |
739 | res = e1 % e2; |
746 | res = e1 % e2; |
740 | |
747 | |
741 | /* remainder should have same sign as second operand */ |
748 | /* remainder should have same sign as second operand */ |
742 | if (res > 0) |
749 | if (res > 0) |
743 | { |
750 | { |
… | |
… | |
759 | { |
766 | { |
760 | num ret; |
767 | num ret; |
761 | long e1, e2, res; |
768 | long e1, e2, res; |
762 | |
769 | |
763 | 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)); |
764 | e1 = num_ivalue (a); |
771 | e1 = num_get_ivalue (a); |
765 | e2 = num_ivalue (b); |
772 | e2 = num_get_ivalue (b); |
766 | res = e1 % e2; |
773 | res = e1 % e2; |
767 | |
774 | |
768 | /* modulo should have same sign as second operand */ |
775 | /* modulo should have same sign as second operand */ |
769 | if (res * e2 < 0) |
776 | if (res * e2 < 0) |
770 | res += e2; |
777 | res += e2; |
771 | |
778 | |
772 | num_set_ivalue (ret, res); |
779 | num_set_ivalue (ret, res); |
773 | return ret; |
780 | return ret; |
774 | } |
781 | } |
775 | |
782 | |
|
|
783 | /* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */ |
776 | static int |
784 | static int |
777 | num_eq (num a, num b) |
785 | num_cmp (num a, num b) |
778 | { |
786 | { |
|
|
787 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
779 | int ret; |
788 | int ret; |
780 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
|
|
781 | |
789 | |
782 | if (is_fixnum) |
790 | if (is_fixnum) |
783 | ret = a.value.ivalue == b.value.ivalue; |
791 | { |
|
|
792 | IVALUE av = num_get_ivalue (a); |
|
|
793 | IVALUE bv = num_get_ivalue (b); |
|
|
794 | |
|
|
795 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
|
|
796 | } |
784 | else |
797 | else |
785 | ret = num_rvalue (a) == num_rvalue (b); |
798 | { |
|
|
799 | RVALUE av = num_get_rvalue (a); |
|
|
800 | RVALUE bv = num_get_rvalue (b); |
|
|
801 | |
|
|
802 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
|
|
803 | } |
786 | |
804 | |
787 | return ret; |
805 | return ret; |
788 | } |
|
|
789 | |
|
|
790 | |
|
|
791 | static int |
|
|
792 | num_gt (num a, num b) |
|
|
793 | { |
|
|
794 | int ret; |
|
|
795 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
|
|
796 | |
|
|
797 | if (is_fixnum) |
|
|
798 | ret = a.value.ivalue > b.value.ivalue; |
|
|
799 | else |
|
|
800 | ret = num_rvalue (a) > num_rvalue (b); |
|
|
801 | |
|
|
802 | return ret; |
|
|
803 | } |
|
|
804 | |
|
|
805 | static int |
|
|
806 | num_ge (num a, num b) |
|
|
807 | { |
|
|
808 | return !num_lt (a, b); |
|
|
809 | } |
|
|
810 | |
|
|
811 | static int |
|
|
812 | num_lt (num a, num b) |
|
|
813 | { |
|
|
814 | int ret; |
|
|
815 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
|
|
816 | |
|
|
817 | if (is_fixnum) |
|
|
818 | ret = a.value.ivalue < b.value.ivalue; |
|
|
819 | else |
|
|
820 | ret = num_rvalue (a) < num_rvalue (b); |
|
|
821 | |
|
|
822 | return ret; |
|
|
823 | } |
|
|
824 | |
|
|
825 | static int |
|
|
826 | num_le (num a, num b) |
|
|
827 | { |
|
|
828 | return !num_gt (a, b); |
|
|
829 | } |
806 | } |
830 | |
807 | |
831 | #if USE_MATH |
808 | #if USE_MATH |
832 | |
809 | |
833 | /* Round to nearest. Round to even if midway */ |
810 | /* Round to nearest. Round to even if midway */ |
… | |
… | |
843 | return ce; |
820 | return ce; |
844 | else if (dfl < dce) |
821 | else if (dfl < dce) |
845 | return fl; |
822 | return fl; |
846 | else |
823 | else |
847 | { |
824 | { |
848 | if (fmod (fl, 2.0) == 0.0) /* I imagine this holds */ |
825 | if (fmod (fl, 2) == 0) /* I imagine this holds */ |
849 | return fl; |
826 | return fl; |
850 | else |
827 | else |
851 | return ce; |
828 | return ce; |
852 | } |
829 | } |
853 | } |
830 | } |
854 | #endif |
831 | #endif |
855 | |
832 | |
856 | static int |
833 | static int |
857 | is_zero_rvalue (RVALUE x) |
834 | is_zero_rvalue (RVALUE x) |
858 | { |
835 | { |
859 | #if USE_FLOAT |
836 | return x == 0; |
|
|
837 | #if 0 |
|
|
838 | #if USE_REAL |
860 | return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ |
839 | return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ |
861 | #else |
840 | #else |
862 | return x == 0; |
841 | return x == 0; |
863 | #endif |
842 | #endif |
864 | } |
843 | #endif |
865 | |
|
|
866 | static long |
|
|
867 | binary_decode (const char *s) |
|
|
868 | { |
|
|
869 | long x = 0; |
|
|
870 | |
|
|
871 | while (*s != 0 && (*s == '1' || *s == '0')) |
|
|
872 | { |
|
|
873 | x <<= 1; |
|
|
874 | x += *s - '0'; |
|
|
875 | s++; |
|
|
876 | } |
|
|
877 | |
|
|
878 | return x; |
|
|
879 | } |
844 | } |
880 | |
845 | |
881 | /* allocate new cell segment */ |
846 | /* allocate new cell segment */ |
882 | static int |
847 | static int |
883 | alloc_cellseg (SCHEME_P_ int n) |
848 | alloc_cellseg (SCHEME_P_ int n) |
… | |
… | |
927 | SCHEME_V->fcells += segsize; |
892 | SCHEME_V->fcells += segsize; |
928 | last = newp + segsize - 1; |
893 | last = newp + segsize - 1; |
929 | |
894 | |
930 | for (p = newp; p <= last; p++) |
895 | for (p = newp; p <= last; p++) |
931 | { |
896 | { |
932 | set_typeflag (p, 0); |
897 | set_typeflag (p, T_FREE); |
|
|
898 | set_car (p, NIL); |
933 | set_cdr (p, p + 1); |
899 | set_cdr (p, p + 1); |
934 | set_car (p, NIL); |
|
|
935 | } |
900 | } |
936 | |
901 | |
937 | /* insert new cells in address order on free list */ |
902 | /* insert new cells in address order on free list */ |
938 | if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell) |
903 | if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell) |
939 | { |
904 | { |
… | |
… | |
953 | } |
918 | } |
954 | |
919 | |
955 | return n; |
920 | return n; |
956 | } |
921 | } |
957 | |
922 | |
958 | /* get new cell. parameter a, b is marked by gc. */ |
923 | /* get new cell. parameter a, b is marked by gc. */ |
959 | static INLINE pointer |
924 | ecb_inline pointer |
960 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
925 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
961 | { |
926 | { |
962 | if (SCHEME_V->free_cell == NIL) |
927 | if (ecb_expect_false (SCHEME_V->free_cell == NIL)) |
963 | { |
928 | { |
964 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
929 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
965 | return S_SINK; |
930 | return S_SINK; |
966 | |
931 | |
967 | if (SCHEME_V->free_cell == NIL) |
932 | if (SCHEME_V->free_cell == NIL) |
… | |
… | |
991 | --SCHEME_V->fcells; |
956 | --SCHEME_V->fcells; |
992 | return x; |
957 | return x; |
993 | } |
958 | } |
994 | } |
959 | } |
995 | |
960 | |
996 | /* make sure that there is a given number of cells free */ |
|
|
997 | static pointer |
|
|
998 | reserve_cells (SCHEME_P_ int n) |
|
|
999 | { |
|
|
1000 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
|
|
1001 | return NIL; |
|
|
1002 | |
|
|
1003 | /* Are there enough cells available? */ |
|
|
1004 | if (SCHEME_V->fcells < n) |
|
|
1005 | { |
|
|
1006 | /* If not, try gc'ing some */ |
|
|
1007 | gc (SCHEME_A_ NIL, NIL); |
|
|
1008 | |
|
|
1009 | if (SCHEME_V->fcells < n) |
|
|
1010 | { |
|
|
1011 | /* If there still aren't, try getting more heap */ |
|
|
1012 | if (!alloc_cellseg (SCHEME_A_ 1) && USE_ERROR_CHECKING) |
|
|
1013 | { |
|
|
1014 | SCHEME_V->no_memory = 1; |
|
|
1015 | return NIL; |
|
|
1016 | } |
|
|
1017 | } |
|
|
1018 | |
|
|
1019 | if (SCHEME_V->fcells < n && USE_ERROR_CHECKING) |
|
|
1020 | { |
|
|
1021 | /* If all fail, report failure */ |
|
|
1022 | SCHEME_V->no_memory = 1; |
|
|
1023 | return NIL; |
|
|
1024 | } |
|
|
1025 | } |
|
|
1026 | |
|
|
1027 | return S_T; |
|
|
1028 | } |
|
|
1029 | |
|
|
1030 | static pointer |
|
|
1031 | get_consecutive_cells (SCHEME_P_ int n) |
|
|
1032 | { |
|
|
1033 | pointer x; |
|
|
1034 | |
|
|
1035 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
|
|
1036 | return S_SINK; |
|
|
1037 | |
|
|
1038 | /* Are there any cells available? */ |
|
|
1039 | x = find_consecutive_cells (SCHEME_A_ n); |
|
|
1040 | |
|
|
1041 | if (x != NIL) |
|
|
1042 | return x; |
|
|
1043 | |
|
|
1044 | /* If not, try gc'ing some */ |
|
|
1045 | gc (SCHEME_A_ NIL, NIL); |
|
|
1046 | |
|
|
1047 | for (;;) |
|
|
1048 | { |
|
|
1049 | x = find_consecutive_cells (SCHEME_A_ n); |
|
|
1050 | |
|
|
1051 | if (x != NIL) |
|
|
1052 | return x; |
|
|
1053 | |
|
|
1054 | /* If there still aren't, try getting more heap */ |
|
|
1055 | if (!alloc_cellseg (SCHEME_A_ 1)) |
|
|
1056 | { |
|
|
1057 | #if USE_ERROR_CHECKING |
|
|
1058 | SCHEME_V->no_memory = 1; |
|
|
1059 | return S_SINK; |
|
|
1060 | #endif |
|
|
1061 | } |
|
|
1062 | } |
|
|
1063 | } |
|
|
1064 | |
|
|
1065 | static int |
|
|
1066 | count_consecutive_cells (pointer x, int needed) |
|
|
1067 | { |
|
|
1068 | int n = 1; |
|
|
1069 | |
|
|
1070 | while (cdr (x) == x + 1) |
|
|
1071 | { |
|
|
1072 | x = cdr (x); |
|
|
1073 | n++; |
|
|
1074 | |
|
|
1075 | if (n > needed) |
|
|
1076 | return n; |
|
|
1077 | } |
|
|
1078 | |
|
|
1079 | return n; |
|
|
1080 | } |
|
|
1081 | |
|
|
1082 | static pointer |
|
|
1083 | find_consecutive_cells (SCHEME_P_ int n) |
|
|
1084 | { |
|
|
1085 | pointer *pp; |
|
|
1086 | int cnt; |
|
|
1087 | |
|
|
1088 | pp = &SCHEME_V->free_cell; |
|
|
1089 | |
|
|
1090 | while (*pp != NIL) |
|
|
1091 | { |
|
|
1092 | cnt = count_consecutive_cells (*pp, n); |
|
|
1093 | |
|
|
1094 | if (cnt >= n) |
|
|
1095 | { |
|
|
1096 | pointer x = *pp; |
|
|
1097 | |
|
|
1098 | *pp = cdr (*pp + n - 1); |
|
|
1099 | SCHEME_V->fcells -= n; |
|
|
1100 | return x; |
|
|
1101 | } |
|
|
1102 | |
|
|
1103 | pp = &cdr (*pp + cnt - 1); |
|
|
1104 | } |
|
|
1105 | |
|
|
1106 | return NIL; |
|
|
1107 | } |
|
|
1108 | |
|
|
1109 | /* To retain recent allocs before interpreter knows about them - |
961 | /* To retain recent allocs before interpreter knows about them - |
1110 | Tehom */ |
962 | Tehom */ |
1111 | |
963 | |
1112 | static void |
964 | static void |
1113 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
965 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
1114 | { |
966 | { |
1115 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
967 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
1116 | |
968 | |
1117 | set_typeflag (holder, T_PAIR | T_IMMUTABLE); |
969 | set_typeflag (holder, T_PAIR); |
|
|
970 | setimmutable (holder); |
1118 | set_car (holder, recent); |
971 | set_car (holder, recent); |
1119 | set_cdr (holder, car (S_SINK)); |
972 | set_cdr (holder, car (S_SINK)); |
1120 | set_car (S_SINK, holder); |
973 | set_car (S_SINK, holder); |
1121 | } |
974 | } |
1122 | |
975 | |
… | |
… | |
1135 | |
988 | |
1136 | return cell; |
989 | return cell; |
1137 | } |
990 | } |
1138 | |
991 | |
1139 | static pointer |
992 | static pointer |
1140 | get_vector_object (SCHEME_P_ int len, pointer init) |
993 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1141 | { |
994 | { |
1142 | pointer cells = get_consecutive_cells (SCHEME_A_ len / 2 + len % 2 + 1); |
995 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
|
|
996 | pointer *e = malloc (len * sizeof (pointer)); |
1143 | |
997 | |
1144 | #if USE_ERROR_CHECKING |
998 | if (!e && USE_ERROR_CHECKING) |
1145 | if (SCHEME_V->no_memory) |
|
|
1146 | return S_SINK; |
999 | return S_SINK; |
1147 | #endif |
|
|
1148 | |
1000 | |
1149 | /* Record it as a vector so that gc understands it. */ |
1001 | /* Record it as a vector so that gc understands it. */ |
1150 | set_typeflag (cells, T_VECTOR | T_ATOM); |
1002 | set_typeflag (v, T_VECTOR | T_ATOM); |
1151 | ivalue_unchecked (cells) = len; |
1003 | |
1152 | set_num_integer (cells); |
1004 | v->object.vector.vvalue = e; |
|
|
1005 | v->object.vector.length = len; |
1153 | fill_vector (cells, init); |
1006 | fill_vector (v, init); |
1154 | push_recent_alloc (SCHEME_A_ cells, NIL); |
1007 | push_recent_alloc (SCHEME_A_ v, NIL); |
1155 | |
1008 | |
1156 | return cells; |
1009 | return v; |
1157 | } |
1010 | } |
1158 | |
1011 | |
1159 | static INLINE void |
1012 | ecb_inline void |
1160 | ok_to_freely_gc (SCHEME_P) |
1013 | ok_to_freely_gc (SCHEME_P) |
1161 | { |
1014 | { |
1162 | set_car (S_SINK, NIL); |
1015 | set_car (S_SINK, NIL); |
1163 | } |
1016 | } |
1164 | |
1017 | |
1165 | #if defined TSGRIND |
1018 | #if defined TSGRIND |
1166 | static void |
1019 | static void |
1167 | check_cell_alloced (pointer p, int expect_alloced) |
1020 | check_cell_alloced (pointer p, int expect_alloced) |
1168 | { |
1021 | { |
1169 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to |
1022 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1170 | sc. */ |
|
|
1171 | if (typeflag (p) & !expect_alloced) |
1023 | if (typeflag (p) & !expect_alloced) |
1172 | xwrstr ("Cell is already allocated!\n"); |
1024 | xwrstr ("Cell is already allocated!\n"); |
1173 | |
1025 | |
1174 | if (!(typeflag (p)) & expect_alloced) |
1026 | if (!(typeflag (p)) & expect_alloced) |
1175 | xwrstr ("Cell is not allocated!\n"); |
1027 | xwrstr ("Cell is not allocated!\n"); |
… | |
… | |
1198 | if (immutable) |
1050 | if (immutable) |
1199 | setimmutable (x); |
1051 | setimmutable (x); |
1200 | |
1052 | |
1201 | set_car (x, a); |
1053 | set_car (x, a); |
1202 | set_cdr (x, b); |
1054 | set_cdr (x, b); |
|
|
1055 | |
1203 | return x; |
1056 | return x; |
1204 | } |
1057 | } |
1205 | |
1058 | |
1206 | /* ========== oblist implementation ========== */ |
1059 | /* ========== oblist implementation ========== */ |
1207 | |
1060 | |
… | |
… | |
1217 | |
1070 | |
1218 | /* returns the new symbol */ |
1071 | /* returns the new symbol */ |
1219 | static pointer |
1072 | static pointer |
1220 | oblist_add_by_name (SCHEME_P_ const char *name) |
1073 | oblist_add_by_name (SCHEME_P_ const char *name) |
1221 | { |
1074 | { |
1222 | pointer x; |
|
|
1223 | int location; |
1075 | int location; |
1224 | |
1076 | |
1225 | x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
1077 | pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
1226 | set_typeflag (x, T_SYMBOL); |
1078 | set_typeflag (x, T_SYMBOL); |
1227 | setimmutable (car (x)); |
1079 | setimmutable (car (x)); |
1228 | |
1080 | |
1229 | location = hash_fn (name, ivalue_unchecked (SCHEME_V->oblist)); |
1081 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1230 | set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); |
1082 | set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); |
1231 | return x; |
1083 | return x; |
1232 | } |
1084 | } |
1233 | |
1085 | |
1234 | static INLINE pointer |
1086 | ecb_inline pointer |
1235 | oblist_find_by_name (SCHEME_P_ const char *name) |
1087 | oblist_find_by_name (SCHEME_P_ const char *name) |
1236 | { |
1088 | { |
1237 | int location; |
1089 | int location; |
1238 | pointer x; |
1090 | pointer x; |
1239 | char *s; |
1091 | char *s; |
1240 | |
1092 | |
1241 | location = hash_fn (name, ivalue_unchecked (SCHEME_V->oblist)); |
1093 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1242 | |
1094 | |
1243 | for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1095 | for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1244 | { |
1096 | { |
1245 | s = symname (car (x)); |
1097 | s = symname (car (x)); |
1246 | |
1098 | |
… | |
… | |
1257 | { |
1109 | { |
1258 | int i; |
1110 | int i; |
1259 | pointer x; |
1111 | pointer x; |
1260 | pointer ob_list = NIL; |
1112 | pointer ob_list = NIL; |
1261 | |
1113 | |
1262 | for (i = 0; i < ivalue_unchecked (SCHEME_V->oblist); i++) |
1114 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1263 | for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1115 | for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1264 | ob_list = cons (x, ob_list); |
1116 | ob_list = cons (x, ob_list); |
1265 | |
1117 | |
1266 | return ob_list; |
1118 | return ob_list; |
1267 | } |
1119 | } |
… | |
… | |
1272 | oblist_initial_value (SCHEME_P) |
1124 | oblist_initial_value (SCHEME_P) |
1273 | { |
1125 | { |
1274 | return NIL; |
1126 | return NIL; |
1275 | } |
1127 | } |
1276 | |
1128 | |
1277 | static INLINE pointer |
1129 | ecb_inline pointer |
1278 | oblist_find_by_name (SCHEME_P_ const char *name) |
1130 | oblist_find_by_name (SCHEME_P_ const char *name) |
1279 | { |
1131 | { |
1280 | pointer x; |
1132 | pointer x; |
1281 | char *s; |
1133 | char *s; |
1282 | |
1134 | |
… | |
… | |
1341 | mk_character (SCHEME_P_ int c) |
1193 | mk_character (SCHEME_P_ int c) |
1342 | { |
1194 | { |
1343 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1195 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1344 | |
1196 | |
1345 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1197 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1346 | ivalue_unchecked (x) = c; |
1198 | ivalue_unchecked (x) = c & 0xff; |
1347 | set_num_integer (x); |
1199 | set_num_integer (x); |
1348 | return x; |
1200 | return x; |
1349 | } |
1201 | } |
1350 | |
1202 | |
1351 | /* get number atom (integer) */ |
1203 | /* get number atom (integer) */ |
… | |
… | |
1370 | set_num_real (x); |
1222 | set_num_real (x); |
1371 | return x; |
1223 | return x; |
1372 | } |
1224 | } |
1373 | |
1225 | |
1374 | static pointer |
1226 | static pointer |
1375 | mk_number (SCHEME_P_ num n) |
1227 | mk_number (SCHEME_P_ const num n) |
1376 | { |
1228 | { |
1377 | if (num_is_fixnum (n)) |
1229 | if (num_is_fixnum (n)) |
1378 | { |
|
|
1379 | return mk_integer (SCHEME_A_ num_ivalue (n)); |
1230 | return mk_integer (SCHEME_A_ num_get_ivalue (n)); |
1380 | } |
|
|
1381 | else |
1231 | else |
1382 | { |
|
|
1383 | return mk_real (SCHEME_A_ num_rvalue (n)); |
1232 | return mk_real (SCHEME_A_ num_get_rvalue (n)); |
1384 | } |
|
|
1385 | } |
1233 | } |
1386 | |
1234 | |
1387 | /* allocate name to string area */ |
1235 | /* allocate name to string area */ |
1388 | static char * |
1236 | static char * |
1389 | store_string (SCHEME_P_ int len_str, const char *str, char fill) |
1237 | store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) |
1390 | { |
1238 | { |
1391 | char *q; |
|
|
1392 | |
|
|
1393 | q = (char *)malloc (len_str + 1); |
1239 | char *q = malloc (len_str + 1); |
1394 | |
1240 | |
1395 | if (q == 0) |
1241 | if (q == 0 && USE_ERROR_CHECKING) |
1396 | { |
1242 | { |
1397 | #if USE_ERROR_CHECKING |
|
|
1398 | SCHEME_V->no_memory = 1; |
1243 | SCHEME_V->no_memory = 1; |
1399 | return SCHEME_V->strbuff; |
1244 | return SCHEME_V->strbuff; |
1400 | #endif |
|
|
1401 | } |
1245 | } |
1402 | |
1246 | |
1403 | if (str) |
1247 | if (str) |
1404 | { |
1248 | { |
1405 | int l = strlen (str); |
1249 | int l = strlen (str); |
1406 | |
1250 | |
1407 | if (l > len_str) |
1251 | if (l > len_str) |
1408 | l = len_str; |
1252 | l = len_str; |
1409 | |
1253 | |
1410 | memcpy (q, str, l + 1); |
1254 | memcpy (q, str, l); |
1411 | q[l + 1] = 0; |
1255 | q[l] = 0; |
1412 | } |
1256 | } |
1413 | else |
1257 | else |
1414 | { |
1258 | { |
1415 | memset (q, fill, len_str); |
1259 | memset (q, fill, len_str); |
1416 | q[len_str] = 0; |
1260 | q[len_str] = 0; |
1417 | } |
1261 | } |
1418 | |
1262 | |
1419 | return q; |
1263 | return q; |
1420 | } |
1264 | } |
1421 | |
1265 | |
1422 | /* get new string */ |
|
|
1423 | INTERFACE pointer |
1266 | INTERFACE pointer |
1424 | mk_string (SCHEME_P_ const char *str) |
1267 | mk_empty_string (SCHEME_P_ uint32_t len, char fill) |
1425 | { |
1268 | { |
1426 | return mk_counted_string (SCHEME_A_ str, strlen (str)); |
1269 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
|
|
1270 | |
|
|
1271 | set_typeflag (x, T_STRING | T_ATOM); |
|
|
1272 | strvalue (x) = store_string (SCHEME_A_ len, 0, fill); |
|
|
1273 | strlength (x) = len; |
|
|
1274 | return x; |
1427 | } |
1275 | } |
1428 | |
1276 | |
1429 | INTERFACE pointer |
1277 | INTERFACE pointer |
1430 | mk_counted_string (SCHEME_P_ const char *str, int len) |
1278 | mk_counted_string (SCHEME_P_ const char *str, uint32_t len) |
1431 | { |
1279 | { |
1432 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1280 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1433 | |
1281 | |
1434 | set_typeflag (x, T_STRING | T_ATOM); |
1282 | set_typeflag (x, T_STRING | T_ATOM); |
1435 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1283 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1436 | strlength (x) = len; |
1284 | strlength (x) = len; |
1437 | return x; |
1285 | return x; |
1438 | } |
1286 | } |
1439 | |
1287 | |
1440 | INTERFACE pointer |
1288 | INTERFACE pointer |
1441 | mk_empty_string (SCHEME_P_ int len, char fill) |
1289 | mk_string (SCHEME_P_ const char *str) |
1442 | { |
1290 | { |
1443 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1291 | return mk_counted_string (SCHEME_A_ str, strlen (str)); |
1444 | |
|
|
1445 | set_typeflag (x, T_STRING | T_ATOM); |
|
|
1446 | strvalue (x) = store_string (SCHEME_A_ len, 0, fill); |
|
|
1447 | strlength (x) = len; |
|
|
1448 | return x; |
|
|
1449 | } |
1292 | } |
1450 | |
1293 | |
1451 | INTERFACE pointer |
1294 | INTERFACE pointer |
1452 | mk_vector (SCHEME_P_ int len) |
1295 | mk_vector (SCHEME_P_ uint32_t len) |
1453 | { |
1296 | { |
1454 | return get_vector_object (SCHEME_A_ len, NIL); |
1297 | return get_vector_object (SCHEME_A_ len, NIL); |
1455 | } |
1298 | } |
1456 | |
1299 | |
1457 | INTERFACE void |
1300 | INTERFACE void |
1458 | fill_vector (pointer vec, pointer obj) |
1301 | fill_vector (pointer vec, pointer obj) |
1459 | { |
1302 | { |
1460 | int i; |
1303 | int i; |
1461 | int num = ivalue (vec) / 2 + ivalue (vec) % 2; |
|
|
1462 | |
1304 | |
1463 | for (i = 0; i < num; i++) |
1305 | for (i = 0; i < vec->object.vector.length; i++) |
1464 | { |
1306 | vecvalue (vec)[i] = obj; |
1465 | set_typeflag (vec + 1 + i, T_PAIR); |
|
|
1466 | setimmutable (vec + 1 + i); |
|
|
1467 | set_car (vec + 1 + i, obj); |
|
|
1468 | set_cdr (vec + 1 + i, obj); |
|
|
1469 | } |
|
|
1470 | } |
1307 | } |
1471 | |
1308 | |
1472 | INTERFACE pointer |
1309 | INTERFACE pointer |
1473 | vector_elem (pointer vec, int ielem) |
1310 | vector_elem (pointer vec, uint32_t ielem) |
1474 | { |
1311 | { |
1475 | int n = ielem / 2; |
1312 | return vecvalue(vec)[ielem]; |
1476 | |
|
|
1477 | if (ielem % 2 == 0) |
|
|
1478 | return car (vec + 1 + n); |
|
|
1479 | else |
|
|
1480 | return cdr (vec + 1 + n); |
|
|
1481 | } |
1313 | } |
1482 | |
1314 | |
1483 | INTERFACE void |
1315 | INTERFACE void |
1484 | set_vector_elem (pointer vec, int ielem, pointer a) |
1316 | set_vector_elem (pointer vec, uint32_t ielem, pointer a) |
1485 | { |
1317 | { |
1486 | int n = ielem / 2; |
1318 | vecvalue(vec)[ielem] = a; |
1487 | |
|
|
1488 | if (ielem % 2 == 0) |
|
|
1489 | set_car (vec + 1 + n, a); |
|
|
1490 | else |
|
|
1491 | set_cdr (vec + 1 + n, a); |
|
|
1492 | } |
1319 | } |
1493 | |
1320 | |
1494 | /* get new symbol */ |
1321 | /* get new symbol */ |
1495 | INTERFACE pointer |
1322 | INTERFACE pointer |
1496 | mk_symbol (SCHEME_P_ const char *name) |
1323 | mk_symbol (SCHEME_P_ const char *name) |
1497 | { |
1324 | { |
1498 | pointer x; |
|
|
1499 | |
|
|
1500 | /* first check oblist */ |
1325 | /* first check oblist */ |
1501 | x = oblist_find_by_name (SCHEME_A_ name); |
1326 | pointer x = oblist_find_by_name (SCHEME_A_ name); |
1502 | |
1327 | |
1503 | if (x != NIL) |
1328 | if (x == NIL) |
1504 | return x; |
|
|
1505 | else |
|
|
1506 | { |
|
|
1507 | x = oblist_add_by_name (SCHEME_A_ name); |
1329 | x = oblist_add_by_name (SCHEME_A_ name); |
|
|
1330 | |
1508 | return x; |
1331 | return x; |
1509 | } |
|
|
1510 | } |
1332 | } |
1511 | |
1333 | |
1512 | INTERFACE pointer |
1334 | INTERFACE pointer |
1513 | gensym (SCHEME_P) |
1335 | gensym (SCHEME_P) |
1514 | { |
1336 | { |
1515 | pointer x; |
1337 | pointer x; |
1516 | char name[40]; |
|
|
1517 | |
1338 | |
1518 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
1339 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
1519 | { |
1340 | { |
1520 | strcpy (name, "gensym-"); |
1341 | char name[40] = "gensym-"; |
1521 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1342 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1522 | |
1343 | |
1523 | /* first check oblist */ |
1344 | /* first check oblist */ |
1524 | x = oblist_find_by_name (SCHEME_A_ name); |
1345 | x = oblist_find_by_name (SCHEME_A_ name); |
1525 | |
1346 | |
1526 | if (x != NIL) |
1347 | if (x == NIL) |
1527 | continue; |
|
|
1528 | else |
|
|
1529 | { |
1348 | { |
1530 | x = oblist_add_by_name (SCHEME_A_ name); |
1349 | x = oblist_add_by_name (SCHEME_A_ name); |
1531 | return x; |
1350 | return x; |
1532 | } |
1351 | } |
1533 | } |
1352 | } |
… | |
… | |
1542 | char c, *p; |
1361 | char c, *p; |
1543 | int has_dec_point = 0; |
1362 | int has_dec_point = 0; |
1544 | int has_fp_exp = 0; |
1363 | int has_fp_exp = 0; |
1545 | |
1364 | |
1546 | #if USE_COLON_HOOK |
1365 | #if USE_COLON_HOOK |
1547 | |
|
|
1548 | if ((p = strstr (q, "::")) != 0) |
1366 | if ((p = strstr (q, "::")) != 0) |
1549 | { |
1367 | { |
1550 | *p = 0; |
1368 | *p = 0; |
1551 | return cons (SCHEME_V->COLON_HOOK, |
1369 | return cons (SCHEME_V->COLON_HOOK, |
1552 | cons (cons (SCHEME_V->QUOTE, |
1370 | cons (cons (SCHEME_V->QUOTE, |
1553 | cons (mk_atom (SCHEME_A_ p + 2), NIL)), cons (mk_symbol (SCHEME_A_ strlwr (q)), NIL))); |
1371 | cons (mk_atom (SCHEME_A_ p + 2), NIL)), cons (mk_symbol (SCHEME_A_ strlwr (q)), NIL))); |
1554 | } |
1372 | } |
1555 | |
|
|
1556 | #endif |
1373 | #endif |
1557 | |
1374 | |
1558 | p = q; |
1375 | p = q; |
1559 | c = *p++; |
1376 | c = *p++; |
1560 | |
1377 | |
… | |
… | |
1567 | has_dec_point = 1; |
1384 | has_dec_point = 1; |
1568 | c = *p++; |
1385 | c = *p++; |
1569 | } |
1386 | } |
1570 | |
1387 | |
1571 | if (!isdigit (c)) |
1388 | if (!isdigit (c)) |
1572 | { |
|
|
1573 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1389 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1574 | } |
|
|
1575 | } |
1390 | } |
1576 | else if (c == '.') |
1391 | else if (c == '.') |
1577 | { |
1392 | { |
1578 | has_dec_point = 1; |
1393 | has_dec_point = 1; |
1579 | c = *p++; |
1394 | c = *p++; |
1580 | |
1395 | |
1581 | if (!isdigit (c)) |
1396 | if (!isdigit (c)) |
1582 | { |
|
|
1583 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1397 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1584 | } |
|
|
1585 | } |
1398 | } |
1586 | else if (!isdigit (c)) |
1399 | else if (!isdigit (c)) |
1587 | { |
|
|
1588 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1400 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1589 | } |
|
|
1590 | |
1401 | |
1591 | for (; (c = *p) != 0; ++p) |
1402 | for (; (c = *p) != 0; ++p) |
1592 | { |
1403 | { |
1593 | if (!isdigit (c)) |
1404 | if (!isdigit (c)) |
1594 | { |
1405 | { |
… | |
… | |
1607 | has_dec_point = 1; /* decimal point illegal |
1418 | has_dec_point = 1; /* decimal point illegal |
1608 | from now on */ |
1419 | from now on */ |
1609 | p++; |
1420 | p++; |
1610 | |
1421 | |
1611 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1422 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1612 | { |
|
|
1613 | continue; |
1423 | continue; |
1614 | } |
|
|
1615 | } |
1424 | } |
1616 | } |
1425 | } |
1617 | |
1426 | |
1618 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1427 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1619 | } |
1428 | } |
1620 | } |
1429 | } |
1621 | |
1430 | |
1622 | #if USE_FLOAT |
1431 | #if USE_REAL |
1623 | if (has_dec_point) |
1432 | if (has_dec_point) |
1624 | return mk_real (SCHEME_A_ atof (q)); |
1433 | return mk_real (SCHEME_A_ atof (q)); |
1625 | #endif |
1434 | #endif |
1626 | |
1435 | |
1627 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1436 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
… | |
… | |
1629 | |
1438 | |
1630 | /* make constant */ |
1439 | /* make constant */ |
1631 | static pointer |
1440 | static pointer |
1632 | mk_sharp_const (SCHEME_P_ char *name) |
1441 | mk_sharp_const (SCHEME_P_ char *name) |
1633 | { |
1442 | { |
1634 | long x; |
|
|
1635 | char tmp[STRBUFFSIZE]; |
|
|
1636 | |
|
|
1637 | if (!strcmp (name, "t")) |
1443 | if (!strcmp (name, "t")) |
1638 | return S_T; |
1444 | return S_T; |
1639 | else if (!strcmp (name, "f")) |
1445 | else if (!strcmp (name, "f")) |
1640 | return S_F; |
1446 | return S_F; |
1641 | else if (*name == 'o') /* #o (octal) */ |
|
|
1642 | { |
|
|
1643 | x = strtol (name + 1, 0, 8); |
|
|
1644 | return mk_integer (SCHEME_A_ x); |
|
|
1645 | } |
|
|
1646 | else if (*name == 'd') /* #d (decimal) */ |
|
|
1647 | { |
|
|
1648 | x = strtol (name + 1, 0, 10); |
|
|
1649 | return mk_integer (SCHEME_A_ x); |
|
|
1650 | } |
|
|
1651 | else if (*name == 'x') /* #x (hex) */ |
|
|
1652 | { |
|
|
1653 | x = strtol (name + 1, 0, 16); |
|
|
1654 | return mk_integer (SCHEME_A_ x); |
|
|
1655 | } |
|
|
1656 | else if (*name == 'b') /* #b (binary) */ |
|
|
1657 | { |
|
|
1658 | x = binary_decode (name + 1); |
|
|
1659 | return mk_integer (SCHEME_A_ x); |
|
|
1660 | } |
|
|
1661 | else if (*name == '\\') /* #\w (character) */ |
1447 | else if (*name == '\\') /* #\w (character) */ |
1662 | { |
1448 | { |
1663 | int c = 0; |
1449 | int c; |
1664 | |
1450 | |
1665 | if (stricmp (name + 1, "space") == 0) |
1451 | if (stricmp (name + 1, "space") == 0) |
1666 | c = ' '; |
1452 | c = ' '; |
1667 | else if (stricmp (name + 1, "newline") == 0) |
1453 | else if (stricmp (name + 1, "newline") == 0) |
1668 | c = '\n'; |
1454 | c = '\n'; |
… | |
… | |
1670 | c = '\r'; |
1456 | c = '\r'; |
1671 | else if (stricmp (name + 1, "tab") == 0) |
1457 | else if (stricmp (name + 1, "tab") == 0) |
1672 | c = '\t'; |
1458 | c = '\t'; |
1673 | else if (name[1] == 'x' && name[2] != 0) |
1459 | else if (name[1] == 'x' && name[2] != 0) |
1674 | { |
1460 | { |
1675 | int c1 = strtol (name + 2, 0, 16); |
1461 | long c1 = strtol (name + 2, 0, 16); |
1676 | |
1462 | |
1677 | if (c1 <= UCHAR_MAX) |
1463 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1678 | c = c1; |
1464 | c = c1; |
1679 | else |
1465 | else |
1680 | return NIL; |
1466 | return NIL; |
1681 | |
1467 | } |
1682 | #if USE_ASCII_NAMES |
1468 | #if USE_ASCII_NAMES |
1683 | } |
|
|
1684 | else if (is_ascii_name (name + 1, &c)) |
1469 | else if (is_ascii_name (name + 1, &c)) |
1685 | { |
|
|
1686 | /* nothing */ |
1470 | /* nothing */; |
1687 | #endif |
1471 | #endif |
1688 | } |
|
|
1689 | else if (name[2] == 0) |
1472 | else if (name[2] == 0) |
1690 | c = name[1]; |
1473 | c = name[1]; |
1691 | else |
1474 | else |
1692 | return NIL; |
1475 | return NIL; |
1693 | |
1476 | |
1694 | return mk_character (SCHEME_A_ c); |
1477 | return mk_character (SCHEME_A_ c); |
1695 | } |
1478 | } |
1696 | else |
1479 | else |
|
|
1480 | { |
|
|
1481 | /* identify base by string index */ |
|
|
1482 | const char baseidx[17] = "ffbf" "ffff" "ofdf" "ffff" "x"; |
|
|
1483 | char *base = strchr (baseidx, *name); |
|
|
1484 | |
|
|
1485 | if (base) |
|
|
1486 | return mk_integer (SCHEME_A_ strtol (name + 1, 0, base - baseidx)); |
|
|
1487 | |
1697 | return NIL; |
1488 | return NIL; |
|
|
1489 | } |
1698 | } |
1490 | } |
1699 | |
1491 | |
1700 | /* ========== garbage collector ========== */ |
1492 | /* ========== garbage collector ========== */ |
1701 | |
1493 | |
1702 | /*-- |
1494 | /*-- |
1703 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1495 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1704 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1496 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1705 | * for marking. |
1497 | * for marking. |
|
|
1498 | * |
|
|
1499 | * The exception is vectors - vectors are currently marked recursively, |
|
|
1500 | * which is inherited form tinyscheme and could be fixed by having another |
|
|
1501 | * word of context in the vector |
1706 | */ |
1502 | */ |
1707 | static void |
1503 | static void |
1708 | mark (pointer a) |
1504 | mark (pointer a) |
1709 | { |
1505 | { |
1710 | pointer t, q, p; |
1506 | pointer t, q, p; |
1711 | |
1507 | |
1712 | t = (pointer) 0; |
1508 | t = 0; |
1713 | p = a; |
1509 | p = a; |
1714 | E2: |
1510 | E2: |
1715 | setmark (p); |
1511 | setmark (p); |
1716 | printf ("mark %p %x\n", p, p->flag);//D |
|
|
1717 | |
1512 | |
1718 | if (is_vector (p)) |
1513 | if (ecb_expect_false (is_vector (p))) |
1719 | { |
1514 | { |
1720 | int i; |
1515 | int i; |
1721 | int num = ivalue_unchecked (p) / 2 + ivalue_unchecked (p) % 2; |
|
|
1722 | |
1516 | |
1723 | for (i = 0; i < num; i++) |
1517 | for (i = 0; i < p->object.vector.length; i++) |
1724 | { |
1518 | mark (vecvalue (p)[i]); |
1725 | /* Vector cells will be treated like ordinary cells */ |
|
|
1726 | mark (p + 1 + i); |
|
|
1727 | } |
|
|
1728 | } |
1519 | } |
1729 | |
1520 | |
1730 | if (is_atom (p)) |
1521 | if (is_atom (p)) |
1731 | goto E6; |
1522 | goto E6; |
1732 | |
1523 | |
… | |
… | |
1742 | goto E2; |
1533 | goto E2; |
1743 | } |
1534 | } |
1744 | |
1535 | |
1745 | E5: |
1536 | E5: |
1746 | q = cdr (p); /* down cdr */ |
1537 | q = cdr (p); /* down cdr */ |
1747 | printf ("mark+ %p\n", q, q->flag);//D |
|
|
1748 | |
1538 | |
1749 | if (q && !is_mark (q)) |
1539 | if (q && !is_mark (q)) |
1750 | { |
1540 | { |
1751 | set_cdr (p, t); |
1541 | set_cdr (p, t); |
1752 | t = p; |
1542 | t = p; |
1753 | p = q; |
1543 | p = q; |
1754 | goto E2; |
1544 | goto E2; |
1755 | } |
1545 | } |
1756 | |
1546 | |
1757 | E6: /* up. Undo the link switching from steps E4 and E5. */ |
1547 | E6: /* up. Undo the link switching from steps E4 and E5. */ |
1758 | |
|
|
1759 | if (!t) |
1548 | if (!t) |
1760 | return; |
1549 | return; |
1761 | |
1550 | |
1762 | q = t; |
1551 | q = t; |
1763 | |
1552 | |
… | |
… | |
1831 | if (is_mark (p)) |
1620 | if (is_mark (p)) |
1832 | clrmark (p); |
1621 | clrmark (p); |
1833 | else |
1622 | else |
1834 | { |
1623 | { |
1835 | /* reclaim cell */ |
1624 | /* reclaim cell */ |
1836 | if (typeflag (p) != 0) |
1625 | if (typeflag (p) != T_FREE) |
1837 | { |
1626 | { |
1838 | finalize_cell (SCHEME_A_ p); |
1627 | finalize_cell (SCHEME_A_ p); |
1839 | set_typeflag (p, 0); |
1628 | set_typeflag (p, T_FREE); |
1840 | set_car (p, NIL); |
1629 | set_car (p, NIL); |
1841 | } |
1630 | } |
1842 | |
1631 | |
1843 | ++SCHEME_V->fcells; |
1632 | ++SCHEME_V->fcells; |
1844 | set_cdr (p, SCHEME_V->free_cell); |
1633 | set_cdr (p, SCHEME_V->free_cell); |
… | |
… | |
1852 | } |
1641 | } |
1853 | |
1642 | |
1854 | static void |
1643 | static void |
1855 | finalize_cell (SCHEME_P_ pointer a) |
1644 | finalize_cell (SCHEME_P_ pointer a) |
1856 | { |
1645 | { |
|
|
1646 | /* TODO, fast bitmap check? */ |
1857 | if (is_string (a)) |
1647 | if (is_string (a)) |
1858 | free (strvalue (a)); |
1648 | free (strvalue (a)); |
|
|
1649 | else if (is_vector (a)) |
|
|
1650 | free (vecvalue (a)); |
1859 | #if USE_PORTS |
1651 | #if USE_PORTS |
1860 | else if (is_port (a)) |
1652 | else if (is_port (a)) |
1861 | { |
1653 | { |
1862 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
1654 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
1863 | port_close (SCHEME_A_ a, port_input | port_output); |
1655 | port_close (SCHEME_A_ a, port_input | port_output); |
… | |
… | |
2289 | char *p = SCHEME_V->strbuff; |
2081 | char *p = SCHEME_V->strbuff; |
2290 | |
2082 | |
2291 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2083 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2292 | |
2084 | |
2293 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2085 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2294 | { |
|
|
2295 | *p = 0; |
2086 | *p = 0; |
2296 | } |
|
|
2297 | else |
2087 | else |
2298 | { |
2088 | { |
2299 | backchar (SCHEME_A_ p[-1]); |
2089 | backchar (SCHEME_A_ p[-1]); |
2300 | *--p = '\0'; |
2090 | *--p = '\0'; |
2301 | } |
2091 | } |
… | |
… | |
2316 | for (;;) |
2106 | for (;;) |
2317 | { |
2107 | { |
2318 | c = inchar (SCHEME_A); |
2108 | c = inchar (SCHEME_A); |
2319 | |
2109 | |
2320 | if (c == EOF || p - SCHEME_V->strbuff > sizeof (SCHEME_V->strbuff) - 1) |
2110 | if (c == EOF || p - SCHEME_V->strbuff > sizeof (SCHEME_V->strbuff) - 1) |
2321 | { |
|
|
2322 | return S_F; |
2111 | return S_F; |
2323 | } |
|
|
2324 | |
2112 | |
2325 | switch (state) |
2113 | switch (state) |
2326 | { |
2114 | { |
2327 | case st_ok: |
2115 | case st_ok: |
2328 | switch (c) |
2116 | switch (c) |
… | |
… | |
2396 | c = toupper (c); |
2184 | c = toupper (c); |
2397 | |
2185 | |
2398 | if (c >= '0' && c <= 'F') |
2186 | if (c >= '0' && c <= 'F') |
2399 | { |
2187 | { |
2400 | if (c <= '9') |
2188 | if (c <= '9') |
2401 | { |
|
|
2402 | c1 = (c1 << 4) + c - '0'; |
2189 | c1 = (c1 << 4) + c - '0'; |
2403 | } |
|
|
2404 | else |
2190 | else |
2405 | { |
|
|
2406 | c1 = (c1 << 4) + c - 'A' + 10; |
2191 | c1 = (c1 << 4) + c - 'A' + 10; |
2407 | } |
|
|
2408 | |
2192 | |
2409 | if (state == st_x1) |
2193 | if (state == st_x1) |
2410 | { |
|
|
2411 | state = st_x2; |
2194 | state = st_x2; |
2412 | } |
|
|
2413 | else |
2195 | else |
2414 | { |
2196 | { |
2415 | *p++ = c1; |
2197 | *p++ = c1; |
2416 | state = st_ok; |
2198 | state = st_ok; |
2417 | } |
2199 | } |
2418 | } |
2200 | } |
2419 | else |
2201 | else |
2420 | { |
|
|
2421 | return S_F; |
2202 | return S_F; |
2422 | } |
|
|
2423 | |
2203 | |
2424 | break; |
2204 | break; |
2425 | |
2205 | |
2426 | case st_oct1: |
2206 | case st_oct1: |
2427 | case st_oct2: |
2207 | case st_oct2: |
… | |
… | |
2452 | } |
2232 | } |
2453 | } |
2233 | } |
2454 | } |
2234 | } |
2455 | |
2235 | |
2456 | /* check c is in chars */ |
2236 | /* check c is in chars */ |
2457 | static INLINE int |
2237 | ecb_inline int |
2458 | is_one_of (char *s, int c) |
2238 | is_one_of (char *s, int c) |
2459 | { |
2239 | { |
2460 | if (c == EOF) |
2240 | if (c == EOF) |
2461 | return 1; |
2241 | return 1; |
2462 | |
2242 | |
2463 | while (*s) |
2243 | return !!strchr (s, c); |
2464 | if (*s++ == c) |
|
|
2465 | return 1; |
|
|
2466 | |
|
|
2467 | return 0; |
|
|
2468 | } |
2244 | } |
2469 | |
2245 | |
2470 | /* skip white characters */ |
2246 | /* skip white characters */ |
2471 | static INLINE int |
2247 | ecb_inline int |
2472 | skipspace (SCHEME_P) |
2248 | skipspace (SCHEME_P) |
2473 | { |
2249 | { |
2474 | int c, curr_line = 0; |
2250 | int c, curr_line = 0; |
2475 | |
2251 | |
2476 | do |
2252 | do |
2477 | { |
2253 | { |
2478 | c = inchar (SCHEME_A); |
2254 | c = inchar (SCHEME_A); |
2479 | #if SHOW_ERROR_LINE |
2255 | #if SHOW_ERROR_LINE |
2480 | if (c == '\n') |
2256 | if (c == '\n') |
2481 | curr_line++; |
2257 | curr_line++; |
2482 | |
|
|
2483 | #endif |
2258 | #endif |
2484 | } |
2259 | } |
2485 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2260 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2486 | |
2261 | |
2487 | /* record it */ |
2262 | /* record it */ |
… | |
… | |
2524 | |
2299 | |
2525 | if (is_one_of (" \n\t", c)) |
2300 | if (is_one_of (" \n\t", c)) |
2526 | return TOK_DOT; |
2301 | return TOK_DOT; |
2527 | else |
2302 | else |
2528 | { |
2303 | { |
|
|
2304 | //TODO: ungetc twice in a row is not supported in C |
2529 | backchar (SCHEME_A_ c); |
2305 | backchar (SCHEME_A_ c); |
2530 | backchar (SCHEME_A_ '.'); |
2306 | backchar (SCHEME_A_ '.'); |
2531 | return TOK_ATOM; |
2307 | return TOK_ATOM; |
2532 | } |
2308 | } |
2533 | |
2309 | |
… | |
… | |
2647 | int d = *s / 16; |
2423 | int d = *s / 16; |
2648 | |
2424 | |
2649 | putcharacter (SCHEME_A_ 'x'); |
2425 | putcharacter (SCHEME_A_ 'x'); |
2650 | |
2426 | |
2651 | if (d < 10) |
2427 | if (d < 10) |
2652 | { |
|
|
2653 | putcharacter (SCHEME_A_ d + '0'); |
2428 | putcharacter (SCHEME_A_ d + '0'); |
2654 | } |
|
|
2655 | else |
2429 | else |
2656 | { |
|
|
2657 | putcharacter (SCHEME_A_ d - 10 + 'A'); |
2430 | putcharacter (SCHEME_A_ d - 10 + 'A'); |
2658 | } |
|
|
2659 | |
2431 | |
2660 | d = *s % 16; |
2432 | d = *s % 16; |
2661 | |
2433 | |
2662 | if (d < 10) |
2434 | if (d < 10) |
2663 | { |
|
|
2664 | putcharacter (SCHEME_A_ d + '0'); |
2435 | putcharacter (SCHEME_A_ d + '0'); |
2665 | } |
|
|
2666 | else |
2436 | else |
2667 | { |
|
|
2668 | putcharacter (SCHEME_A_ d - 10 + 'A'); |
2437 | putcharacter (SCHEME_A_ d - 10 + 'A'); |
2669 | } |
|
|
2670 | } |
2438 | } |
2671 | } |
2439 | } |
2672 | } |
2440 | } |
2673 | else |
2441 | else |
2674 | { |
|
|
2675 | putcharacter (SCHEME_A_ * s); |
2442 | putcharacter (SCHEME_A_ * s); |
2676 | } |
|
|
2677 | |
2443 | |
2678 | s++; |
2444 | s++; |
2679 | } |
2445 | } |
2680 | |
2446 | |
2681 | putcharacter (SCHEME_A_ '"'); |
2447 | putcharacter (SCHEME_A_ '"'); |
… | |
… | |
2716 | |
2482 | |
2717 | if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ |
2483 | if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ |
2718 | { |
2484 | { |
2719 | if (num_is_integer (l)) |
2485 | if (num_is_integer (l)) |
2720 | xnum (p, ivalue_unchecked (l)); |
2486 | xnum (p, ivalue_unchecked (l)); |
2721 | #if USE_FLOAT |
2487 | #if USE_REAL |
2722 | else |
2488 | else |
2723 | { |
2489 | { |
2724 | snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); |
2490 | snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); |
2725 | /* r5rs says there must be a '.' (unless 'e'?) */ |
2491 | /* r5rs says there must be a '.' (unless 'e'?) */ |
2726 | f = strcspn (p, ".e"); |
2492 | f = strcspn (p, ".e"); |
… | |
… | |
2898 | list_star (SCHEME_P_ pointer d) |
2664 | list_star (SCHEME_P_ pointer d) |
2899 | { |
2665 | { |
2900 | pointer p, q; |
2666 | pointer p, q; |
2901 | |
2667 | |
2902 | if (cdr (d) == NIL) |
2668 | if (cdr (d) == NIL) |
2903 | { |
|
|
2904 | return car (d); |
2669 | return car (d); |
2905 | } |
|
|
2906 | |
2670 | |
2907 | p = cons (car (d), cdr (d)); |
2671 | p = cons (car (d), cdr (d)); |
2908 | q = p; |
2672 | q = p; |
2909 | |
2673 | |
2910 | while (cdr (cdr (p)) != NIL) |
2674 | while (cdr (cdr (p)) != NIL) |
2911 | { |
2675 | { |
2912 | d = cons (car (p), cdr (p)); |
2676 | d = cons (car (p), cdr (p)); |
2913 | |
2677 | |
2914 | if (cdr (cdr (p)) != NIL) |
2678 | if (cdr (cdr (p)) != NIL) |
2915 | { |
|
|
2916 | p = cdr (d); |
2679 | p = cdr (d); |
2917 | } |
|
|
2918 | } |
2680 | } |
2919 | |
2681 | |
2920 | set_cdr (p, car (cdr (p))); |
2682 | set_cdr (p, car (cdr (p))); |
2921 | return q; |
2683 | return q; |
2922 | } |
2684 | } |
… | |
… | |
2936 | |
2698 | |
2937 | /* reverse list --- in-place */ |
2699 | /* reverse list --- in-place */ |
2938 | static pointer |
2700 | static pointer |
2939 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2701 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2940 | { |
2702 | { |
2941 | pointer p = list, result = term, q; |
2703 | pointer result = term; |
|
|
2704 | pointer p = list; |
2942 | |
2705 | |
2943 | while (p != NIL) |
2706 | while (p != NIL) |
2944 | { |
2707 | { |
2945 | q = cdr (p); |
2708 | pointer q = cdr (p); |
2946 | set_cdr (p, result); |
2709 | set_cdr (p, result); |
2947 | result = p; |
2710 | result = p; |
2948 | p = q; |
2711 | p = q; |
2949 | } |
2712 | } |
2950 | |
2713 | |
… | |
… | |
2983 | } |
2746 | } |
2984 | else if (is_number (a)) |
2747 | else if (is_number (a)) |
2985 | { |
2748 | { |
2986 | if (is_number (b)) |
2749 | if (is_number (b)) |
2987 | if (num_is_integer (a) == num_is_integer (b)) |
2750 | if (num_is_integer (a) == num_is_integer (b)) |
2988 | return num_eq (nvalue (a), nvalue (b)); |
2751 | return num_cmp (nvalue (a), nvalue (b)) == 0; |
2989 | |
2752 | |
2990 | return 0; |
2753 | return 0; |
2991 | } |
2754 | } |
2992 | else if (is_character (a)) |
2755 | else if (is_character (a)) |
2993 | { |
2756 | { |
… | |
… | |
3025 | #if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) |
2788 | #if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) |
3026 | |
2789 | |
3027 | static int |
2790 | static int |
3028 | hash_fn (const char *key, int table_size) |
2791 | hash_fn (const char *key, int table_size) |
3029 | { |
2792 | { |
3030 | unsigned int hashed = 0; |
2793 | const unsigned char *p = key; |
3031 | const char *c; |
2794 | uint32_t hash = 2166136261; |
3032 | int bits_per_int = sizeof (unsigned int) * 8; |
|
|
3033 | |
2795 | |
3034 | for (c = key; *c; c++) |
2796 | while (*p) |
3035 | { |
2797 | hash = (hash ^ *p++) * 16777619; |
3036 | /* letters have about 5 bits in them */ |
|
|
3037 | hashed = (hashed << 5) | (hashed >> (bits_per_int - 5)); |
|
|
3038 | hashed ^= *c; |
|
|
3039 | } |
|
|
3040 | |
2798 | |
3041 | return hashed % table_size; |
2799 | return hash % table_size; |
3042 | } |
2800 | } |
3043 | #endif |
2801 | #endif |
3044 | |
2802 | |
3045 | #ifndef USE_ALIST_ENV |
2803 | #ifndef USE_ALIST_ENV |
3046 | |
2804 | |
… | |
… | |
3065 | |
2823 | |
3066 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2824 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
3067 | setenvironment (SCHEME_V->envir); |
2825 | setenvironment (SCHEME_V->envir); |
3068 | } |
2826 | } |
3069 | |
2827 | |
3070 | static INLINE void |
2828 | ecb_inline void |
3071 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2829 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
3072 | { |
2830 | { |
3073 | pointer slot = immutable_cons (variable, value); |
2831 | pointer slot = immutable_cons (variable, value); |
3074 | |
2832 | |
3075 | if (is_vector (car (env))) |
2833 | if (is_vector (car (env))) |
3076 | { |
2834 | { |
3077 | int location = hash_fn (symname (variable), ivalue_unchecked (car (env))); |
2835 | int location = hash_fn (symname (variable), veclength (car (env))); |
3078 | |
2836 | |
3079 | set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); |
2837 | set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); |
3080 | } |
2838 | } |
3081 | else |
2839 | else |
3082 | set_car (env, immutable_cons (slot, car (env))); |
2840 | set_car (env, immutable_cons (slot, car (env))); |
… | |
… | |
3090 | |
2848 | |
3091 | for (x = env; x != NIL; x = cdr (x)) |
2849 | for (x = env; x != NIL; x = cdr (x)) |
3092 | { |
2850 | { |
3093 | if (is_vector (car (x))) |
2851 | if (is_vector (car (x))) |
3094 | { |
2852 | { |
3095 | location = hash_fn (symname (hdl), ivalue_unchecked (car (x))); |
2853 | location = hash_fn (symname (hdl), veclength (car (x))); |
3096 | y = vector_elem (car (x), location); |
2854 | y = vector_elem (car (x), location); |
3097 | } |
2855 | } |
3098 | else |
2856 | else |
3099 | y = car (x); |
2857 | y = car (x); |
3100 | |
2858 | |
… | |
… | |
3115 | return NIL; |
2873 | return NIL; |
3116 | } |
2874 | } |
3117 | |
2875 | |
3118 | #else /* USE_ALIST_ENV */ |
2876 | #else /* USE_ALIST_ENV */ |
3119 | |
2877 | |
3120 | static INLINE void |
2878 | ecb_inline void |
3121 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2879 | new_frame_in_env (SCHEME_P_ pointer old_env) |
3122 | { |
2880 | { |
3123 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2881 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
3124 | setenvironment (SCHEME_V->envir); |
2882 | setenvironment (SCHEME_V->envir); |
3125 | } |
2883 | } |
3126 | |
2884 | |
3127 | static INLINE void |
2885 | ecb_inline void |
3128 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2886 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
3129 | { |
2887 | { |
3130 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2888 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
3131 | } |
2889 | } |
3132 | |
2890 | |
… | |
… | |
3154 | return NIL; |
2912 | return NIL; |
3155 | } |
2913 | } |
3156 | |
2914 | |
3157 | #endif /* USE_ALIST_ENV else */ |
2915 | #endif /* USE_ALIST_ENV else */ |
3158 | |
2916 | |
3159 | static INLINE void |
2917 | ecb_inline void |
3160 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2918 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
3161 | { |
2919 | { |
3162 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2920 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
3163 | } |
2921 | } |
3164 | |
2922 | |
3165 | static INLINE void |
2923 | ecb_inline void |
3166 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2924 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
3167 | { |
2925 | { |
3168 | set_cdr (slot, value); |
2926 | set_cdr (slot, value); |
3169 | } |
2927 | } |
3170 | |
2928 | |
3171 | static INLINE pointer |
2929 | ecb_inline pointer |
3172 | slot_value_in_env (pointer slot) |
2930 | slot_value_in_env (pointer slot) |
3173 | { |
2931 | { |
3174 | return cdr (slot); |
2932 | return cdr (slot); |
3175 | } |
2933 | } |
3176 | |
2934 | |
3177 | /* ========== Evaluation Cycle ========== */ |
2935 | /* ========== Evaluation Cycle ========== */ |
3178 | |
2936 | |
3179 | static pointer |
2937 | static int |
3180 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2938 | xError_1 (SCHEME_P_ const char *s, pointer a) |
3181 | { |
2939 | { |
3182 | #if USE_ERROR_HOOK |
2940 | #if USE_ERROR_HOOK |
3183 | pointer x; |
2941 | pointer x; |
3184 | pointer hdl = SCHEME_V->ERROR_HOOK; |
2942 | pointer hdl = SCHEME_V->ERROR_HOOK; |
… | |
… | |
3210 | #if USE_ERROR_HOOK |
2968 | #if USE_ERROR_HOOK |
3211 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); |
2969 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); |
3212 | |
2970 | |
3213 | if (x != NIL) |
2971 | if (x != NIL) |
3214 | { |
2972 | { |
3215 | if (a) |
2973 | pointer code = a |
3216 | SCHEME_V->code = cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL); |
2974 | ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) |
3217 | else |
2975 | : NIL; |
3218 | SCHEME_V->code = NIL; |
|
|
3219 | |
2976 | |
3220 | SCHEME_V->code = cons (mk_string (SCHEME_A_ s), SCHEME_V->code); |
2977 | code = cons (mk_string (SCHEME_A_ s), code); |
3221 | setimmutable (car (SCHEME_V->code)); |
2978 | setimmutable (car (code)); |
3222 | SCHEME_V->code = cons (slot_value_in_env (x), SCHEME_V->code); |
2979 | SCHEME_V->code = cons (slot_value_in_env (x), code); |
3223 | SCHEME_V->op = OP_EVAL; |
2980 | SCHEME_V->op = OP_EVAL; |
3224 | |
2981 | |
3225 | return S_T; |
2982 | return 0; |
3226 | } |
2983 | } |
3227 | #endif |
2984 | #endif |
3228 | |
2985 | |
3229 | if (a) |
2986 | if (a) |
3230 | SCHEME_V->args = cons (a, NIL); |
2987 | SCHEME_V->args = cons (a, NIL); |
3231 | else |
2988 | else |
3232 | SCHEME_V->args = NIL; |
2989 | SCHEME_V->args = NIL; |
3233 | |
2990 | |
3234 | SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); |
2991 | SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); |
3235 | setimmutable (car (SCHEME_V->args)); |
2992 | setimmutable (car (SCHEME_V->args)); |
3236 | SCHEME_V->op = (int)OP_ERR0; |
2993 | SCHEME_V->op = OP_ERR0; |
|
|
2994 | |
3237 | return S_T; |
2995 | return 0; |
3238 | } |
2996 | } |
3239 | |
2997 | |
3240 | #define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) |
2998 | #define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) |
3241 | #define Error_0(s) Error_1 (s, 0) |
2999 | #define Error_0(s) Error_1 (s, 0) |
3242 | |
3000 | |
3243 | /* Too small to turn into function */ |
3001 | /* Too small to turn into function */ |
3244 | #define BEGIN do { |
3002 | #define BEGIN do { |
3245 | #define END } while (0) |
3003 | #define END } while (0) |
3246 | #define s_goto(a) BEGIN \ |
3004 | #define s_goto(a) BEGIN \ |
3247 | SCHEME_V->op = (int)(a); \ |
3005 | SCHEME_V->op = a; \ |
3248 | return S_T; END |
3006 | return 0; END |
3249 | |
3007 | |
3250 | #define s_return(a) return xs_return(SCHEME_A_ a) |
3008 | #define s_return(a) return xs_return (SCHEME_A_ a) |
3251 | |
3009 | |
3252 | #ifndef USE_SCHEME_STACK |
3010 | #ifndef USE_SCHEME_STACK |
3253 | |
3011 | |
3254 | /* this structure holds all the interpreter's registers */ |
3012 | /* this structure holds all the interpreter's registers */ |
3255 | struct dump_stack_frame |
3013 | struct dump_stack_frame |
… | |
… | |
3274 | SCHEME_V->dump_size += STACK_GROWTH; |
3032 | SCHEME_V->dump_size += STACK_GROWTH; |
3275 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3033 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3276 | } |
3034 | } |
3277 | |
3035 | |
3278 | next_frame = SCHEME_V->dump_base + nframes; |
3036 | next_frame = SCHEME_V->dump_base + nframes; |
|
|
3037 | |
3279 | next_frame->op = op; |
3038 | next_frame->op = op; |
3280 | next_frame->args = args; |
3039 | next_frame->args = args; |
3281 | next_frame->envir = SCHEME_V->envir; |
3040 | next_frame->envir = SCHEME_V->envir; |
3282 | next_frame->code = code; |
3041 | next_frame->code = code; |
|
|
3042 | |
3283 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3043 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3284 | } |
3044 | } |
3285 | |
3045 | |
3286 | static pointer |
3046 | static int |
3287 | xs_return (SCHEME_P_ pointer a) |
3047 | xs_return (SCHEME_P_ pointer a) |
3288 | { |
3048 | { |
3289 | int nframes = (uintptr_t)SCHEME_V->dump; |
3049 | int nframes = (uintptr_t)SCHEME_V->dump; |
3290 | struct dump_stack_frame *frame; |
3050 | struct dump_stack_frame *frame; |
3291 | |
3051 | |
3292 | SCHEME_V->value = a; |
3052 | SCHEME_V->value = a; |
3293 | |
3053 | |
3294 | if (nframes <= 0) |
3054 | if (nframes <= 0) |
3295 | return NIL; |
3055 | return -1; |
3296 | |
3056 | |
3297 | nframes--; |
|
|
3298 | frame = SCHEME_V->dump_base + nframes; |
3057 | frame = &SCHEME_V->dump_base[--nframes]; |
3299 | SCHEME_V->op = frame->op; |
3058 | SCHEME_V->op = frame->op; |
3300 | SCHEME_V->args = frame->args; |
3059 | SCHEME_V->args = frame->args; |
3301 | SCHEME_V->envir = frame->envir; |
3060 | SCHEME_V->envir = frame->envir; |
3302 | SCHEME_V->code = frame->code; |
3061 | SCHEME_V->code = frame->code; |
3303 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3062 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3304 | |
3063 | |
3305 | return S_T; |
3064 | return 0; |
3306 | } |
3065 | } |
3307 | |
3066 | |
3308 | static INLINE void |
3067 | ecb_inline void |
3309 | dump_stack_reset (SCHEME_P) |
3068 | dump_stack_reset (SCHEME_P) |
3310 | { |
3069 | { |
3311 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3070 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3312 | SCHEME_V->dump = (pointer)0; |
3071 | SCHEME_V->dump = (pointer)+0; |
3313 | } |
3072 | } |
3314 | |
3073 | |
3315 | static INLINE void |
3074 | ecb_inline void |
3316 | dump_stack_initialize (SCHEME_P) |
3075 | dump_stack_initialize (SCHEME_P) |
3317 | { |
3076 | { |
3318 | SCHEME_V->dump_size = 0; |
3077 | SCHEME_V->dump_size = 0; |
3319 | SCHEME_V->dump_base = NULL; |
3078 | SCHEME_V->dump_base = 0; |
3320 | dump_stack_reset (SCHEME_A); |
3079 | dump_stack_reset (SCHEME_A); |
3321 | } |
3080 | } |
3322 | |
3081 | |
3323 | static void |
3082 | static void |
3324 | dump_stack_free (SCHEME_P) |
3083 | dump_stack_free (SCHEME_P) |
3325 | { |
3084 | { |
3326 | free (SCHEME_V->dump_base); |
3085 | free (SCHEME_V->dump_base); |
3327 | SCHEME_V->dump_base = NULL; |
3086 | SCHEME_V->dump_base = 0; |
3328 | SCHEME_V->dump = (pointer)0; |
3087 | SCHEME_V->dump = (pointer)0; |
3329 | SCHEME_V->dump_size = 0; |
3088 | SCHEME_V->dump_size = 0; |
3330 | } |
3089 | } |
3331 | |
3090 | |
3332 | static void |
3091 | static void |
… | |
… | |
3374 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3133 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3375 | |
3134 | |
3376 | while (cont != NIL) |
3135 | while (cont != NIL) |
3377 | { |
3136 | { |
3378 | frame->op = ivalue (car (cont)); cont = cdr (cont); |
3137 | frame->op = ivalue (car (cont)); cont = cdr (cont); |
3379 | frame->args = car (cont) ; cont = cdr (cont); |
3138 | frame->args = car (cont) ; cont = cdr (cont); |
3380 | frame->envir = car (cont) ; cont = cdr (cont); |
3139 | frame->envir = car (cont) ; cont = cdr (cont); |
3381 | frame->code = car (cont) ; cont = cdr (cont); |
3140 | frame->code = car (cont) ; cont = cdr (cont); |
3382 | |
3141 | |
3383 | ++frame; |
3142 | ++frame; |
3384 | ++i; |
3143 | ++i; |
3385 | } |
3144 | } |
3386 | |
3145 | |
3387 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3146 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3388 | } |
3147 | } |
3389 | |
3148 | |
3390 | #else |
3149 | #else |
3391 | |
3150 | |
3392 | static INLINE void |
3151 | ecb_inline void |
3393 | dump_stack_reset (SCHEME_P) |
3152 | dump_stack_reset (SCHEME_P) |
3394 | { |
3153 | { |
3395 | SCHEME_V->dump = NIL; |
3154 | SCHEME_V->dump = NIL; |
3396 | } |
3155 | } |
3397 | |
3156 | |
3398 | static INLINE void |
3157 | ecb_inline void |
3399 | dump_stack_initialize (SCHEME_P) |
3158 | dump_stack_initialize (SCHEME_P) |
3400 | { |
3159 | { |
3401 | dump_stack_reset (SCHEME_A); |
3160 | dump_stack_reset (SCHEME_A); |
3402 | } |
3161 | } |
3403 | |
3162 | |
… | |
… | |
3405 | dump_stack_free (SCHEME_P) |
3164 | dump_stack_free (SCHEME_P) |
3406 | { |
3165 | { |
3407 | SCHEME_V->dump = NIL; |
3166 | SCHEME_V->dump = NIL; |
3408 | } |
3167 | } |
3409 | |
3168 | |
3410 | static pointer |
3169 | static int |
3411 | xs_return (SCHEME_P_ pointer a) |
3170 | xs_return (SCHEME_P_ pointer a) |
3412 | { |
3171 | { |
3413 | pointer dump = SCHEME_V->dump; |
3172 | pointer dump = SCHEME_V->dump; |
3414 | |
3173 | |
3415 | SCHEME_V->value = a; |
3174 | SCHEME_V->value = a; |
3416 | |
3175 | |
3417 | if (dump == NIL) |
3176 | if (dump == NIL) |
3418 | return NIL; |
3177 | return -1; |
3419 | |
3178 | |
3420 | SCHEME_V->op = ivalue (car (dump)); |
3179 | SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); |
3421 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3180 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3422 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3181 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3423 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3182 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3424 | |
3183 | |
3425 | SCHEME_V->dump = dump; |
3184 | SCHEME_V->dump = dump; |
3426 | |
3185 | |
3427 | return S_T; |
3186 | return 0; |
3428 | } |
3187 | } |
3429 | |
3188 | |
3430 | static void |
3189 | static void |
3431 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3190 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3432 | { |
3191 | { |
… | |
… | |
3457 | |
3216 | |
3458 | #endif |
3217 | #endif |
3459 | |
3218 | |
3460 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3219 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3461 | |
3220 | |
3462 | static pointer |
3221 | static int |
3463 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3222 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3464 | { |
3223 | { |
|
|
3224 | pointer args = SCHEME_V->args; |
3465 | pointer x, y; |
3225 | pointer x, y; |
3466 | |
3226 | |
3467 | switch (op) |
3227 | switch (op) |
3468 | { |
3228 | { |
3469 | case OP_LOAD: /* load */ |
3229 | case OP_LOAD: /* load */ |
3470 | if (file_interactive (SCHEME_A)) |
3230 | if (file_interactive (SCHEME_A)) |
3471 | { |
3231 | { |
3472 | xwrstr ("Loading "); xwrstr (strvalue (car (SCHEME_V->args))); xwrstr ("\n"); |
3232 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3473 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (SCHEME_V->args))); |
3233 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3474 | } |
3234 | } |
3475 | |
3235 | |
3476 | if (!file_push (SCHEME_A_ strvalue (car (SCHEME_V->args)))) |
3236 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3477 | Error_1 ("unable to open", car (SCHEME_V->args)); |
3237 | Error_1 ("unable to open", car (args)); |
3478 | else |
3238 | else |
3479 | { |
3239 | { |
3480 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3240 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3481 | s_goto (OP_T0LVL); |
3241 | s_goto (OP_T0LVL); |
3482 | } |
3242 | } |
… | |
… | |
3517 | s_save (SCHEME_A_ OP_VALUEPRINT, NIL, NIL); |
3277 | s_save (SCHEME_A_ OP_VALUEPRINT, NIL, NIL); |
3518 | s_save (SCHEME_A_ OP_T1LVL, NIL, NIL); |
3278 | s_save (SCHEME_A_ OP_T1LVL, NIL, NIL); |
3519 | s_goto (OP_READ_INTERNAL); |
3279 | s_goto (OP_READ_INTERNAL); |
3520 | |
3280 | |
3521 | case OP_T1LVL: /* top level */ |
3281 | case OP_T1LVL: /* top level */ |
3522 | SCHEME_V->code = SCHEME_V->value; |
3282 | SCHEME_V->code = SCHEME_V->value; |
3523 | SCHEME_V->inport = SCHEME_V->save_inport; |
3283 | SCHEME_V->inport = SCHEME_V->save_inport; |
3524 | s_goto (OP_EVAL); |
3284 | s_goto (OP_EVAL); |
3525 | |
3285 | |
3526 | case OP_READ_INTERNAL: /* internal read */ |
3286 | case OP_READ_INTERNAL: /* internal read */ |
3527 | SCHEME_V->tok = token (SCHEME_A); |
3287 | SCHEME_V->tok = token (SCHEME_A); |
3528 | |
3288 | |
3529 | if (SCHEME_V->tok == TOK_EOF) |
3289 | if (SCHEME_V->tok == TOK_EOF) |
3530 | { |
|
|
3531 | s_return (S_EOF); |
3290 | s_return (S_EOF); |
3532 | } |
|
|
3533 | |
3291 | |
3534 | s_goto (OP_RDSEXPR); |
3292 | s_goto (OP_RDSEXPR); |
3535 | |
3293 | |
3536 | case OP_GENSYM: |
3294 | case OP_GENSYM: |
3537 | s_return (gensym (SCHEME_A)); |
3295 | s_return (gensym (SCHEME_A)); |
… | |
… | |
3541 | /* OP_VALUEPRINT is always pushed, because when changing from |
3299 | /* OP_VALUEPRINT is always pushed, because when changing from |
3542 | non-interactive to interactive mode, it needs to be |
3300 | non-interactive to interactive mode, it needs to be |
3543 | already on the stack */ |
3301 | already on the stack */ |
3544 | #if USE_TRACING |
3302 | #if USE_TRACING |
3545 | if (SCHEME_V->tracing) |
3303 | if (SCHEME_V->tracing) |
3546 | { |
|
|
3547 | putstr (SCHEME_A_ "\nGives: "); |
3304 | putstr (SCHEME_A_ "\nGives: "); |
3548 | } |
|
|
3549 | #endif |
3305 | #endif |
3550 | |
3306 | |
3551 | if (file_interactive (SCHEME_A)) |
3307 | if (file_interactive (SCHEME_A)) |
3552 | { |
3308 | { |
3553 | SCHEME_V->print_flag = 1; |
3309 | SCHEME_V->print_flag = 1; |
3554 | SCHEME_V->args = SCHEME_V->value; |
3310 | SCHEME_V->args = SCHEME_V->value; |
3555 | s_goto (OP_P0LIST); |
3311 | s_goto (OP_P0LIST); |
3556 | } |
3312 | } |
3557 | else |
3313 | else |
3558 | { |
|
|
3559 | s_return (SCHEME_V->value); |
3314 | s_return (SCHEME_V->value); |
3560 | } |
|
|
3561 | |
3315 | |
3562 | case OP_EVAL: /* main part of evaluation */ |
3316 | case OP_EVAL: /* main part of evaluation */ |
3563 | #if USE_TRACING |
3317 | #if USE_TRACING |
3564 | if (SCHEME_V->tracing) |
3318 | if (SCHEME_V->tracing) |
3565 | { |
3319 | { |
3566 | /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */ |
3320 | /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */ |
3567 | s_save (SCHEME_A_ OP_REAL_EVAL, SCHEME_V->args, SCHEME_V->code); |
3321 | s_save (SCHEME_A_ OP_REAL_EVAL, args, SCHEME_V->code); |
3568 | SCHEME_V->args = SCHEME_V->code; |
3322 | SCHEME_V->args = SCHEME_V->code; |
3569 | putstr (SCHEME_A_ "\nEval: "); |
3323 | putstr (SCHEME_A_ "\nEval: "); |
3570 | s_goto (OP_P0LIST); |
3324 | s_goto (OP_P0LIST); |
3571 | } |
3325 | } |
3572 | |
3326 | |
3573 | /* fall through */ |
3327 | /* fall through */ |
|
|
3328 | |
3574 | case OP_REAL_EVAL: |
3329 | case OP_REAL_EVAL: |
3575 | #endif |
3330 | #endif |
3576 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3331 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3577 | { |
3332 | { |
3578 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3333 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
… | |
… | |
3582 | else |
3337 | else |
3583 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3338 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3584 | } |
3339 | } |
3585 | else if (is_pair (SCHEME_V->code)) |
3340 | else if (is_pair (SCHEME_V->code)) |
3586 | { |
3341 | { |
|
|
3342 | x = car (SCHEME_V->code); |
|
|
3343 | |
3587 | if (is_syntax (x = car (SCHEME_V->code))) /* SYNTAX */ |
3344 | if (is_syntax (x)) /* SYNTAX */ |
3588 | { |
3345 | { |
3589 | SCHEME_V->code = cdr (SCHEME_V->code); |
3346 | SCHEME_V->code = cdr (SCHEME_V->code); |
3590 | s_goto (syntaxnum (x)); |
3347 | s_goto (syntaxnum (x)); |
3591 | } |
3348 | } |
3592 | else /* first, eval top element and eval arguments */ |
3349 | else /* first, eval top element and eval arguments */ |
3593 | { |
3350 | { |
3594 | s_save (SCHEME_A_ OP_E0ARGS, NIL, SCHEME_V->code); |
3351 | s_save (SCHEME_A_ OP_E0ARGS, NIL, SCHEME_V->code); |
3595 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3352 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3596 | SCHEME_V->code = car (SCHEME_V->code); |
3353 | SCHEME_V->code = x; |
3597 | s_goto (OP_EVAL); |
3354 | s_goto (OP_EVAL); |
3598 | } |
3355 | } |
3599 | } |
3356 | } |
3600 | else |
3357 | else |
3601 | s_return (SCHEME_V->code); |
3358 | s_return (SCHEME_V->code); |
… | |
… | |
3613 | SCHEME_V->code = cdr (SCHEME_V->code); |
3370 | SCHEME_V->code = cdr (SCHEME_V->code); |
3614 | s_goto (OP_E1ARGS); |
3371 | s_goto (OP_E1ARGS); |
3615 | } |
3372 | } |
3616 | |
3373 | |
3617 | case OP_E1ARGS: /* eval arguments */ |
3374 | case OP_E1ARGS: /* eval arguments */ |
3618 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3375 | args = cons (SCHEME_V->value, args); |
3619 | |
3376 | |
3620 | if (is_pair (SCHEME_V->code)) /* continue */ |
3377 | if (is_pair (SCHEME_V->code)) /* continue */ |
3621 | { |
3378 | { |
3622 | s_save (SCHEME_A_ OP_E1ARGS, SCHEME_V->args, cdr (SCHEME_V->code)); |
3379 | s_save (SCHEME_A_ OP_E1ARGS, args, cdr (SCHEME_V->code)); |
3623 | SCHEME_V->code = car (SCHEME_V->code); |
3380 | SCHEME_V->code = car (SCHEME_V->code); |
3624 | SCHEME_V->args = NIL; |
3381 | SCHEME_V->args = NIL; |
3625 | s_goto (OP_EVAL); |
3382 | s_goto (OP_EVAL); |
3626 | } |
3383 | } |
3627 | else /* end */ |
3384 | else /* end */ |
3628 | { |
3385 | { |
3629 | SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); |
3386 | args = reverse_in_place (SCHEME_A_ NIL, args); |
3630 | SCHEME_V->code = car (SCHEME_V->args); |
3387 | SCHEME_V->code = car (args); |
3631 | SCHEME_V->args = cdr (SCHEME_V->args); |
3388 | SCHEME_V->args = cdr (args); |
3632 | s_goto (OP_APPLY); |
3389 | s_goto (OP_APPLY); |
3633 | } |
3390 | } |
3634 | |
3391 | |
3635 | #if USE_TRACING |
3392 | #if USE_TRACING |
3636 | |
3393 | |
3637 | case OP_TRACING: |
3394 | case OP_TRACING: |
3638 | { |
3395 | { |
3639 | int tr = SCHEME_V->tracing; |
3396 | int tr = SCHEME_V->tracing; |
3640 | |
3397 | |
3641 | SCHEME_V->tracing = ivalue (car (SCHEME_V->args)); |
3398 | SCHEME_V->tracing = ivalue (car (args)); |
3642 | s_return (mk_integer (SCHEME_A_ tr)); |
3399 | s_return (mk_integer (SCHEME_A_ tr)); |
3643 | } |
3400 | } |
3644 | |
3401 | |
3645 | #endif |
3402 | #endif |
3646 | |
3403 | |
3647 | case OP_APPLY: /* apply 'code' to 'args' */ |
3404 | case OP_APPLY: /* apply 'code' to 'args' */ |
3648 | #if USE_TRACING |
3405 | #if USE_TRACING |
3649 | if (SCHEME_V->tracing) |
3406 | if (SCHEME_V->tracing) |
3650 | { |
3407 | { |
3651 | s_save (SCHEME_A_ OP_REAL_APPLY, SCHEME_V->args, SCHEME_V->code); |
3408 | s_save (SCHEME_A_ OP_REAL_APPLY, args, SCHEME_V->code); |
3652 | SCHEME_V->print_flag = 1; |
3409 | SCHEME_V->print_flag = 1; |
3653 | /* SCHEME_V->args=cons(SCHEME_V->code,SCHEME_V->args); */ |
3410 | /* args=cons(SCHEME_V->code,args); */ |
3654 | putstr (SCHEME_A_ "\nApply to: "); |
3411 | putstr (SCHEME_A_ "\nApply to: "); |
3655 | s_goto (OP_P0LIST); |
3412 | s_goto (OP_P0LIST); |
3656 | } |
3413 | } |
3657 | |
3414 | |
3658 | /* fall through */ |
3415 | /* fall through */ |
|
|
3416 | |
3659 | case OP_REAL_APPLY: |
3417 | case OP_REAL_APPLY: |
3660 | #endif |
3418 | #endif |
3661 | if (is_proc (SCHEME_V->code)) |
3419 | if (is_proc (SCHEME_V->code)) |
3662 | { |
|
|
3663 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3420 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3664 | } |
|
|
3665 | else if (is_foreign (SCHEME_V->code)) |
3421 | else if (is_foreign (SCHEME_V->code)) |
3666 | { |
3422 | { |
3667 | /* Keep nested calls from GC'ing the arglist */ |
3423 | /* Keep nested calls from GC'ing the arglist */ |
3668 | push_recent_alloc (SCHEME_A_ SCHEME_V->args, NIL); |
3424 | push_recent_alloc (SCHEME_A_ args, NIL); |
3669 | x = SCHEME_V->code->object.ff (SCHEME_A_ SCHEME_V->args); |
3425 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3670 | |
3426 | |
3671 | s_return (x); |
3427 | s_return (x); |
3672 | } |
3428 | } |
3673 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3429 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3674 | { |
3430 | { |
3675 | /* Should not accept promise */ |
3431 | /* Should not accept promise */ |
3676 | /* make environment */ |
3432 | /* make environment */ |
3677 | new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); |
3433 | new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); |
3678 | |
3434 | |
3679 | for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y)) |
3435 | for (x = car (closure_code (SCHEME_V->code)), y = args; is_pair (x); x = cdr (x), y = cdr (y)) |
3680 | { |
3436 | { |
3681 | if (y == NIL) |
3437 | if (y == NIL) |
3682 | { |
|
|
3683 | Error_0 ("not enough arguments"); |
3438 | Error_0 ("not enough arguments"); |
3684 | } |
|
|
3685 | else |
3439 | else |
3686 | { |
|
|
3687 | new_slot_in_env (SCHEME_A_ car (x), car (y)); |
3440 | new_slot_in_env (SCHEME_A_ car (x), car (y)); |
3688 | } |
|
|
3689 | } |
3441 | } |
3690 | |
3442 | |
3691 | if (x == NIL) |
3443 | if (x == NIL) |
3692 | { |
3444 | { |
3693 | |
|
|
3694 | /*-- |
3445 | /*-- |
3695 | * if (y != NIL) { |
3446 | * if (y != NIL) { |
3696 | * Error_0("too many arguments"); |
3447 | * Error_0("too many arguments"); |
3697 | * } |
3448 | * } |
3698 | */ |
3449 | */ |
3699 | } |
3450 | } |
3700 | else if (is_symbol (x)) |
3451 | else if (is_symbol (x)) |
3701 | new_slot_in_env (SCHEME_A_ x, y); |
3452 | new_slot_in_env (SCHEME_A_ x, y); |
3702 | else |
3453 | else |
3703 | { |
|
|
3704 | Error_1 ("syntax error in closure: not a symbol:", x); |
3454 | Error_1 ("syntax error in closure: not a symbol:", x); |
3705 | } |
|
|
3706 | |
3455 | |
3707 | SCHEME_V->code = cdr (closure_code (SCHEME_V->code)); |
3456 | SCHEME_V->code = cdr (closure_code (SCHEME_V->code)); |
3708 | SCHEME_V->args = NIL; |
3457 | SCHEME_V->args = NIL; |
3709 | s_goto (OP_BEGIN); |
3458 | s_goto (OP_BEGIN); |
3710 | } |
3459 | } |
3711 | else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ |
3460 | else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ |
3712 | { |
3461 | { |
3713 | ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); |
3462 | ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); |
3714 | s_return (SCHEME_V->args != NIL ? car (SCHEME_V->args) : NIL); |
3463 | s_return (args != NIL ? car (args) : NIL); |
3715 | } |
3464 | } |
3716 | else |
3465 | else |
3717 | Error_0 ("illegal function"); |
3466 | Error_0 ("illegal function"); |
3718 | |
3467 | |
3719 | case OP_DOMACRO: /* do macro */ |
3468 | case OP_DOMACRO: /* do macro */ |
… | |
… | |
3728 | { |
3477 | { |
3729 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3478 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3730 | |
3479 | |
3731 | if (f != NIL) |
3480 | if (f != NIL) |
3732 | { |
3481 | { |
3733 | s_save (SCHEME_A_ OP_LAMBDA1, SCHEME_V->args, SCHEME_V->code); |
3482 | s_save (SCHEME_A_ OP_LAMBDA1, args, SCHEME_V->code); |
3734 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3483 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3735 | SCHEME_V->code = slot_value_in_env (f); |
3484 | SCHEME_V->code = slot_value_in_env (f); |
3736 | s_goto (OP_APPLY); |
3485 | s_goto (OP_APPLY); |
3737 | } |
3486 | } |
3738 | |
3487 | |
… | |
… | |
3749 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
3498 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
3750 | |
3499 | |
3751 | #endif |
3500 | #endif |
3752 | |
3501 | |
3753 | case OP_MKCLOSURE: /* make-closure */ |
3502 | case OP_MKCLOSURE: /* make-closure */ |
3754 | x = car (SCHEME_V->args); |
3503 | x = car (args); |
3755 | |
3504 | |
3756 | if (car (x) == SCHEME_V->LAMBDA) |
3505 | if (car (x) == SCHEME_V->LAMBDA) |
3757 | x = cdr (x); |
3506 | x = cdr (x); |
3758 | |
3507 | |
3759 | if (cdr (SCHEME_V->args) == NIL) |
3508 | if (cdr (args) == NIL) |
3760 | y = SCHEME_V->envir; |
3509 | y = SCHEME_V->envir; |
3761 | else |
3510 | else |
3762 | y = cadr (SCHEME_V->args); |
3511 | y = cadr (args); |
3763 | |
3512 | |
3764 | s_return (mk_closure (SCHEME_A_ x, y)); |
3513 | s_return (mk_closure (SCHEME_A_ x, y)); |
3765 | |
3514 | |
3766 | case OP_QUOTE: /* quote */ |
3515 | case OP_QUOTE: /* quote */ |
3767 | s_return (car (SCHEME_V->code)); |
3516 | s_return (car (SCHEME_V->code)); |
… | |
… | |
3780 | x = car (SCHEME_V->code); |
3529 | x = car (SCHEME_V->code); |
3781 | SCHEME_V->code = cadr (SCHEME_V->code); |
3530 | SCHEME_V->code = cadr (SCHEME_V->code); |
3782 | } |
3531 | } |
3783 | |
3532 | |
3784 | if (!is_symbol (x)) |
3533 | if (!is_symbol (x)) |
3785 | { |
|
|
3786 | Error_0 ("variable is not a symbol"); |
3534 | Error_0 ("variable is not a symbol"); |
3787 | } |
|
|
3788 | |
3535 | |
3789 | s_save (SCHEME_A_ OP_DEF1, NIL, x); |
3536 | s_save (SCHEME_A_ OP_DEF1, NIL, x); |
3790 | s_goto (OP_EVAL); |
3537 | s_goto (OP_EVAL); |
3791 | |
3538 | |
3792 | case OP_DEF1: /* define */ |
3539 | case OP_DEF1: /* define */ |
3793 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); |
3540 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); |
3794 | |
3541 | |
3795 | if (x != NIL) |
3542 | if (x != NIL) |
3796 | { |
|
|
3797 | set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); |
3543 | set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); |
3798 | } |
|
|
3799 | else |
3544 | else |
3800 | { |
|
|
3801 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3545 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3802 | } |
|
|
3803 | |
3546 | |
3804 | s_return (SCHEME_V->code); |
3547 | s_return (SCHEME_V->code); |
3805 | |
3548 | |
3806 | |
3549 | |
3807 | case OP_DEFP: /* defined? */ |
3550 | case OP_DEFP: /* defined? */ |
3808 | x = SCHEME_V->envir; |
3551 | x = SCHEME_V->envir; |
3809 | |
3552 | |
3810 | if (cdr (SCHEME_V->args) != NIL) |
3553 | if (cdr (args) != NIL) |
3811 | { |
|
|
3812 | x = cadr (SCHEME_V->args); |
3554 | x = cadr (args); |
3813 | } |
|
|
3814 | |
3555 | |
3815 | s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL); |
3556 | s_retbool (find_slot_in_env (SCHEME_A_ x, car (args), 1) != NIL); |
3816 | |
3557 | |
3817 | case OP_SET0: /* set! */ |
3558 | case OP_SET0: /* set! */ |
3818 | if (is_immutable (car (SCHEME_V->code))) |
3559 | if (is_immutable (car (SCHEME_V->code))) |
3819 | Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code)); |
3560 | Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code)); |
3820 | |
3561 | |
… | |
… | |
3829 | { |
3570 | { |
3830 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3571 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3831 | s_return (SCHEME_V->value); |
3572 | s_return (SCHEME_V->value); |
3832 | } |
3573 | } |
3833 | else |
3574 | else |
3834 | { |
|
|
3835 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3575 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3836 | } |
|
|
3837 | |
3576 | |
3838 | |
3577 | |
3839 | case OP_BEGIN: /* begin */ |
3578 | case OP_BEGIN: /* begin */ |
3840 | if (!is_pair (SCHEME_V->code)) |
3579 | if (!is_pair (SCHEME_V->code)) |
3841 | { |
|
|
3842 | s_return (SCHEME_V->code); |
3580 | s_return (SCHEME_V->code); |
3843 | } |
|
|
3844 | |
3581 | |
3845 | if (cdr (SCHEME_V->code) != NIL) |
3582 | if (cdr (SCHEME_V->code) != NIL) |
3846 | { |
|
|
3847 | s_save (SCHEME_A_ OP_BEGIN, NIL, cdr (SCHEME_V->code)); |
3583 | s_save (SCHEME_A_ OP_BEGIN, NIL, cdr (SCHEME_V->code)); |
3848 | } |
|
|
3849 | |
3584 | |
3850 | SCHEME_V->code = car (SCHEME_V->code); |
3585 | SCHEME_V->code = car (SCHEME_V->code); |
3851 | s_goto (OP_EVAL); |
3586 | s_goto (OP_EVAL); |
3852 | |
3587 | |
3853 | case OP_IF0: /* if */ |
3588 | case OP_IF0: /* if */ |
… | |
… | |
3857 | |
3592 | |
3858 | case OP_IF1: /* if */ |
3593 | case OP_IF1: /* if */ |
3859 | if (is_true (SCHEME_V->value)) |
3594 | if (is_true (SCHEME_V->value)) |
3860 | SCHEME_V->code = car (SCHEME_V->code); |
3595 | SCHEME_V->code = car (SCHEME_V->code); |
3861 | else |
3596 | else |
3862 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because |
3597 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
3863 | |
|
|
3864 | * car(NIL) = NIL */ |
|
|
3865 | s_goto (OP_EVAL); |
3598 | s_goto (OP_EVAL); |
3866 | |
3599 | |
3867 | case OP_LET0: /* let */ |
3600 | case OP_LET0: /* let */ |
3868 | SCHEME_V->args = NIL; |
3601 | SCHEME_V->args = NIL; |
3869 | SCHEME_V->value = SCHEME_V->code; |
3602 | SCHEME_V->value = SCHEME_V->code; |
3870 | SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); |
3603 | SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); |
3871 | s_goto (OP_LET1); |
3604 | s_goto (OP_LET1); |
3872 | |
3605 | |
3873 | case OP_LET1: /* let (calculate parameters) */ |
3606 | case OP_LET1: /* let (calculate parameters) */ |
3874 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3607 | args = cons (SCHEME_V->value, args); |
3875 | |
3608 | |
3876 | if (is_pair (SCHEME_V->code)) /* continue */ |
3609 | if (is_pair (SCHEME_V->code)) /* continue */ |
3877 | { |
3610 | { |
3878 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
3611 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
3879 | { |
|
|
3880 | Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); |
3612 | Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); |
3881 | } |
|
|
3882 | |
3613 | |
3883 | s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code)); |
3614 | s_save (SCHEME_A_ OP_LET1, args, cdr (SCHEME_V->code)); |
3884 | SCHEME_V->code = cadar (SCHEME_V->code); |
3615 | SCHEME_V->code = cadar (SCHEME_V->code); |
3885 | SCHEME_V->args = NIL; |
3616 | SCHEME_V->args = NIL; |
3886 | s_goto (OP_EVAL); |
3617 | s_goto (OP_EVAL); |
3887 | } |
3618 | } |
3888 | else /* end */ |
3619 | else /* end */ |
3889 | { |
3620 | { |
3890 | SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); |
3621 | args = reverse_in_place (SCHEME_A_ NIL, args); |
3891 | SCHEME_V->code = car (SCHEME_V->args); |
3622 | SCHEME_V->code = car (args); |
3892 | SCHEME_V->args = cdr (SCHEME_V->args); |
3623 | SCHEME_V->args = cdr (args); |
3893 | s_goto (OP_LET2); |
3624 | s_goto (OP_LET2); |
3894 | } |
3625 | } |
3895 | |
3626 | |
3896 | case OP_LET2: /* let */ |
3627 | case OP_LET2: /* let */ |
3897 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
3628 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
3898 | |
3629 | |
3899 | for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args; |
3630 | for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args; |
3900 | y != NIL; x = cdr (x), y = cdr (y)) |
3631 | y != NIL; x = cdr (x), y = cdr (y)) |
3901 | { |
|
|
3902 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3632 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3903 | } |
|
|
3904 | |
3633 | |
3905 | if (is_symbol (car (SCHEME_V->code))) /* named let */ |
3634 | if (is_symbol (car (SCHEME_V->code))) /* named let */ |
3906 | { |
3635 | { |
3907 | for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x)) |
3636 | for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x)) |
3908 | { |
3637 | { |
3909 | if (!is_pair (x)) |
3638 | if (!is_pair (x)) |
3910 | Error_1 ("Bad syntax of binding in let :", x); |
3639 | Error_1 ("Bad syntax of binding in let :", x); |
3911 | |
3640 | |
3912 | if (!is_list (SCHEME_A_ car (x))) |
3641 | if (!is_list (SCHEME_A_ car (x))) |
3913 | Error_1 ("Bad syntax of binding in let :", car (x)); |
3642 | Error_1 ("Bad syntax of binding in let :", car (x)); |
3914 | |
3643 | |
3915 | SCHEME_V->args = cons (caar (x), SCHEME_V->args); |
3644 | args = cons (caar (x), args); |
3916 | } |
3645 | } |
3917 | |
3646 | |
3918 | x = |
|
|
3919 | mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args), cddr (SCHEME_V->code)), |
3647 | x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)), |
3920 | SCHEME_V->envir); |
3648 | SCHEME_V->envir); |
3921 | new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x); |
3649 | new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x); |
3922 | SCHEME_V->code = cddr (SCHEME_V->code); |
3650 | SCHEME_V->code = cddr (SCHEME_V->code); |
3923 | SCHEME_V->args = NIL; |
|
|
3924 | } |
3651 | } |
3925 | else |
3652 | else |
3926 | { |
3653 | { |
3927 | SCHEME_V->code = cdr (SCHEME_V->code); |
3654 | SCHEME_V->code = cdr (SCHEME_V->code); |
|
|
3655 | } |
|
|
3656 | |
3928 | SCHEME_V->args = NIL; |
3657 | SCHEME_V->args = NIL; |
3929 | } |
|
|
3930 | |
|
|
3931 | s_goto (OP_BEGIN); |
3658 | s_goto (OP_BEGIN); |
3932 | |
3659 | |
3933 | case OP_LET0AST: /* let* */ |
3660 | case OP_LET0AST: /* let* */ |
3934 | if (car (SCHEME_V->code) == NIL) |
3661 | if (car (SCHEME_V->code) == NIL) |
3935 | { |
3662 | { |
… | |
… | |
3953 | new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value); |
3680 | new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value); |
3954 | SCHEME_V->code = cdr (SCHEME_V->code); |
3681 | SCHEME_V->code = cdr (SCHEME_V->code); |
3955 | |
3682 | |
3956 | if (is_pair (SCHEME_V->code)) /* continue */ |
3683 | if (is_pair (SCHEME_V->code)) /* continue */ |
3957 | { |
3684 | { |
3958 | s_save (SCHEME_A_ OP_LET2AST, SCHEME_V->args, SCHEME_V->code); |
3685 | s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code); |
3959 | SCHEME_V->code = cadar (SCHEME_V->code); |
3686 | SCHEME_V->code = cadar (SCHEME_V->code); |
3960 | SCHEME_V->args = NIL; |
3687 | SCHEME_V->args = NIL; |
3961 | s_goto (OP_EVAL); |
3688 | s_goto (OP_EVAL); |
3962 | } |
3689 | } |
3963 | else /* end */ |
3690 | else /* end */ |
3964 | { |
3691 | { |
3965 | SCHEME_V->code = SCHEME_V->args; |
3692 | SCHEME_V->code = args; |
3966 | SCHEME_V->args = NIL; |
3693 | SCHEME_V->args = NIL; |
3967 | s_goto (OP_BEGIN); |
3694 | s_goto (OP_BEGIN); |
3968 | } |
3695 | } |
3969 | |
3696 | |
3970 | case OP_LET0REC: /* letrec */ |
3697 | case OP_LET0REC: /* letrec */ |
… | |
… | |
3973 | SCHEME_V->value = SCHEME_V->code; |
3700 | SCHEME_V->value = SCHEME_V->code; |
3974 | SCHEME_V->code = car (SCHEME_V->code); |
3701 | SCHEME_V->code = car (SCHEME_V->code); |
3975 | s_goto (OP_LET1REC); |
3702 | s_goto (OP_LET1REC); |
3976 | |
3703 | |
3977 | case OP_LET1REC: /* letrec (calculate parameters) */ |
3704 | case OP_LET1REC: /* letrec (calculate parameters) */ |
3978 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3705 | args = cons (SCHEME_V->value, args); |
3979 | |
3706 | |
3980 | if (is_pair (SCHEME_V->code)) /* continue */ |
3707 | if (is_pair (SCHEME_V->code)) /* continue */ |
3981 | { |
3708 | { |
3982 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
3709 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
3983 | { |
|
|
3984 | Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code)); |
3710 | Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code)); |
3985 | } |
|
|
3986 | |
3711 | |
3987 | s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code)); |
3712 | s_save (SCHEME_A_ OP_LET1REC, args, cdr (SCHEME_V->code)); |
3988 | SCHEME_V->code = cadar (SCHEME_V->code); |
3713 | SCHEME_V->code = cadar (SCHEME_V->code); |
3989 | SCHEME_V->args = NIL; |
3714 | SCHEME_V->args = NIL; |
3990 | s_goto (OP_EVAL); |
3715 | s_goto (OP_EVAL); |
3991 | } |
3716 | } |
3992 | else /* end */ |
3717 | else /* end */ |
3993 | { |
3718 | { |
3994 | SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); |
3719 | args = reverse_in_place (SCHEME_A_ NIL, args); |
3995 | SCHEME_V->code = car (SCHEME_V->args); |
3720 | SCHEME_V->code = car (args); |
3996 | SCHEME_V->args = cdr (SCHEME_V->args); |
3721 | SCHEME_V->args = cdr (args); |
3997 | s_goto (OP_LET2REC); |
3722 | s_goto (OP_LET2REC); |
3998 | } |
3723 | } |
3999 | |
3724 | |
4000 | case OP_LET2REC: /* letrec */ |
3725 | case OP_LET2REC: /* letrec */ |
4001 | for (x = car (SCHEME_V->code), y = SCHEME_V->args; y != NIL; x = cdr (x), y = cdr (y)) |
3726 | for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y)) |
4002 | { |
|
|
4003 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3727 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
4004 | } |
|
|
4005 | |
3728 | |
4006 | SCHEME_V->code = cdr (SCHEME_V->code); |
3729 | SCHEME_V->code = cdr (SCHEME_V->code); |
4007 | SCHEME_V->args = NIL; |
3730 | SCHEME_V->args = NIL; |
4008 | s_goto (OP_BEGIN); |
3731 | s_goto (OP_BEGIN); |
4009 | |
3732 | |
4010 | case OP_COND0: /* cond */ |
3733 | case OP_COND0: /* cond */ |
4011 | if (!is_pair (SCHEME_V->code)) |
3734 | if (!is_pair (SCHEME_V->code)) |
4012 | { |
|
|
4013 | Error_0 ("syntax error in cond"); |
3735 | Error_0 ("syntax error in cond"); |
4014 | } |
|
|
4015 | |
3736 | |
4016 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3737 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
4017 | SCHEME_V->code = caar (SCHEME_V->code); |
3738 | SCHEME_V->code = caar (SCHEME_V->code); |
4018 | s_goto (OP_EVAL); |
3739 | s_goto (OP_EVAL); |
4019 | |
3740 | |
4020 | case OP_COND1: /* cond */ |
3741 | case OP_COND1: /* cond */ |
4021 | if (is_true (SCHEME_V->value)) |
3742 | if (is_true (SCHEME_V->value)) |
4022 | { |
3743 | { |
4023 | if ((SCHEME_V->code = cdar (SCHEME_V->code)) == NIL) |
3744 | if ((SCHEME_V->code = cdar (SCHEME_V->code)) == NIL) |
4024 | { |
|
|
4025 | s_return (SCHEME_V->value); |
3745 | s_return (SCHEME_V->value); |
4026 | } |
|
|
4027 | |
3746 | |
4028 | if (car (SCHEME_V->code) == SCHEME_V->FEED_TO) |
3747 | if (car (SCHEME_V->code) == SCHEME_V->FEED_TO) |
4029 | { |
3748 | { |
4030 | if (!is_pair (cdr (SCHEME_V->code))) |
3749 | if (!is_pair (cdr (SCHEME_V->code))) |
4031 | { |
|
|
4032 | Error_0 ("syntax error in cond"); |
3750 | Error_0 ("syntax error in cond"); |
4033 | } |
|
|
4034 | |
3751 | |
4035 | x = cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)); |
3752 | x = cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)); |
4036 | SCHEME_V->code = cons (cadr (SCHEME_V->code), cons (x, NIL)); |
3753 | SCHEME_V->code = cons (cadr (SCHEME_V->code), cons (x, NIL)); |
4037 | s_goto (OP_EVAL); |
3754 | s_goto (OP_EVAL); |
4038 | } |
3755 | } |
… | |
… | |
4040 | s_goto (OP_BEGIN); |
3757 | s_goto (OP_BEGIN); |
4041 | } |
3758 | } |
4042 | else |
3759 | else |
4043 | { |
3760 | { |
4044 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3761 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
4045 | { |
|
|
4046 | s_return (NIL); |
3762 | s_return (NIL); |
4047 | } |
|
|
4048 | else |
3763 | else |
4049 | { |
3764 | { |
4050 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3765 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
4051 | SCHEME_V->code = caar (SCHEME_V->code); |
3766 | SCHEME_V->code = caar (SCHEME_V->code); |
4052 | s_goto (OP_EVAL); |
3767 | s_goto (OP_EVAL); |
… | |
… | |
4058 | set_typeflag (x, T_PROMISE); |
3773 | set_typeflag (x, T_PROMISE); |
4059 | s_return (x); |
3774 | s_return (x); |
4060 | |
3775 | |
4061 | case OP_AND0: /* and */ |
3776 | case OP_AND0: /* and */ |
4062 | if (SCHEME_V->code == NIL) |
3777 | if (SCHEME_V->code == NIL) |
4063 | { |
|
|
4064 | s_return (S_T); |
3778 | s_return (S_T); |
4065 | } |
|
|
4066 | |
3779 | |
4067 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3780 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
4068 | SCHEME_V->code = car (SCHEME_V->code); |
3781 | SCHEME_V->code = car (SCHEME_V->code); |
4069 | s_goto (OP_EVAL); |
3782 | s_goto (OP_EVAL); |
4070 | |
3783 | |
4071 | case OP_AND1: /* and */ |
3784 | case OP_AND1: /* and */ |
4072 | if (is_false (SCHEME_V->value)) |
3785 | if (is_false (SCHEME_V->value)) |
4073 | { |
|
|
4074 | s_return (SCHEME_V->value); |
3786 | s_return (SCHEME_V->value); |
4075 | } |
|
|
4076 | else if (SCHEME_V->code == NIL) |
3787 | else if (SCHEME_V->code == NIL) |
4077 | { |
|
|
4078 | s_return (SCHEME_V->value); |
3788 | s_return (SCHEME_V->value); |
4079 | } |
|
|
4080 | else |
3789 | else |
4081 | { |
3790 | { |
4082 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3791 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
4083 | SCHEME_V->code = car (SCHEME_V->code); |
3792 | SCHEME_V->code = car (SCHEME_V->code); |
4084 | s_goto (OP_EVAL); |
3793 | s_goto (OP_EVAL); |
4085 | } |
3794 | } |
4086 | |
3795 | |
4087 | case OP_OR0: /* or */ |
3796 | case OP_OR0: /* or */ |
4088 | if (SCHEME_V->code == NIL) |
3797 | if (SCHEME_V->code == NIL) |
4089 | { |
|
|
4090 | s_return (S_F); |
3798 | s_return (S_F); |
4091 | } |
|
|
4092 | |
3799 | |
4093 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3800 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
4094 | SCHEME_V->code = car (SCHEME_V->code); |
3801 | SCHEME_V->code = car (SCHEME_V->code); |
4095 | s_goto (OP_EVAL); |
3802 | s_goto (OP_EVAL); |
4096 | |
3803 | |
4097 | case OP_OR1: /* or */ |
3804 | case OP_OR1: /* or */ |
4098 | if (is_true (SCHEME_V->value)) |
3805 | if (is_true (SCHEME_V->value)) |
4099 | { |
|
|
4100 | s_return (SCHEME_V->value); |
3806 | s_return (SCHEME_V->value); |
4101 | } |
|
|
4102 | else if (SCHEME_V->code == NIL) |
3807 | else if (SCHEME_V->code == NIL) |
4103 | { |
|
|
4104 | s_return (SCHEME_V->value); |
3808 | s_return (SCHEME_V->value); |
4105 | } |
|
|
4106 | else |
3809 | else |
4107 | { |
3810 | { |
4108 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3811 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
4109 | SCHEME_V->code = car (SCHEME_V->code); |
3812 | SCHEME_V->code = car (SCHEME_V->code); |
4110 | s_goto (OP_EVAL); |
3813 | s_goto (OP_EVAL); |
… | |
… | |
4114 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3817 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
4115 | SCHEME_V->code = car (SCHEME_V->code); |
3818 | SCHEME_V->code = car (SCHEME_V->code); |
4116 | s_goto (OP_EVAL); |
3819 | s_goto (OP_EVAL); |
4117 | |
3820 | |
4118 | case OP_C1STREAM: /* cons-stream */ |
3821 | case OP_C1STREAM: /* cons-stream */ |
4119 | SCHEME_V->args = SCHEME_V->value; /* save SCHEME_V->value to register SCHEME_V->args for gc */ |
3822 | SCHEME_V->args = SCHEME_V->value; /* save SCHEME_V->value to register args for gc */ |
4120 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
3823 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
4121 | set_typeflag (x, T_PROMISE); |
3824 | set_typeflag (x, T_PROMISE); |
4122 | s_return (cons (SCHEME_V->args, x)); |
3825 | s_return (cons (args, x)); |
4123 | |
3826 | |
4124 | case OP_MACRO0: /* macro */ |
3827 | case OP_MACRO0: /* macro */ |
4125 | if (is_pair (car (SCHEME_V->code))) |
3828 | if (is_pair (car (SCHEME_V->code))) |
4126 | { |
3829 | { |
4127 | x = caar (SCHEME_V->code); |
3830 | x = caar (SCHEME_V->code); |
… | |
… | |
4132 | x = car (SCHEME_V->code); |
3835 | x = car (SCHEME_V->code); |
4133 | SCHEME_V->code = cadr (SCHEME_V->code); |
3836 | SCHEME_V->code = cadr (SCHEME_V->code); |
4134 | } |
3837 | } |
4135 | |
3838 | |
4136 | if (!is_symbol (x)) |
3839 | if (!is_symbol (x)) |
4137 | { |
|
|
4138 | Error_0 ("variable is not a symbol"); |
3840 | Error_0 ("variable is not a symbol"); |
4139 | } |
|
|
4140 | |
3841 | |
4141 | s_save (SCHEME_A_ OP_MACRO1, NIL, x); |
3842 | s_save (SCHEME_A_ OP_MACRO1, NIL, x); |
4142 | s_goto (OP_EVAL); |
3843 | s_goto (OP_EVAL); |
4143 | |
3844 | |
4144 | case OP_MACRO1: /* macro */ |
3845 | case OP_MACRO1: /* macro */ |
4145 | set_typeflag (SCHEME_V->value, T_MACRO); |
3846 | set_typeflag (SCHEME_V->value, T_MACRO); |
4146 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); |
3847 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); |
4147 | |
3848 | |
4148 | if (x != NIL) |
3849 | if (x != NIL) |
4149 | { |
|
|
4150 | set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); |
3850 | set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); |
4151 | } |
|
|
4152 | else |
3851 | else |
4153 | { |
|
|
4154 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3852 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
4155 | } |
|
|
4156 | |
3853 | |
4157 | s_return (SCHEME_V->code); |
3854 | s_return (SCHEME_V->code); |
4158 | |
3855 | |
4159 | case OP_CASE0: /* case */ |
3856 | case OP_CASE0: /* case */ |
4160 | s_save (SCHEME_A_ OP_CASE1, NIL, cdr (SCHEME_V->code)); |
3857 | s_save (SCHEME_A_ OP_CASE1, NIL, cdr (SCHEME_V->code)); |
… | |
… | |
4163 | |
3860 | |
4164 | case OP_CASE1: /* case */ |
3861 | case OP_CASE1: /* case */ |
4165 | for (x = SCHEME_V->code; x != NIL; x = cdr (x)) |
3862 | for (x = SCHEME_V->code; x != NIL; x = cdr (x)) |
4166 | { |
3863 | { |
4167 | if (!is_pair (y = caar (x))) |
3864 | if (!is_pair (y = caar (x))) |
4168 | { |
|
|
4169 | break; |
3865 | break; |
4170 | } |
|
|
4171 | |
3866 | |
4172 | for (; y != NIL; y = cdr (y)) |
3867 | for (; y != NIL; y = cdr (y)) |
4173 | { |
|
|
4174 | if (eqv (car (y), SCHEME_V->value)) |
3868 | if (eqv (car (y), SCHEME_V->value)) |
4175 | { |
|
|
4176 | break; |
3869 | break; |
4177 | } |
|
|
4178 | } |
|
|
4179 | |
3870 | |
4180 | if (y != NIL) |
3871 | if (y != NIL) |
4181 | { |
|
|
4182 | break; |
3872 | break; |
4183 | } |
|
|
4184 | } |
3873 | } |
4185 | |
3874 | |
4186 | if (x != NIL) |
3875 | if (x != NIL) |
4187 | { |
3876 | { |
4188 | if (is_pair (caar (x))) |
3877 | if (is_pair (caar (x))) |
… | |
… | |
4196 | SCHEME_V->code = caar (x); |
3885 | SCHEME_V->code = caar (x); |
4197 | s_goto (OP_EVAL); |
3886 | s_goto (OP_EVAL); |
4198 | } |
3887 | } |
4199 | } |
3888 | } |
4200 | else |
3889 | else |
4201 | { |
|
|
4202 | s_return (NIL); |
3890 | s_return (NIL); |
4203 | } |
|
|
4204 | |
3891 | |
4205 | case OP_CASE2: /* case */ |
3892 | case OP_CASE2: /* case */ |
4206 | if (is_true (SCHEME_V->value)) |
3893 | if (is_true (SCHEME_V->value)) |
4207 | { |
|
|
4208 | s_goto (OP_BEGIN); |
3894 | s_goto (OP_BEGIN); |
4209 | } |
|
|
4210 | else |
3895 | else |
4211 | { |
|
|
4212 | s_return (NIL); |
3896 | s_return (NIL); |
4213 | } |
|
|
4214 | |
3897 | |
4215 | case OP_PAPPLY: /* apply */ |
3898 | case OP_PAPPLY: /* apply */ |
4216 | SCHEME_V->code = car (SCHEME_V->args); |
3899 | SCHEME_V->code = car (args); |
4217 | SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args)); |
3900 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
4218 | /*SCHEME_V->args = cadr(SCHEME_V->args); */ |
3901 | /*SCHEME_V->args = cadr(args); */ |
4219 | s_goto (OP_APPLY); |
3902 | s_goto (OP_APPLY); |
4220 | |
3903 | |
4221 | case OP_PEVAL: /* eval */ |
3904 | case OP_PEVAL: /* eval */ |
4222 | if (cdr (SCHEME_V->args) != NIL) |
3905 | if (cdr (args) != NIL) |
4223 | { |
|
|
4224 | SCHEME_V->envir = cadr (SCHEME_V->args); |
3906 | SCHEME_V->envir = cadr (args); |
4225 | } |
|
|
4226 | |
3907 | |
4227 | SCHEME_V->code = car (SCHEME_V->args); |
3908 | SCHEME_V->code = car (args); |
4228 | s_goto (OP_EVAL); |
3909 | s_goto (OP_EVAL); |
4229 | |
3910 | |
4230 | case OP_CONTINUATION: /* call-with-current-continuation */ |
3911 | case OP_CONTINUATION: /* call-with-current-continuation */ |
4231 | SCHEME_V->code = car (SCHEME_V->args); |
3912 | SCHEME_V->code = car (args); |
4232 | SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_V)), NIL); |
3913 | SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); |
4233 | s_goto (OP_APPLY); |
3914 | s_goto (OP_APPLY); |
4234 | } |
3915 | } |
4235 | |
3916 | |
4236 | return S_T; |
3917 | if (USE_ERROR_CHECKING) abort (); |
4237 | } |
3918 | } |
4238 | |
3919 | |
4239 | static pointer |
3920 | static int |
4240 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
3921 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4241 | { |
3922 | { |
4242 | pointer x; |
3923 | pointer args = SCHEME_V->args; |
|
|
3924 | pointer x = car (args); |
4243 | num v; |
3925 | num v; |
4244 | |
3926 | |
4245 | #if USE_MATH |
3927 | #if USE_MATH |
4246 | RVALUE dd; |
3928 | RVALUE dd; |
4247 | #endif |
3929 | #endif |
4248 | |
3930 | |
4249 | switch (op) |
3931 | switch (op) |
4250 | { |
3932 | { |
4251 | #if USE_MATH |
3933 | #if USE_MATH |
4252 | |
|
|
4253 | case OP_INEX2EX: /* inexact->exact */ |
3934 | case OP_INEX2EX: /* inexact->exact */ |
4254 | x = car (SCHEME_V->args); |
|
|
4255 | |
|
|
4256 | if (num_is_integer (x)) |
3935 | if (num_is_integer (x)) |
4257 | { |
|
|
4258 | s_return (x); |
3936 | s_return (x); |
4259 | } |
|
|
4260 | else if (modf (rvalue_unchecked (x), &dd) == 0.0) |
3937 | else if (modf (rvalue_unchecked (x), &dd) == 0) |
4261 | { |
|
|
4262 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
3938 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4263 | } |
|
|
4264 | else |
3939 | else |
4265 | { |
|
|
4266 | Error_1 ("inexact->exact: not integral:", x); |
3940 | Error_1 ("inexact->exact: not integral:", x); |
4267 | } |
|
|
4268 | |
3941 | |
4269 | case OP_EXP: |
|
|
4270 | x = car (SCHEME_V->args); |
|
|
4271 | s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
3942 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4272 | |
|
|
4273 | case OP_LOG: |
|
|
4274 | x = car (SCHEME_V->args); |
|
|
4275 | s_return (mk_real (SCHEME_A_ log (rvalue (x)))); |
3943 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); |
4276 | |
|
|
4277 | case OP_SIN: |
|
|
4278 | x = car (SCHEME_V->args); |
|
|
4279 | s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
3944 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4280 | |
|
|
4281 | case OP_COS: |
|
|
4282 | x = car (SCHEME_V->args); |
|
|
4283 | s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
3945 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
4284 | |
|
|
4285 | case OP_TAN: |
|
|
4286 | x = car (SCHEME_V->args); |
|
|
4287 | s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
3946 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
4288 | |
|
|
4289 | case OP_ASIN: |
|
|
4290 | x = car (SCHEME_V->args); |
|
|
4291 | s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
3947 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
4292 | |
|
|
4293 | case OP_ACOS: |
|
|
4294 | x = car (SCHEME_V->args); |
|
|
4295 | s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
3948 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4296 | |
3949 | |
4297 | case OP_ATAN: |
3950 | case OP_ATAN: |
4298 | x = car (SCHEME_V->args); |
|
|
4299 | |
|
|
4300 | if (cdr (SCHEME_V->args) == NIL) |
3951 | if (cdr (args) == NIL) |
4301 | { |
|
|
4302 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
3952 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4303 | } |
|
|
4304 | else |
3953 | else |
4305 | { |
3954 | { |
4306 | pointer y = cadr (SCHEME_V->args); |
3955 | pointer y = cadr (args); |
4307 | |
|
|
4308 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
3956 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
4309 | } |
3957 | } |
4310 | |
3958 | |
4311 | case OP_SQRT: |
3959 | case OP_SQRT: |
4312 | x = car (SCHEME_V->args); |
|
|
4313 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
3960 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
4314 | |
3961 | |
4315 | case OP_EXPT: |
3962 | case OP_EXPT: |
4316 | { |
3963 | { |
4317 | RVALUE result; |
3964 | RVALUE result; |
4318 | int real_result = 1; |
3965 | int real_result = 1; |
4319 | pointer y = cadr (SCHEME_V->args); |
3966 | pointer y = cadr (args); |
4320 | |
|
|
4321 | x = car (SCHEME_V->args); |
|
|
4322 | |
3967 | |
4323 | if (num_is_integer (x) && num_is_integer (y)) |
3968 | if (num_is_integer (x) && num_is_integer (y)) |
4324 | real_result = 0; |
3969 | real_result = 0; |
4325 | |
3970 | |
4326 | /* This 'if' is an R5RS compatibility fix. */ |
3971 | /* This 'if' is an R5RS compatibility fix. */ |
4327 | /* NOTE: Remove this 'if' fix for R6RS. */ |
3972 | /* NOTE: Remove this 'if' fix for R6RS. */ |
4328 | if (rvalue (x) == 0 && rvalue (y) < 0) |
3973 | if (rvalue (x) == 0 && rvalue (y) < 0) |
4329 | { |
|
|
4330 | result = 0.0; |
3974 | result = 0; |
4331 | } |
|
|
4332 | else |
3975 | else |
4333 | { |
|
|
4334 | result = pow (rvalue (x), rvalue (y)); |
3976 | result = pow (rvalue (x), rvalue (y)); |
4335 | } |
|
|
4336 | |
3977 | |
4337 | /* Before returning integer result make sure we can. */ |
3978 | /* Before returning integer result make sure we can. */ |
4338 | /* If the test fails, result is too big for integer. */ |
3979 | /* If the test fails, result is too big for integer. */ |
4339 | if (!real_result) |
3980 | if (!real_result) |
4340 | { |
3981 | { |
4341 | long result_as_long = (long) result; |
3982 | long result_as_long = result; |
4342 | |
3983 | |
4343 | if (result != (RVALUE) result_as_long) |
3984 | if (result != (RVALUE) result_as_long) |
4344 | real_result = 1; |
3985 | real_result = 1; |
4345 | } |
3986 | } |
4346 | |
3987 | |
4347 | if (real_result) |
3988 | if (real_result) |
4348 | { |
|
|
4349 | s_return (mk_real (SCHEME_A_ result)); |
3989 | s_return (mk_real (SCHEME_A_ result)); |
4350 | } |
|
|
4351 | else |
3990 | else |
4352 | { |
|
|
4353 | s_return (mk_integer (SCHEME_A_ result)); |
3991 | s_return (mk_integer (SCHEME_A_ result)); |
4354 | } |
|
|
4355 | } |
3992 | } |
4356 | |
3993 | |
4357 | case OP_FLOOR: |
|
|
4358 | x = car (SCHEME_V->args); |
|
|
4359 | s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
3994 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
4360 | |
|
|
4361 | case OP_CEILING: |
|
|
4362 | x = car (SCHEME_V->args); |
|
|
4363 | s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
3995 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
4364 | |
3996 | |
4365 | case OP_TRUNCATE: |
3997 | case OP_TRUNCATE: |
4366 | { |
3998 | { |
4367 | RVALUE rvalue_of_x; |
3999 | RVALUE rvalue_of_x; |
4368 | |
4000 | |
4369 | x = car (SCHEME_V->args); |
|
|
4370 | rvalue_of_x = rvalue (x); |
4001 | rvalue_of_x = rvalue (x); |
4371 | |
4002 | |
4372 | if (rvalue_of_x > 0) |
4003 | if (rvalue_of_x > 0) |
4373 | { |
|
|
4374 | s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); |
4004 | s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); |
4375 | } |
|
|
4376 | else |
4005 | else |
4377 | { |
|
|
4378 | s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); |
4006 | s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); |
4379 | } |
|
|
4380 | } |
4007 | } |
4381 | |
4008 | |
4382 | case OP_ROUND: |
4009 | case OP_ROUND: |
4383 | x = car (SCHEME_V->args); |
|
|
4384 | |
|
|
4385 | if (num_is_integer (x)) |
4010 | if (num_is_integer (x)) |
4386 | s_return (x); |
4011 | s_return (x); |
4387 | |
4012 | |
4388 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
4013 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
4389 | #endif |
4014 | #endif |
4390 | |
4015 | |
4391 | case OP_ADD: /* + */ |
4016 | case OP_ADD: /* + */ |
4392 | v = num_zero; |
4017 | v = num_zero; |
4393 | |
4018 | |
4394 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4019 | for (x = args; x != NIL; x = cdr (x)) |
4395 | v = num_add (v, nvalue (car (x))); |
4020 | v = num_op (NUM_ADD, v, nvalue (car (x))); |
4396 | |
4021 | |
4397 | s_return (mk_number (SCHEME_A_ v)); |
4022 | s_return (mk_number (SCHEME_A_ v)); |
4398 | |
4023 | |
4399 | case OP_MUL: /* * */ |
4024 | case OP_MUL: /* * */ |
4400 | v = num_one; |
4025 | v = num_one; |
4401 | |
4026 | |
4402 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4027 | for (x = args; x != NIL; x = cdr (x)) |
4403 | v = num_mul (v, nvalue (car (x))); |
4028 | v = num_op (NUM_MUL, v, nvalue (car (x))); |
4404 | |
4029 | |
4405 | s_return (mk_number (SCHEME_A_ v)); |
4030 | s_return (mk_number (SCHEME_A_ v)); |
4406 | |
4031 | |
4407 | case OP_SUB: /* - */ |
4032 | case OP_SUB: /* - */ |
4408 | if (cdr (SCHEME_V->args) == NIL) |
4033 | if (cdr (args) == NIL) |
4409 | { |
4034 | { |
4410 | x = SCHEME_V->args; |
4035 | x = args; |
4411 | v = num_zero; |
4036 | v = num_zero; |
4412 | } |
4037 | } |
4413 | else |
4038 | else |
4414 | { |
4039 | { |
4415 | x = cdr (SCHEME_V->args); |
4040 | x = cdr (args); |
4416 | v = nvalue (car (SCHEME_V->args)); |
4041 | v = nvalue (car (args)); |
4417 | } |
4042 | } |
4418 | |
4043 | |
4419 | for (; x != NIL; x = cdr (x)) |
4044 | for (; x != NIL; x = cdr (x)) |
4420 | v = num_sub (v, nvalue (car (x))); |
4045 | v = num_op (NUM_SUB, v, nvalue (car (x))); |
4421 | |
4046 | |
4422 | s_return (mk_number (SCHEME_A_ v)); |
4047 | s_return (mk_number (SCHEME_A_ v)); |
4423 | |
4048 | |
4424 | case OP_DIV: /* / */ |
4049 | case OP_DIV: /* / */ |
4425 | if (cdr (SCHEME_V->args) == NIL) |
4050 | if (cdr (args) == NIL) |
4426 | { |
4051 | { |
4427 | x = SCHEME_V->args; |
4052 | x = args; |
4428 | v = num_one; |
4053 | v = num_one; |
4429 | } |
4054 | } |
4430 | else |
4055 | else |
4431 | { |
4056 | { |
4432 | x = cdr (SCHEME_V->args); |
4057 | x = cdr (args); |
4433 | v = nvalue (car (SCHEME_V->args)); |
4058 | v = nvalue (car (args)); |
4434 | } |
4059 | } |
4435 | |
4060 | |
4436 | for (; x != NIL; x = cdr (x)) |
4061 | for (; x != NIL; x = cdr (x)) |
4437 | { |
|
|
4438 | if (!is_zero_rvalue (rvalue (car (x)))) |
4062 | if (!is_zero_rvalue (rvalue (car (x)))) |
4439 | v = num_div (v, nvalue (car (x))); |
4063 | v = num_div (v, nvalue (car (x))); |
4440 | else |
4064 | else |
4441 | Error_0 ("/: division by zero"); |
4065 | Error_0 ("/: division by zero"); |
4442 | } |
|
|
4443 | |
4066 | |
4444 | s_return (mk_number (SCHEME_A_ v)); |
4067 | s_return (mk_number (SCHEME_A_ v)); |
4445 | |
4068 | |
4446 | case OP_INTDIV: /* quotient */ |
4069 | case OP_INTDIV: /* quotient */ |
4447 | if (cdr (SCHEME_V->args) == NIL) |
4070 | if (cdr (args) == NIL) |
4448 | { |
4071 | { |
4449 | x = SCHEME_V->args; |
4072 | x = args; |
4450 | v = num_one; |
4073 | v = num_one; |
4451 | } |
4074 | } |
4452 | else |
4075 | else |
4453 | { |
4076 | { |
4454 | x = cdr (SCHEME_V->args); |
4077 | x = cdr (args); |
4455 | v = nvalue (car (SCHEME_V->args)); |
4078 | v = nvalue (car (args)); |
4456 | } |
4079 | } |
4457 | |
4080 | |
4458 | for (; x != NIL; x = cdr (x)) |
4081 | for (; x != NIL; x = cdr (x)) |
4459 | { |
4082 | { |
4460 | if (ivalue (car (x)) != 0) |
4083 | if (ivalue (car (x)) != 0) |
4461 | v = num_intdiv (v, nvalue (car (x))); |
4084 | v = num_op (NUM_INTDIV, v, nvalue (car (x))); |
4462 | else |
4085 | else |
4463 | Error_0 ("quotient: division by zero"); |
4086 | Error_0 ("quotient: division by zero"); |
4464 | } |
4087 | } |
4465 | |
4088 | |
4466 | s_return (mk_number (SCHEME_A_ v)); |
4089 | s_return (mk_number (SCHEME_A_ v)); |
4467 | |
4090 | |
4468 | case OP_REM: /* remainder */ |
4091 | case OP_REM: /* remainder */ |
4469 | v = nvalue (car (SCHEME_V->args)); |
4092 | v = nvalue (x); |
4470 | |
4093 | |
4471 | if (ivalue (cadr (SCHEME_V->args)) != 0) |
4094 | if (ivalue (cadr (args)) != 0) |
4472 | v = num_rem (v, nvalue (cadr (SCHEME_V->args))); |
4095 | v = num_rem (v, nvalue (cadr (args))); |
4473 | else |
4096 | else |
4474 | Error_0 ("remainder: division by zero"); |
4097 | Error_0 ("remainder: division by zero"); |
4475 | |
4098 | |
4476 | s_return (mk_number (SCHEME_A_ v)); |
4099 | s_return (mk_number (SCHEME_A_ v)); |
4477 | |
4100 | |
4478 | case OP_MOD: /* modulo */ |
4101 | case OP_MOD: /* modulo */ |
4479 | v = nvalue (car (SCHEME_V->args)); |
4102 | v = nvalue (x); |
4480 | |
4103 | |
4481 | if (ivalue (cadr (SCHEME_V->args)) != 0) |
4104 | if (ivalue (cadr (args)) != 0) |
4482 | v = num_mod (v, nvalue (cadr (SCHEME_V->args))); |
4105 | v = num_mod (v, nvalue (cadr (args))); |
4483 | else |
4106 | else |
4484 | Error_0 ("modulo: division by zero"); |
4107 | Error_0 ("modulo: division by zero"); |
4485 | |
4108 | |
4486 | s_return (mk_number (SCHEME_A_ v)); |
4109 | s_return (mk_number (SCHEME_A_ v)); |
4487 | |
4110 | |
4488 | case OP_CAR: /* car */ |
4111 | case OP_CAR: /* car */ |
4489 | s_return (caar (SCHEME_V->args)); |
4112 | s_return (caar (args)); |
4490 | |
4113 | |
4491 | case OP_CDR: /* cdr */ |
4114 | case OP_CDR: /* cdr */ |
4492 | s_return (cdar (SCHEME_V->args)); |
4115 | s_return (cdar (args)); |
4493 | |
4116 | |
4494 | case OP_CONS: /* cons */ |
4117 | case OP_CONS: /* cons */ |
4495 | set_cdr (SCHEME_V->args, cadr (SCHEME_V->args)); |
4118 | set_cdr (args, cadr (args)); |
4496 | s_return (SCHEME_V->args); |
4119 | s_return (args); |
4497 | |
4120 | |
4498 | case OP_SETCAR: /* set-car! */ |
4121 | case OP_SETCAR: /* set-car! */ |
4499 | if (!is_immutable (car (SCHEME_V->args))) |
4122 | if (!is_immutable (x)) |
4500 | { |
4123 | { |
4501 | set_car (car (SCHEME_V->args), cadr (SCHEME_V->args)); |
4124 | set_car (x, cadr (args)); |
4502 | s_return (car (SCHEME_V->args)); |
4125 | s_return (car (args)); |
4503 | } |
4126 | } |
4504 | else |
4127 | else |
4505 | Error_0 ("set-car!: unable to alter immutable pair"); |
4128 | Error_0 ("set-car!: unable to alter immutable pair"); |
4506 | |
4129 | |
4507 | case OP_SETCDR: /* set-cdr! */ |
4130 | case OP_SETCDR: /* set-cdr! */ |
4508 | if (!is_immutable (car (SCHEME_V->args))) |
4131 | if (!is_immutable (x)) |
4509 | { |
4132 | { |
4510 | set_cdr (car (SCHEME_V->args), cadr (SCHEME_V->args)); |
4133 | set_cdr (x, cadr (args)); |
4511 | s_return (car (SCHEME_V->args)); |
4134 | s_return (car (args)); |
4512 | } |
4135 | } |
4513 | else |
4136 | else |
4514 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4137 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4515 | |
4138 | |
4516 | case OP_CHAR2INT: /* char->integer */ |
4139 | case OP_CHAR2INT: /* char->integer */ |
4517 | { |
|
|
4518 | char c; |
|
|
4519 | |
|
|
4520 | c = (char) ivalue (car (SCHEME_V->args)); |
|
|
4521 | s_return (mk_integer (SCHEME_A_ (unsigned char) c)); |
4140 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4522 | } |
|
|
4523 | |
4141 | |
4524 | case OP_INT2CHAR: /* integer->char */ |
4142 | case OP_INT2CHAR: /* integer->char */ |
4525 | { |
|
|
4526 | unsigned char c; |
|
|
4527 | |
|
|
4528 | c = (unsigned char) ivalue (car (SCHEME_V->args)); |
|
|
4529 | s_return (mk_character (SCHEME_A_ (char) c)); |
4143 | s_return (mk_character (SCHEME_A_ ivalue (x))); |
4530 | } |
|
|
4531 | |
4144 | |
4532 | case OP_CHARUPCASE: |
4145 | case OP_CHARUPCASE: |
4533 | { |
4146 | { |
4534 | unsigned char c; |
4147 | unsigned char c = ivalue (x); |
4535 | |
|
|
4536 | c = (unsigned char) ivalue (car (SCHEME_V->args)); |
|
|
4537 | c = toupper (c); |
4148 | c = toupper (c); |
4538 | s_return (mk_character (SCHEME_A_ (char) c)); |
4149 | s_return (mk_character (SCHEME_A_ c)); |
4539 | } |
4150 | } |
4540 | |
4151 | |
4541 | case OP_CHARDNCASE: |
4152 | case OP_CHARDNCASE: |
4542 | { |
4153 | { |
4543 | unsigned char c; |
4154 | unsigned char c = ivalue (x); |
4544 | |
|
|
4545 | c = (unsigned char) ivalue (car (SCHEME_V->args)); |
|
|
4546 | c = tolower (c); |
4155 | c = tolower (c); |
4547 | s_return (mk_character (SCHEME_A_ (char) c)); |
4156 | s_return (mk_character (SCHEME_A_ c)); |
4548 | } |
4157 | } |
4549 | |
4158 | |
4550 | case OP_STR2SYM: /* string->symbol */ |
4159 | case OP_STR2SYM: /* string->symbol */ |
4551 | s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); |
4160 | s_return (mk_symbol (SCHEME_A_ strvalue (x))); |
4552 | |
4161 | |
4553 | case OP_STR2ATOM: /* string->atom */ |
4162 | case OP_STR2ATOM: /* string->atom */ |
4554 | { |
4163 | { |
4555 | char *s = strvalue (car (SCHEME_V->args)); |
4164 | char *s = strvalue (x); |
4556 | long pf = 0; |
4165 | long pf = 0; |
4557 | |
4166 | |
4558 | if (cdr (SCHEME_V->args) != NIL) |
4167 | if (cdr (args) != NIL) |
4559 | { |
4168 | { |
4560 | /* we know cadr(SCHEME_V->args) is a natural number */ |
4169 | /* we know cadr(args) is a natural number */ |
4561 | /* see if it is 2, 8, 10, or 16, or error */ |
4170 | /* see if it is 2, 8, 10, or 16, or error */ |
4562 | pf = ivalue_unchecked (cadr (SCHEME_V->args)); |
4171 | pf = ivalue_unchecked (cadr (args)); |
4563 | |
4172 | |
4564 | if (pf == 16 || pf == 10 || pf == 8 || pf == 2) |
4173 | if (pf == 16 || pf == 10 || pf == 8 || pf == 2) |
4565 | { |
4174 | { |
4566 | /* base is OK */ |
4175 | /* base is OK */ |
4567 | } |
4176 | } |
4568 | else |
4177 | else |
4569 | { |
|
|
4570 | pf = -1; |
4178 | pf = -1; |
4571 | } |
|
|
4572 | } |
4179 | } |
4573 | |
4180 | |
4574 | if (pf < 0) |
4181 | if (pf < 0) |
4575 | { |
|
|
4576 | Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); |
4182 | Error_1 ("string->atom: bad base:", cadr (args)); |
4577 | } |
|
|
4578 | else if (*s == '#') /* no use of base! */ |
4183 | else if (*s == '#') /* no use of base! */ |
4579 | { |
|
|
4580 | s_return (mk_sharp_const (SCHEME_A_ s + 1)); |
4184 | s_return (mk_sharp_const (SCHEME_A_ s + 1)); |
4581 | } |
|
|
4582 | else |
4185 | else |
4583 | { |
4186 | { |
4584 | if (pf == 0 || pf == 10) |
4187 | if (pf == 0 || pf == 10) |
4585 | { |
|
|
4586 | s_return (mk_atom (SCHEME_A_ s)); |
4188 | s_return (mk_atom (SCHEME_A_ s)); |
4587 | } |
|
|
4588 | else |
4189 | else |
4589 | { |
4190 | { |
4590 | char *ep; |
4191 | char *ep; |
4591 | long iv = strtol (s, &ep, (int) pf); |
4192 | long iv = strtol (s, &ep, (int) pf); |
4592 | |
4193 | |
4593 | if (*ep == 0) |
4194 | if (*ep == 0) |
4594 | { |
|
|
4595 | s_return (mk_integer (SCHEME_A_ iv)); |
4195 | s_return (mk_integer (SCHEME_A_ iv)); |
4596 | } |
|
|
4597 | else |
4196 | else |
4598 | { |
|
|
4599 | s_return (S_F); |
4197 | s_return (S_F); |
4600 | } |
|
|
4601 | } |
4198 | } |
4602 | } |
4199 | } |
4603 | } |
4200 | } |
4604 | |
4201 | |
4605 | case OP_SYM2STR: /* symbol->string */ |
4202 | case OP_SYM2STR: /* symbol->string */ |
4606 | x = mk_string (SCHEME_A_ symname (car (SCHEME_V->args))); |
4203 | x = mk_string (SCHEME_A_ symname (x)); |
4607 | setimmutable (x); |
4204 | setimmutable (x); |
4608 | s_return (x); |
4205 | s_return (x); |
4609 | |
4206 | |
4610 | case OP_ATOM2STR: /* atom->string */ |
4207 | case OP_ATOM2STR: /* atom->string */ |
4611 | { |
4208 | { |
4612 | long pf = 0; |
4209 | long pf = 0; |
4613 | |
4210 | |
4614 | x = car (SCHEME_V->args); |
|
|
4615 | |
|
|
4616 | if (cdr (SCHEME_V->args) != NIL) |
4211 | if (cdr (args) != NIL) |
4617 | { |
4212 | { |
4618 | /* we know cadr(SCHEME_V->args) is a natural number */ |
4213 | /* we know cadr(args) is a natural number */ |
4619 | /* see if it is 2, 8, 10, or 16, or error */ |
4214 | /* see if it is 2, 8, 10, or 16, or error */ |
4620 | pf = ivalue_unchecked (cadr (SCHEME_V->args)); |
4215 | pf = ivalue_unchecked (cadr (args)); |
4621 | |
4216 | |
4622 | if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) |
4217 | if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) |
4623 | { |
4218 | { |
4624 | /* base is OK */ |
4219 | /* base is OK */ |
4625 | } |
4220 | } |
4626 | else |
4221 | else |
4627 | { |
|
|
4628 | pf = -1; |
4222 | pf = -1; |
4629 | } |
|
|
4630 | } |
4223 | } |
4631 | |
4224 | |
4632 | if (pf < 0) |
4225 | if (pf < 0) |
4633 | { |
|
|
4634 | Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args)); |
4226 | Error_1 ("atom->string: bad base:", cadr (args)); |
4635 | } |
|
|
4636 | else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) |
4227 | else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) |
4637 | { |
4228 | { |
4638 | char *p; |
4229 | char *p; |
4639 | int len; |
4230 | int len; |
4640 | |
4231 | |
4641 | atom2str (SCHEME_A_ x, (int) pf, &p, &len); |
4232 | atom2str (SCHEME_A_ x, pf, &p, &len); |
4642 | s_return (mk_counted_string (SCHEME_A_ p, len)); |
4233 | s_return (mk_counted_string (SCHEME_A_ p, len)); |
4643 | } |
4234 | } |
4644 | else |
4235 | else |
4645 | { |
|
|
4646 | Error_1 ("atom->string: not an atom:", x); |
4236 | Error_1 ("atom->string: not an atom:", x); |
4647 | } |
|
|
4648 | } |
4237 | } |
4649 | |
4238 | |
4650 | case OP_MKSTRING: /* make-string */ |
4239 | case OP_MKSTRING: /* make-string */ |
4651 | { |
4240 | { |
4652 | int fill = ' '; |
4241 | int fill = ' '; |
4653 | int len; |
4242 | int len; |
4654 | |
4243 | |
4655 | len = ivalue (car (SCHEME_V->args)); |
4244 | len = ivalue (x); |
4656 | |
4245 | |
4657 | if (cdr (SCHEME_V->args) != NIL) |
4246 | if (cdr (args) != NIL) |
4658 | { |
|
|
4659 | fill = charvalue (cadr (SCHEME_V->args)); |
4247 | fill = charvalue (cadr (args)); |
4660 | } |
|
|
4661 | |
4248 | |
4662 | s_return (mk_empty_string (SCHEME_A_ len, (char) fill)); |
4249 | s_return (mk_empty_string (SCHEME_A_ len, fill)); |
4663 | } |
4250 | } |
4664 | |
4251 | |
4665 | case OP_STRLEN: /* string-length */ |
4252 | case OP_STRLEN: /* string-length */ |
4666 | s_return (mk_integer (SCHEME_A_ strlength (car (SCHEME_V->args)))); |
4253 | s_return (mk_integer (SCHEME_A_ strlength (x))); |
4667 | |
4254 | |
4668 | case OP_STRREF: /* string-ref */ |
4255 | case OP_STRREF: /* string-ref */ |
4669 | { |
4256 | { |
4670 | char *str; |
4257 | char *str; |
4671 | int index; |
4258 | int index; |
4672 | |
4259 | |
4673 | str = strvalue (car (SCHEME_V->args)); |
4260 | str = strvalue (x); |
4674 | |
4261 | |
4675 | index = ivalue (cadr (SCHEME_V->args)); |
4262 | index = ivalue (cadr (args)); |
4676 | |
4263 | |
4677 | if (index >= strlength (car (SCHEME_V->args))) |
4264 | if (index >= strlength (x)) |
4678 | { |
|
|
4679 | Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); |
4265 | Error_1 ("string-ref: out of bounds:", cadr (args)); |
4680 | } |
|
|
4681 | |
4266 | |
4682 | s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); |
4267 | s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); |
4683 | } |
4268 | } |
4684 | |
4269 | |
4685 | case OP_STRSET: /* string-set! */ |
4270 | case OP_STRSET: /* string-set! */ |
4686 | { |
4271 | { |
4687 | char *str; |
4272 | char *str; |
4688 | int index; |
4273 | int index; |
4689 | int c; |
4274 | int c; |
4690 | |
4275 | |
4691 | if (is_immutable (car (SCHEME_V->args))) |
4276 | if (is_immutable (x)) |
4692 | { |
|
|
4693 | Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); |
4277 | Error_1 ("string-set!: unable to alter immutable string:", x); |
4694 | } |
|
|
4695 | |
4278 | |
4696 | str = strvalue (car (SCHEME_V->args)); |
4279 | str = strvalue (x); |
4697 | |
4280 | |
4698 | index = ivalue (cadr (SCHEME_V->args)); |
4281 | index = ivalue (cadr (args)); |
4699 | |
4282 | |
4700 | if (index >= strlength (car (SCHEME_V->args))) |
4283 | if (index >= strlength (x)) |
4701 | { |
|
|
4702 | Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); |
4284 | Error_1 ("string-set!: out of bounds:", cadr (args)); |
4703 | } |
|
|
4704 | |
4285 | |
4705 | c = charvalue (caddr (SCHEME_V->args)); |
4286 | c = charvalue (caddr (args)); |
4706 | |
4287 | |
4707 | str[index] = (char) c; |
4288 | str[index] = c; |
4708 | s_return (car (SCHEME_V->args)); |
4289 | s_return (car (args)); |
4709 | } |
4290 | } |
4710 | |
4291 | |
4711 | case OP_STRAPPEND: /* string-append */ |
4292 | case OP_STRAPPEND: /* string-append */ |
4712 | { |
4293 | { |
4713 | /* in 1.29 string-append was in Scheme in init.scm but was too slow */ |
4294 | /* in 1.29 string-append was in Scheme in init.scm but was too slow */ |
4714 | int len = 0; |
4295 | int len = 0; |
4715 | pointer newstr; |
4296 | pointer newstr; |
4716 | char *pos; |
4297 | char *pos; |
4717 | |
4298 | |
4718 | /* compute needed length for new string */ |
4299 | /* compute needed length for new string */ |
4719 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4300 | for (x = args; x != NIL; x = cdr (x)) |
4720 | { |
|
|
4721 | len += strlength (car (x)); |
4301 | len += strlength (car (x)); |
4722 | } |
|
|
4723 | |
4302 | |
4724 | newstr = mk_empty_string (SCHEME_A_ len, ' '); |
4303 | newstr = mk_empty_string (SCHEME_A_ len, ' '); |
4725 | |
4304 | |
4726 | /* store the contents of the argument strings into the new string */ |
4305 | /* store the contents of the argument strings into the new string */ |
4727 | for (pos = strvalue (newstr), x = SCHEME_V->args; x != NIL; pos += strlength (car (x)), x = cdr (x)) |
4306 | for (pos = strvalue (newstr), x = args; x != NIL; pos += strlength (car (x)), x = cdr (x)) |
4728 | { |
|
|
4729 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4307 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4730 | } |
|
|
4731 | |
4308 | |
4732 | s_return (newstr); |
4309 | s_return (newstr); |
4733 | } |
4310 | } |
4734 | |
4311 | |
4735 | case OP_SUBSTR: /* substring */ |
4312 | case OP_SUBSTR: /* substring */ |
… | |
… | |
4737 | char *str; |
4314 | char *str; |
4738 | int index0; |
4315 | int index0; |
4739 | int index1; |
4316 | int index1; |
4740 | int len; |
4317 | int len; |
4741 | |
4318 | |
4742 | str = strvalue (car (SCHEME_V->args)); |
4319 | str = strvalue (x); |
4743 | |
4320 | |
4744 | index0 = ivalue (cadr (SCHEME_V->args)); |
4321 | index0 = ivalue (cadr (args)); |
4745 | |
4322 | |
4746 | if (index0 > strlength (car (SCHEME_V->args))) |
4323 | if (index0 > strlength (x)) |
|
|
4324 | Error_1 ("substring: start out of bounds:", cadr (args)); |
|
|
4325 | |
|
|
4326 | if (cddr (args) != NIL) |
4747 | { |
4327 | { |
4748 | Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args)); |
|
|
4749 | } |
|
|
4750 | |
|
|
4751 | if (cddr (SCHEME_V->args) != NIL) |
|
|
4752 | { |
|
|
4753 | index1 = ivalue (caddr (SCHEME_V->args)); |
4328 | index1 = ivalue (caddr (args)); |
4754 | |
4329 | |
4755 | if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) |
4330 | if (index1 > strlength (x) || index1 < index0) |
4756 | { |
|
|
4757 | Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); |
4331 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4758 | } |
|
|
4759 | } |
4332 | } |
4760 | else |
4333 | else |
4761 | { |
4334 | index1 = strlength (x); |
4762 | index1 = strlength (car (SCHEME_V->args)); |
|
|
4763 | } |
|
|
4764 | |
4335 | |
4765 | len = index1 - index0; |
4336 | len = index1 - index0; |
4766 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4337 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4767 | memcpy (strvalue (x), str + index0, len); |
4338 | memcpy (strvalue (x), str + index0, len); |
4768 | strvalue (x)[len] = 0; |
4339 | strvalue (x)[len] = 0; |
… | |
… | |
4772 | |
4343 | |
4773 | case OP_VECTOR: /* vector */ |
4344 | case OP_VECTOR: /* vector */ |
4774 | { |
4345 | { |
4775 | int i; |
4346 | int i; |
4776 | pointer vec; |
4347 | pointer vec; |
4777 | int len = list_length (SCHEME_A_ SCHEME_V->args); |
4348 | int len = list_length (SCHEME_A_ args); |
4778 | |
4349 | |
4779 | if (len < 0) |
4350 | if (len < 0) |
4780 | { |
|
|
4781 | Error_1 ("vector: not a proper list:", SCHEME_V->args); |
4351 | Error_1 ("vector: not a proper list:", args); |
4782 | } |
|
|
4783 | |
4352 | |
4784 | vec = mk_vector (SCHEME_A_ len); |
4353 | vec = mk_vector (SCHEME_A_ len); |
4785 | |
4354 | |
4786 | #if USE_ERROR_CHECKING |
4355 | #if USE_ERROR_CHECKING |
4787 | if (SCHEME_V->no_memory) |
4356 | if (SCHEME_V->no_memory) |
4788 | s_return (S_SINK); |
4357 | s_return (S_SINK); |
4789 | #endif |
4358 | #endif |
4790 | |
4359 | |
4791 | for (x = SCHEME_V->args, i = 0; is_pair (x); x = cdr (x), i++) |
4360 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4792 | set_vector_elem (vec, i, car (x)); |
4361 | set_vector_elem (vec, i, car (x)); |
4793 | |
4362 | |
4794 | s_return (vec); |
4363 | s_return (vec); |
4795 | } |
4364 | } |
4796 | |
4365 | |
… | |
… | |
4798 | { |
4367 | { |
4799 | pointer fill = NIL; |
4368 | pointer fill = NIL; |
4800 | int len; |
4369 | int len; |
4801 | pointer vec; |
4370 | pointer vec; |
4802 | |
4371 | |
4803 | len = ivalue (car (SCHEME_V->args)); |
4372 | len = ivalue (x); |
4804 | |
4373 | |
4805 | if (cdr (SCHEME_V->args) != NIL) |
4374 | if (cdr (args) != NIL) |
4806 | fill = cadr (SCHEME_V->args); |
4375 | fill = cadr (args); |
4807 | |
4376 | |
4808 | vec = mk_vector (SCHEME_A_ len); |
4377 | vec = mk_vector (SCHEME_A_ len); |
4809 | |
4378 | |
4810 | #if USE_ERROR_CHECKING |
4379 | #if USE_ERROR_CHECKING |
4811 | if (SCHEME_V->no_memory) |
4380 | if (SCHEME_V->no_memory) |
… | |
… | |
4817 | |
4386 | |
4818 | s_return (vec); |
4387 | s_return (vec); |
4819 | } |
4388 | } |
4820 | |
4389 | |
4821 | case OP_VECLEN: /* vector-length */ |
4390 | case OP_VECLEN: /* vector-length */ |
4822 | s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); |
4391 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4823 | |
4392 | |
4824 | case OP_VECREF: /* vector-ref */ |
4393 | case OP_VECREF: /* vector-ref */ |
4825 | { |
4394 | { |
4826 | int index; |
4395 | int index; |
4827 | |
4396 | |
4828 | index = ivalue (cadr (SCHEME_V->args)); |
4397 | index = ivalue (cadr (args)); |
4829 | |
4398 | |
4830 | if (index >= ivalue (car (SCHEME_V->args))) |
4399 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4831 | { |
|
|
4832 | Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); |
4400 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4833 | } |
|
|
4834 | |
4401 | |
4835 | s_return (vector_elem (car (SCHEME_V->args), index)); |
4402 | s_return (vector_elem (x, index)); |
4836 | } |
4403 | } |
4837 | |
4404 | |
4838 | case OP_VECSET: /* vector-set! */ |
4405 | case OP_VECSET: /* vector-set! */ |
4839 | { |
4406 | { |
4840 | int index; |
4407 | int index; |
4841 | |
4408 | |
4842 | if (is_immutable (car (SCHEME_V->args))) |
4409 | if (is_immutable (x)) |
4843 | { |
|
|
4844 | Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); |
4410 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4845 | } |
|
|
4846 | |
4411 | |
4847 | index = ivalue (cadr (SCHEME_V->args)); |
4412 | index = ivalue (cadr (args)); |
4848 | |
4413 | |
4849 | if (index >= ivalue (car (SCHEME_V->args))) |
4414 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4850 | { |
|
|
4851 | Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); |
4415 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4852 | } |
|
|
4853 | |
4416 | |
4854 | set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); |
4417 | set_vector_elem (x, index, caddr (args)); |
4855 | s_return (car (SCHEME_V->args)); |
4418 | s_return (x); |
4856 | } |
4419 | } |
4857 | } |
4420 | } |
4858 | |
4421 | |
4859 | return S_T; |
4422 | if (USE_ERROR_CHECKING) abort (); |
4860 | } |
4423 | } |
4861 | |
4424 | |
4862 | INTERFACE int |
4425 | INTERFACE int |
4863 | is_list (SCHEME_P_ pointer a) |
4426 | is_list (SCHEME_P_ pointer a) |
4864 | { |
4427 | { |
… | |
… | |
4911 | return -1; |
4474 | return -1; |
4912 | } |
4475 | } |
4913 | } |
4476 | } |
4914 | } |
4477 | } |
4915 | |
4478 | |
4916 | static pointer |
4479 | static int |
|
|
4480 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
|
|
4481 | { |
|
|
4482 | pointer x = SCHEME_V->args; |
|
|
4483 | |
|
|
4484 | for (;;) |
|
|
4485 | { |
|
|
4486 | num v = nvalue (car (x)); |
|
|
4487 | x = cdr (x); |
|
|
4488 | |
|
|
4489 | if (x == NIL) |
|
|
4490 | break; |
|
|
4491 | |
|
|
4492 | int r = num_cmp (v, nvalue (car (x))); |
|
|
4493 | |
|
|
4494 | switch (op) |
|
|
4495 | { |
|
|
4496 | case OP_NUMEQ: r = r == 0; break; |
|
|
4497 | case OP_LESS: r = r < 0; break; |
|
|
4498 | case OP_GRE: r = r > 0; break; |
|
|
4499 | case OP_LEQ: r = r <= 0; break; |
|
|
4500 | case OP_GEQ: r = r >= 0; break; |
|
|
4501 | } |
|
|
4502 | |
|
|
4503 | if (!r) |
|
|
4504 | s_return (S_F); |
|
|
4505 | } |
|
|
4506 | |
|
|
4507 | s_return (S_T); |
|
|
4508 | } |
|
|
4509 | |
|
|
4510 | static int |
4917 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4511 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4918 | { |
4512 | { |
4919 | pointer x; |
4513 | pointer args = SCHEME_V->args; |
4920 | num v; |
4514 | pointer a = car (args); |
4921 | int (*comp_func) (num, num); |
4515 | pointer d = cdr (args); |
|
|
4516 | int r; |
4922 | |
4517 | |
4923 | switch (op) |
4518 | switch (op) |
4924 | { |
4519 | { |
4925 | case OP_NOT: /* not */ |
4520 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4926 | s_retbool (is_false (car (SCHEME_V->args))); |
4521 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; |
|
|
4522 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
|
|
4523 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
|
|
4524 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
|
|
4525 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
|
|
4526 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
|
|
4527 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
|
|
4528 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
|
|
4529 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4927 | |
4530 | |
4928 | case OP_BOOLP: /* boolean? */ |
|
|
4929 | s_retbool (car (SCHEME_V->args) == S_F || car (SCHEME_V->args) == S_T); |
|
|
4930 | |
|
|
4931 | case OP_EOFOBJP: /* boolean? */ |
|
|
4932 | s_retbool (car (SCHEME_V->args) == S_EOF); |
|
|
4933 | |
|
|
4934 | case OP_NULLP: /* null? */ |
|
|
4935 | s_retbool (car (SCHEME_V->args) == NIL); |
|
|
4936 | |
|
|
4937 | case OP_NUMEQ: /* = */ |
|
|
4938 | case OP_LESS: /* < */ |
|
|
4939 | case OP_GRE: /* > */ |
|
|
4940 | case OP_LEQ: /* <= */ |
|
|
4941 | case OP_GEQ: /* >= */ |
|
|
4942 | switch (op) |
|
|
4943 | { |
|
|
4944 | case OP_NUMEQ: |
|
|
4945 | comp_func = num_eq; |
|
|
4946 | break; |
|
|
4947 | |
|
|
4948 | case OP_LESS: |
|
|
4949 | comp_func = num_lt; |
|
|
4950 | break; |
|
|
4951 | |
|
|
4952 | case OP_GRE: |
|
|
4953 | comp_func = num_gt; |
|
|
4954 | break; |
|
|
4955 | |
|
|
4956 | case OP_LEQ: |
|
|
4957 | comp_func = num_le; |
|
|
4958 | break; |
|
|
4959 | |
|
|
4960 | case OP_GEQ: |
|
|
4961 | comp_func = num_ge; |
|
|
4962 | break; |
|
|
4963 | } |
|
|
4964 | |
|
|
4965 | x = SCHEME_V->args; |
|
|
4966 | v = nvalue (car (x)); |
|
|
4967 | x = cdr (x); |
|
|
4968 | |
|
|
4969 | for (; x != NIL; x = cdr (x)) |
|
|
4970 | { |
|
|
4971 | if (!comp_func (v, nvalue (car (x)))) |
|
|
4972 | { |
|
|
4973 | s_retbool (0); |
|
|
4974 | } |
|
|
4975 | |
|
|
4976 | v = nvalue (car (x)); |
|
|
4977 | } |
|
|
4978 | |
|
|
4979 | s_retbool (1); |
|
|
4980 | |
|
|
4981 | case OP_SYMBOLP: /* symbol? */ |
|
|
4982 | s_retbool (is_symbol (car (SCHEME_V->args))); |
|
|
4983 | |
|
|
4984 | case OP_NUMBERP: /* number? */ |
|
|
4985 | s_retbool (is_number (car (SCHEME_V->args))); |
|
|
4986 | |
|
|
4987 | case OP_STRINGP: /* string? */ |
|
|
4988 | s_retbool (is_string (car (SCHEME_V->args))); |
|
|
4989 | |
|
|
4990 | case OP_INTEGERP: /* integer? */ |
|
|
4991 | s_retbool (is_integer (car (SCHEME_V->args))); |
|
|
4992 | |
|
|
4993 | case OP_REALP: /* real? */ |
|
|
4994 | s_retbool (is_number (car (SCHEME_V->args))); /* All numbers are real */ |
|
|
4995 | |
|
|
4996 | case OP_CHARP: /* char? */ |
|
|
4997 | s_retbool (is_character (car (SCHEME_V->args))); |
|
|
4998 | #if USE_CHAR_CLASSIFIERS |
4531 | #if USE_CHAR_CLASSIFIERS |
4999 | |
|
|
5000 | case OP_CHARAP: /* char-alphabetic? */ |
4532 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; |
5001 | s_retbool (Cisalpha (ivalue (car (SCHEME_V->args)))); |
4533 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; |
5002 | |
|
|
5003 | case OP_CHARNP: /* char-numeric? */ |
|
|
5004 | s_retbool (Cisdigit (ivalue (car (SCHEME_V->args)))); |
|
|
5005 | |
|
|
5006 | case OP_CHARWP: /* char-whitespace? */ |
4534 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; |
5007 | s_retbool (Cisspace (ivalue (car (SCHEME_V->args)))); |
|
|
5008 | |
|
|
5009 | case OP_CHARUP: /* char-upper-case? */ |
4535 | case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; |
5010 | s_retbool (Cisupper (ivalue (car (SCHEME_V->args)))); |
|
|
5011 | |
|
|
5012 | case OP_CHARLP: /* char-lower-case? */ |
4536 | case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; |
5013 | s_retbool (Cislower (ivalue (car (SCHEME_V->args)))); |
|
|
5014 | #endif |
4537 | #endif |
|
|
4538 | |
5015 | #if USE_PORTS |
4539 | #if USE_PORTS |
5016 | |
4540 | case OP_PORTP: /* port? */ r = is_port (a) ; break; |
5017 | case OP_PORTP: /* port? */ |
|
|
5018 | s_retbool (is_port (car (SCHEME_V->args))); |
|
|
5019 | |
|
|
5020 | case OP_INPORTP: /* input-port? */ |
4541 | case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; |
5021 | s_retbool (is_inport (car (SCHEME_V->args))); |
|
|
5022 | |
|
|
5023 | case OP_OUTPORTP: /* output-port? */ |
4542 | case OP_OUTPORTP: /* output-port? */ r = is_outport (a); break; |
5024 | s_retbool (is_outport (car (SCHEME_V->args))); |
|
|
5025 | #endif |
4543 | #endif |
5026 | |
4544 | |
5027 | case OP_PROCP: /* procedure? */ |
4545 | case OP_PROCP: /* procedure? */ |
5028 | |
4546 | |
5029 | /*-- |
4547 | /*-- |
5030 | * continuation should be procedure by the example |
4548 | * continuation should be procedure by the example |
5031 | * (call-with-current-continuation procedure?) ==> #t |
4549 | * (call-with-current-continuation procedure?) ==> #t |
5032 | * in R^3 report sec. 6.9 |
4550 | * in R^3 report sec. 6.9 |
5033 | */ |
4551 | */ |
5034 | s_retbool (is_proc (car (SCHEME_V->args)) || is_closure (car (SCHEME_V->args)) |
4552 | r = is_proc (a) || is_closure (a) || is_continuation (a) || is_foreign (a); |
5035 | || is_continuation (car (SCHEME_V->args)) || is_foreign (car (SCHEME_V->args))); |
4553 | break; |
5036 | |
4554 | |
5037 | case OP_PAIRP: /* pair? */ |
4555 | case OP_PAIRP: /* pair? */ r = is_pair (a) ; break; |
5038 | s_retbool (is_pair (car (SCHEME_V->args))); |
4556 | case OP_LISTP: /* list? */ r = list_length (SCHEME_A_ a) >= 0; break; |
5039 | |
4557 | case OP_ENVP: /* environment? */ r = is_environment (a) ; break; |
5040 | case OP_LISTP: /* list? */ |
4558 | case OP_VECTORP: /* vector? */ r = is_vector (a) ; break; |
5041 | s_retbool (list_length (SCHEME_A_ car (SCHEME_V->args)) >= 0); |
4559 | case OP_EQ: /* eq? */ r = a == cadr (args) ; break; |
5042 | |
4560 | case OP_EQV: /* eqv? */ r = eqv (a, cadr (args)) ; break; |
5043 | case OP_ENVP: /* environment? */ |
|
|
5044 | s_retbool (is_environment (car (SCHEME_V->args))); |
|
|
5045 | |
|
|
5046 | case OP_VECTORP: /* vector? */ |
|
|
5047 | s_retbool (is_vector (car (SCHEME_V->args))); |
|
|
5048 | |
|
|
5049 | case OP_EQ: /* eq? */ |
|
|
5050 | s_retbool (car (SCHEME_V->args) == cadr (SCHEME_V->args)); |
|
|
5051 | |
|
|
5052 | case OP_EQV: /* eqv? */ |
|
|
5053 | s_retbool (eqv (car (SCHEME_V->args), cadr (SCHEME_V->args))); |
|
|
5054 | } |
4561 | } |
5055 | |
4562 | |
5056 | return S_T; |
4563 | s_retbool (r); |
5057 | } |
4564 | } |
5058 | |
4565 | |
5059 | static pointer |
4566 | static int |
5060 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4567 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
5061 | { |
4568 | { |
|
|
4569 | pointer args = SCHEME_V->args; |
|
|
4570 | pointer a = car (args); |
5062 | pointer x, y; |
4571 | pointer x, y; |
5063 | |
4572 | |
5064 | switch (op) |
4573 | switch (op) |
5065 | { |
4574 | { |
5066 | case OP_FORCE: /* force */ |
4575 | case OP_FORCE: /* force */ |
5067 | SCHEME_V->code = car (SCHEME_V->args); |
4576 | SCHEME_V->code = a; |
5068 | |
4577 | |
5069 | if (is_promise (SCHEME_V->code)) |
4578 | if (is_promise (SCHEME_V->code)) |
5070 | { |
4579 | { |
5071 | /* Should change type to closure here */ |
4580 | /* Should change type to closure here */ |
5072 | s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); |
4581 | s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); |
5073 | SCHEME_V->args = NIL; |
4582 | SCHEME_V->args = NIL; |
5074 | s_goto (OP_APPLY); |
4583 | s_goto (OP_APPLY); |
5075 | } |
4584 | } |
5076 | else |
4585 | else |
5077 | { |
|
|
5078 | s_return (SCHEME_V->code); |
4586 | s_return (SCHEME_V->code); |
5079 | } |
|
|
5080 | |
4587 | |
5081 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4588 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
5082 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4589 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
5083 | s_return (SCHEME_V->value); |
4590 | s_return (SCHEME_V->value); |
5084 | |
4591 | |
… | |
… | |
5095 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4602 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
5096 | SCHEME_V->outport = cadr (SCHEME_V->args); |
4603 | SCHEME_V->outport = cadr (SCHEME_V->args); |
5097 | } |
4604 | } |
5098 | } |
4605 | } |
5099 | |
4606 | |
5100 | SCHEME_V->args = car (SCHEME_V->args); |
4607 | SCHEME_V->args = a; |
5101 | |
4608 | |
5102 | if (op == OP_WRITE) |
4609 | if (op == OP_WRITE) |
5103 | SCHEME_V->print_flag = 1; |
4610 | SCHEME_V->print_flag = 1; |
5104 | else |
4611 | else |
5105 | SCHEME_V->print_flag = 0; |
4612 | SCHEME_V->print_flag = 0; |
5106 | |
4613 | |
5107 | s_goto (OP_P0LIST); |
4614 | s_goto (OP_P0LIST); |
5108 | |
4615 | |
5109 | case OP_NEWLINE: /* newline */ |
4616 | case OP_NEWLINE: /* newline */ |
5110 | if (is_pair (SCHEME_V->args)) |
4617 | if (is_pair (args)) |
5111 | { |
4618 | { |
5112 | if (car (SCHEME_V->args) != SCHEME_V->outport) |
4619 | if (a != SCHEME_V->outport) |
5113 | { |
4620 | { |
5114 | x = cons (SCHEME_V->outport, NIL); |
4621 | x = cons (SCHEME_V->outport, NIL); |
5115 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4622 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
5116 | SCHEME_V->outport = car (SCHEME_V->args); |
4623 | SCHEME_V->outport = a; |
5117 | } |
4624 | } |
5118 | } |
4625 | } |
5119 | |
4626 | |
5120 | putstr (SCHEME_A_ "\n"); |
4627 | putstr (SCHEME_A_ "\n"); |
5121 | s_return (S_T); |
4628 | s_return (S_T); |
5122 | #endif |
4629 | #endif |
5123 | |
4630 | |
5124 | case OP_ERR0: /* error */ |
4631 | case OP_ERR0: /* error */ |
5125 | SCHEME_V->retcode = -1; |
4632 | SCHEME_V->retcode = -1; |
5126 | |
4633 | |
5127 | if (!is_string (car (SCHEME_V->args))) |
4634 | if (!is_string (a)) |
5128 | { |
4635 | { |
5129 | SCHEME_V->args = cons (mk_string (SCHEME_A_ " -- "), SCHEME_V->args); |
4636 | args = cons (mk_string (SCHEME_A_ " -- "), args); |
5130 | setimmutable (car (SCHEME_V->args)); |
4637 | setimmutable (car (args)); |
5131 | } |
4638 | } |
5132 | |
4639 | |
5133 | putstr (SCHEME_A_ "Error: "); |
4640 | putstr (SCHEME_A_ "Error: "); |
5134 | putstr (SCHEME_A_ strvalue (car (SCHEME_V->args))); |
4641 | putstr (SCHEME_A_ strvalue (car (args))); |
5135 | SCHEME_V->args = cdr (SCHEME_V->args); |
4642 | SCHEME_V->args = cdr (args); |
5136 | s_goto (OP_ERR1); |
4643 | s_goto (OP_ERR1); |
5137 | |
4644 | |
5138 | case OP_ERR1: /* error */ |
4645 | case OP_ERR1: /* error */ |
5139 | putstr (SCHEME_A_ " "); |
4646 | putstr (SCHEME_A_ " "); |
5140 | |
4647 | |
5141 | if (SCHEME_V->args != NIL) |
4648 | if (args != NIL) |
5142 | { |
4649 | { |
5143 | s_save (SCHEME_A_ OP_ERR1, cdr (SCHEME_V->args), NIL); |
4650 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
5144 | SCHEME_V->args = car (SCHEME_V->args); |
4651 | SCHEME_V->args = a; |
5145 | SCHEME_V->print_flag = 1; |
4652 | SCHEME_V->print_flag = 1; |
5146 | s_goto (OP_P0LIST); |
4653 | s_goto (OP_P0LIST); |
5147 | } |
4654 | } |
5148 | else |
4655 | else |
5149 | { |
4656 | { |
5150 | putstr (SCHEME_A_ "\n"); |
4657 | putstr (SCHEME_A_ "\n"); |
5151 | |
4658 | |
5152 | if (SCHEME_V->interactive_repl) |
4659 | if (SCHEME_V->interactive_repl) |
5153 | { |
|
|
5154 | s_goto (OP_T0LVL); |
4660 | s_goto (OP_T0LVL); |
5155 | } |
|
|
5156 | else |
4661 | else |
5157 | { |
|
|
5158 | return NIL; |
4662 | return -1; |
5159 | } |
|
|
5160 | } |
4663 | } |
5161 | |
4664 | |
5162 | case OP_REVERSE: /* reverse */ |
4665 | case OP_REVERSE: /* reverse */ |
5163 | s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); |
4666 | s_return (reverse (SCHEME_A_ a)); |
5164 | |
4667 | |
5165 | case OP_LIST_STAR: /* list* */ |
4668 | case OP_LIST_STAR: /* list* */ |
5166 | s_return (list_star (SCHEME_A_ SCHEME_V->args)); |
4669 | s_return (list_star (SCHEME_A_ SCHEME_V->args)); |
5167 | |
4670 | |
5168 | case OP_APPEND: /* append */ |
4671 | case OP_APPEND: /* append */ |
5169 | x = NIL; |
4672 | x = NIL; |
5170 | y = SCHEME_V->args; |
4673 | y = args; |
5171 | |
4674 | |
5172 | if (y == x) |
4675 | if (y == x) |
5173 | s_return (x); |
4676 | s_return (x); |
5174 | |
4677 | |
5175 | /* cdr() in the while condition is not a typo. If car() */ |
4678 | /* cdr() in the while condition is not a typo. If car() */ |
… | |
… | |
5186 | s_return (reverse_in_place (SCHEME_A_ car (y), x)); |
4689 | s_return (reverse_in_place (SCHEME_A_ car (y), x)); |
5187 | |
4690 | |
5188 | #if USE_PLIST |
4691 | #if USE_PLIST |
5189 | |
4692 | |
5190 | case OP_PUT: /* put */ |
4693 | case OP_PUT: /* put */ |
5191 | if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) |
4694 | if (!hasprop (a) || !hasprop (cadr (args))) |
5192 | { |
|
|
5193 | Error_0 ("illegal use of put"); |
4695 | Error_0 ("illegal use of put"); |
5194 | } |
|
|
5195 | |
4696 | |
5196 | for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) |
4697 | for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x)) |
5197 | { |
4698 | { |
5198 | if (caar (x) == y) |
4699 | if (caar (x) == y) |
5199 | { |
|
|
5200 | break; |
4700 | break; |
5201 | } |
|
|
5202 | } |
4701 | } |
5203 | |
4702 | |
5204 | if (x != NIL) |
4703 | if (x != NIL) |
5205 | cdar (x) = caddr (SCHEME_V->args); |
4704 | cdar (x) = caddr (args); |
5206 | else |
4705 | else |
5207 | symprop (car (SCHEME_V->args)) = cons (cons (y, caddr (SCHEME_V->args)), symprop (car (SCHEME_V->args))); |
4706 | symprop (a) = cons (cons (y, caddr (args)), symprop (a)); |
5208 | |
4707 | |
5209 | s_return (S_T); |
4708 | s_return (S_T); |
5210 | |
4709 | |
5211 | case OP_GET: /* get */ |
4710 | case OP_GET: /* get */ |
5212 | if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) |
4711 | if (!hasprop (a) || !hasprop (cadr (args))) |
5213 | Error_0 ("illegal use of get"); |
4712 | Error_0 ("illegal use of get"); |
5214 | |
4713 | |
5215 | for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) |
4714 | for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x)) |
5216 | if (caar (x) == y) |
4715 | if (caar (x) == y) |
5217 | break; |
4716 | break; |
5218 | |
4717 | |
5219 | if (x != NIL) |
4718 | if (x != NIL) |
5220 | s_return (cdar (x)); |
4719 | s_return (cdar (x)); |
… | |
… | |
5222 | s_return (NIL); |
4721 | s_return (NIL); |
5223 | |
4722 | |
5224 | #endif /* USE_PLIST */ |
4723 | #endif /* USE_PLIST */ |
5225 | |
4724 | |
5226 | case OP_QUIT: /* quit */ |
4725 | case OP_QUIT: /* quit */ |
5227 | if (is_pair (SCHEME_V->args)) |
4726 | if (is_pair (args)) |
5228 | SCHEME_V->retcode = ivalue (car (SCHEME_V->args)); |
4727 | SCHEME_V->retcode = ivalue (a); |
5229 | |
4728 | |
5230 | return NIL; |
4729 | return -1; |
5231 | |
4730 | |
5232 | case OP_GC: /* gc */ |
4731 | case OP_GC: /* gc */ |
5233 | gc (SCHEME_A_ NIL, NIL); |
4732 | gc (SCHEME_A_ NIL, NIL); |
5234 | s_return (S_T); |
4733 | s_return (S_T); |
5235 | |
4734 | |
5236 | case OP_GCVERB: /* gc-verbose */ |
4735 | case OP_GCVERB: /* gc-verbose */ |
5237 | { |
4736 | { |
5238 | int was = SCHEME_V->gc_verbose; |
4737 | int was = SCHEME_V->gc_verbose; |
5239 | |
4738 | |
5240 | SCHEME_V->gc_verbose = (car (SCHEME_V->args) != S_F); |
4739 | SCHEME_V->gc_verbose = (a != S_F); |
5241 | s_retbool (was); |
4740 | s_retbool (was); |
5242 | } |
4741 | } |
5243 | |
4742 | |
5244 | case OP_NEWSEGMENT: /* new-segment */ |
4743 | case OP_NEWSEGMENT: /* new-segment */ |
5245 | if (!is_pair (SCHEME_V->args) || !is_number (car (SCHEME_V->args))) |
4744 | if (!is_pair (args) || !is_number (a)) |
5246 | Error_0 ("new-segment: argument must be a number"); |
4745 | Error_0 ("new-segment: argument must be a number"); |
5247 | |
4746 | |
5248 | alloc_cellseg (SCHEME_A_ (int)ivalue (car (SCHEME_V->args))); |
4747 | alloc_cellseg (SCHEME_A_ (int)ivalue (a)); |
5249 | |
4748 | |
5250 | s_return (S_T); |
4749 | s_return (S_T); |
5251 | |
4750 | |
5252 | case OP_OBLIST: /* oblist */ |
4751 | case OP_OBLIST: /* oblist */ |
5253 | s_return (oblist_all_symbols (SCHEME_A)); |
4752 | s_return (oblist_all_symbols (SCHEME_A)); |
… | |
… | |
5280 | case OP_OPEN_INOUTFILE: |
4779 | case OP_OPEN_INOUTFILE: |
5281 | prop = port_input | port_output; |
4780 | prop = port_input | port_output; |
5282 | break; |
4781 | break; |
5283 | } |
4782 | } |
5284 | |
4783 | |
5285 | p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); |
4784 | p = port_from_filename (SCHEME_A_ strvalue (a), prop); |
5286 | |
4785 | |
5287 | if (p == NIL) |
4786 | s_return (p == NIL ? S_F : p); |
5288 | { |
|
|
5289 | s_return (S_F); |
|
|
5290 | } |
|
|
5291 | |
|
|
5292 | s_return (p); |
|
|
5293 | } |
4787 | } |
5294 | |
4788 | |
5295 | # if USE_STRING_PORTS |
4789 | # if USE_STRING_PORTS |
5296 | |
4790 | |
5297 | case OP_OPEN_INSTRING: /* open-input-string */ |
4791 | case OP_OPEN_INSTRING: /* open-input-string */ |
… | |
… | |
5309 | case OP_OPEN_INOUTSTRING: |
4803 | case OP_OPEN_INOUTSTRING: |
5310 | prop = port_input | port_output; |
4804 | prop = port_input | port_output; |
5311 | break; |
4805 | break; |
5312 | } |
4806 | } |
5313 | |
4807 | |
5314 | p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), |
4808 | p = port_from_string (SCHEME_A_ strvalue (a), |
5315 | strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); |
4809 | strvalue (a) + strlength (a), prop); |
5316 | |
4810 | |
5317 | if (p == NIL) |
4811 | s_return (p == NIL ? S_F : p); |
5318 | { |
|
|
5319 | s_return (S_F); |
|
|
5320 | } |
|
|
5321 | |
|
|
5322 | s_return (p); |
|
|
5323 | } |
4812 | } |
5324 | |
4813 | |
5325 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
4814 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
5326 | { |
4815 | { |
5327 | pointer p; |
4816 | pointer p; |
5328 | |
4817 | |
5329 | if (car (SCHEME_V->args) == NIL) |
4818 | if (a == NIL) |
5330 | { |
|
|
5331 | p = port_from_scratch (SCHEME_A); |
4819 | p = port_from_scratch (SCHEME_A); |
5332 | |
|
|
5333 | if (p == NIL) |
|
|
5334 | { |
|
|
5335 | s_return (S_F); |
|
|
5336 | } |
|
|
5337 | } |
|
|
5338 | else |
4820 | else |
5339 | { |
|
|
5340 | p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), |
4821 | p = port_from_string (SCHEME_A_ strvalue (a), |
5341 | strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); |
4822 | strvalue (a) + strlength (a), port_output); |
5342 | |
4823 | |
5343 | if (p == NIL) |
4824 | s_return (p == NIL ? S_F : p); |
5344 | { |
|
|
5345 | s_return (S_F); |
|
|
5346 | } |
|
|
5347 | } |
|
|
5348 | |
|
|
5349 | s_return (p); |
|
|
5350 | } |
4825 | } |
5351 | |
4826 | |
5352 | case OP_GET_OUTSTRING: /* get-output-string */ |
4827 | case OP_GET_OUTSTRING: /* get-output-string */ |
5353 | { |
4828 | { |
5354 | port *p; |
4829 | port *p; |
5355 | |
4830 | |
5356 | if ((p = car (SCHEME_V->args)->object.port)->kind & port_string) |
4831 | if ((p = a->object.port)->kind & port_string) |
5357 | { |
4832 | { |
5358 | off_t size; |
4833 | off_t size; |
5359 | char *str; |
4834 | char *str; |
5360 | |
4835 | |
5361 | size = p->rep.string.curr - p->rep.string.start + 1; |
4836 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
5377 | } |
4852 | } |
5378 | |
4853 | |
5379 | # endif |
4854 | # endif |
5380 | |
4855 | |
5381 | case OP_CLOSE_INPORT: /* close-input-port */ |
4856 | case OP_CLOSE_INPORT: /* close-input-port */ |
5382 | port_close (SCHEME_A_ car (SCHEME_V->args), port_input); |
4857 | port_close (SCHEME_A_ a, port_input); |
5383 | s_return (S_T); |
4858 | s_return (S_T); |
5384 | |
4859 | |
5385 | case OP_CLOSE_OUTPORT: /* close-output-port */ |
4860 | case OP_CLOSE_OUTPORT: /* close-output-port */ |
5386 | port_close (SCHEME_A_ car (SCHEME_V->args), port_output); |
4861 | port_close (SCHEME_A_ a, port_output); |
5387 | s_return (S_T); |
4862 | s_return (S_T); |
5388 | #endif |
4863 | #endif |
5389 | |
4864 | |
5390 | case OP_INT_ENV: /* interaction-environment */ |
4865 | case OP_INT_ENV: /* interaction-environment */ |
5391 | s_return (SCHEME_V->global_env); |
4866 | s_return (SCHEME_V->global_env); |
… | |
… | |
5393 | case OP_CURR_ENV: /* current-environment */ |
4868 | case OP_CURR_ENV: /* current-environment */ |
5394 | s_return (SCHEME_V->envir); |
4869 | s_return (SCHEME_V->envir); |
5395 | |
4870 | |
5396 | } |
4871 | } |
5397 | |
4872 | |
5398 | return S_T; |
4873 | if (USE_ERROR_CHECKING) abort (); |
5399 | } |
4874 | } |
5400 | |
4875 | |
5401 | static pointer |
4876 | static int |
5402 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4877 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
5403 | { |
4878 | { |
|
|
4879 | pointer args = SCHEME_V->args; |
5404 | pointer x; |
4880 | pointer x; |
5405 | |
4881 | |
5406 | if (SCHEME_V->nesting != 0) |
4882 | if (SCHEME_V->nesting != 0) |
5407 | { |
4883 | { |
5408 | int n = SCHEME_V->nesting; |
4884 | int n = SCHEME_V->nesting; |
… | |
… | |
5415 | switch (op) |
4891 | switch (op) |
5416 | { |
4892 | { |
5417 | /* ========== reading part ========== */ |
4893 | /* ========== reading part ========== */ |
5418 | #if USE_PORTS |
4894 | #if USE_PORTS |
5419 | case OP_READ: |
4895 | case OP_READ: |
5420 | if (!is_pair (SCHEME_V->args)) |
4896 | if (!is_pair (args)) |
5421 | { |
|
|
5422 | s_goto (OP_READ_INTERNAL); |
4897 | s_goto (OP_READ_INTERNAL); |
5423 | } |
|
|
5424 | |
4898 | |
5425 | if (!is_inport (car (SCHEME_V->args))) |
4899 | if (!is_inport (car (args))) |
5426 | { |
|
|
5427 | Error_1 ("read: not an input port:", car (SCHEME_V->args)); |
4900 | Error_1 ("read: not an input port:", car (args)); |
5428 | } |
|
|
5429 | |
4901 | |
5430 | if (car (SCHEME_V->args) == SCHEME_V->inport) |
4902 | if (car (args) == SCHEME_V->inport) |
5431 | { |
|
|
5432 | s_goto (OP_READ_INTERNAL); |
4903 | s_goto (OP_READ_INTERNAL); |
5433 | } |
|
|
5434 | |
4904 | |
5435 | x = SCHEME_V->inport; |
4905 | x = SCHEME_V->inport; |
5436 | SCHEME_V->inport = car (SCHEME_V->args); |
4906 | SCHEME_V->inport = car (args); |
5437 | x = cons (x, NIL); |
4907 | x = cons (x, NIL); |
5438 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
4908 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
5439 | s_goto (OP_READ_INTERNAL); |
4909 | s_goto (OP_READ_INTERNAL); |
5440 | |
4910 | |
5441 | case OP_READ_CHAR: /* read-char */ |
4911 | case OP_READ_CHAR: /* read-char */ |
5442 | case OP_PEEK_CHAR: /* peek-char */ |
4912 | case OP_PEEK_CHAR: /* peek-char */ |
5443 | { |
4913 | { |
5444 | int c; |
4914 | int c; |
5445 | |
4915 | |
5446 | if (is_pair (SCHEME_V->args)) |
4916 | if (is_pair (args)) |
5447 | { |
4917 | { |
5448 | if (car (SCHEME_V->args) != SCHEME_V->inport) |
4918 | if (car (args) != SCHEME_V->inport) |
5449 | { |
4919 | { |
5450 | x = SCHEME_V->inport; |
4920 | x = SCHEME_V->inport; |
5451 | x = cons (x, NIL); |
4921 | x = cons (x, NIL); |
5452 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
4922 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
5453 | SCHEME_V->inport = car (SCHEME_V->args); |
4923 | SCHEME_V->inport = car (args); |
5454 | } |
4924 | } |
5455 | } |
4925 | } |
5456 | |
4926 | |
5457 | c = inchar (SCHEME_A); |
4927 | c = inchar (SCHEME_A); |
5458 | |
4928 | |
… | |
… | |
5468 | case OP_CHAR_READY: /* char-ready? */ |
4938 | case OP_CHAR_READY: /* char-ready? */ |
5469 | { |
4939 | { |
5470 | pointer p = SCHEME_V->inport; |
4940 | pointer p = SCHEME_V->inport; |
5471 | int res; |
4941 | int res; |
5472 | |
4942 | |
5473 | if (is_pair (SCHEME_V->args)) |
4943 | if (is_pair (args)) |
5474 | { |
|
|
5475 | p = car (SCHEME_V->args); |
4944 | p = car (args); |
5476 | } |
|
|
5477 | |
4945 | |
5478 | res = p->object.port->kind & port_string; |
4946 | res = p->object.port->kind & port_string; |
5479 | |
4947 | |
5480 | s_retbool (res); |
4948 | s_retbool (res); |
5481 | } |
4949 | } |
5482 | |
4950 | |
5483 | case OP_SET_INPORT: /* set-input-port */ |
4951 | case OP_SET_INPORT: /* set-input-port */ |
5484 | SCHEME_V->inport = car (SCHEME_V->args); |
4952 | SCHEME_V->inport = car (args); |
5485 | s_return (SCHEME_V->value); |
4953 | s_return (SCHEME_V->value); |
5486 | |
4954 | |
5487 | case OP_SET_OUTPORT: /* set-output-port */ |
4955 | case OP_SET_OUTPORT: /* set-output-port */ |
5488 | SCHEME_V->outport = car (SCHEME_V->args); |
4956 | SCHEME_V->outport = car (args); |
5489 | s_return (SCHEME_V->value); |
4957 | s_return (SCHEME_V->value); |
5490 | #endif |
4958 | #endif |
5491 | |
4959 | |
5492 | case OP_RDSEXPR: |
4960 | case OP_RDSEXPR: |
5493 | switch (SCHEME_V->tok) |
4961 | switch (SCHEME_V->tok) |
… | |
… | |
5496 | s_return (S_EOF); |
4964 | s_return (S_EOF); |
5497 | /* NOTREACHED */ |
4965 | /* NOTREACHED */ |
5498 | |
4966 | |
5499 | case TOK_VEC: |
4967 | case TOK_VEC: |
5500 | s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); |
4968 | s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); |
5501 | |
|
|
5502 | /* fall through */ |
4969 | /* fall through */ |
|
|
4970 | |
5503 | case TOK_LPAREN: |
4971 | case TOK_LPAREN: |
5504 | SCHEME_V->tok = token (SCHEME_A); |
4972 | SCHEME_V->tok = token (SCHEME_A); |
5505 | |
4973 | |
5506 | if (SCHEME_V->tok == TOK_RPAREN) |
4974 | if (SCHEME_V->tok == TOK_RPAREN) |
5507 | s_return (NIL); |
4975 | s_return (NIL); |
… | |
… | |
5527 | s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); |
4995 | s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); |
5528 | SCHEME_V->tok = TOK_LPAREN; |
4996 | SCHEME_V->tok = TOK_LPAREN; |
5529 | s_goto (OP_RDSEXPR); |
4997 | s_goto (OP_RDSEXPR); |
5530 | } |
4998 | } |
5531 | else |
4999 | else |
5532 | { |
|
|
5533 | s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); |
5000 | s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); |
5534 | } |
|
|
5535 | |
5001 | |
5536 | s_goto (OP_RDSEXPR); |
5002 | s_goto (OP_RDSEXPR); |
5537 | |
5003 | |
5538 | case TOK_COMMA: |
5004 | case TOK_COMMA: |
5539 | s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); |
5005 | s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); |
… | |
… | |
5581 | } |
5047 | } |
5582 | |
5048 | |
5583 | break; |
5049 | break; |
5584 | |
5050 | |
5585 | case OP_RDLIST: |
5051 | case OP_RDLIST: |
5586 | { |
|
|
5587 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
5052 | SCHEME_V->args = cons (SCHEME_V->value, args); |
5588 | SCHEME_V->tok = token (SCHEME_A); |
5053 | SCHEME_V->tok = token (SCHEME_A); |
5589 | |
5054 | |
5590 | if (SCHEME_V->tok == TOK_EOF) |
5055 | switch (SCHEME_V->tok) |
|
|
5056 | { |
|
|
5057 | case TOK_EOF: |
5591 | s_return (S_EOF); |
5058 | s_return (S_EOF); |
5592 | else if (SCHEME_V->tok == TOK_RPAREN) |
5059 | |
|
|
5060 | case TOK_RPAREN: |
5593 | { |
5061 | { |
5594 | int c = inchar (SCHEME_A); |
5062 | int c = inchar (SCHEME_A); |
5595 | |
5063 | |
5596 | if (c != '\n') |
5064 | if (c != '\n') |
5597 | backchar (SCHEME_A_ c); |
5065 | backchar (SCHEME_A_ c); |
5598 | |
|
|
5599 | #if SHOW_ERROR_LINE |
5066 | #if SHOW_ERROR_LINE |
5600 | else if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
5067 | else if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
5601 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line++; |
5068 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line++; |
5602 | |
|
|
5603 | #endif |
5069 | #endif |
|
|
5070 | |
5604 | SCHEME_V->nesting_stack[SCHEME_V->file_i]--; |
5071 | SCHEME_V->nesting_stack[SCHEME_V->file_i]--; |
5605 | s_return (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args)); |
5072 | s_return (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args)); |
5606 | } |
5073 | } |
5607 | else if (SCHEME_V->tok == TOK_DOT) |
5074 | |
5608 | { |
5075 | case TOK_DOT: |
5609 | s_save (SCHEME_A_ OP_RDDOT, SCHEME_V->args, NIL); |
5076 | s_save (SCHEME_A_ OP_RDDOT, SCHEME_V->args, NIL); |
5610 | SCHEME_V->tok = token (SCHEME_A); |
5077 | SCHEME_V->tok = token (SCHEME_A); |
5611 | s_goto (OP_RDSEXPR); |
5078 | s_goto (OP_RDSEXPR); |
5612 | } |
5079 | |
5613 | else |
5080 | default: |
5614 | { |
|
|
5615 | s_save (SCHEME_A_ OP_RDLIST, SCHEME_V->args, NIL);; |
5081 | s_save (SCHEME_A_ OP_RDLIST, SCHEME_V->args, NIL); |
5616 | s_goto (OP_RDSEXPR); |
5082 | s_goto (OP_RDSEXPR); |
5617 | } |
5083 | } |
5618 | } |
|
|
5619 | |
5084 | |
5620 | case OP_RDDOT: |
5085 | case OP_RDDOT: |
5621 | if (token (SCHEME_A) != TOK_RPAREN) |
5086 | if (token (SCHEME_A) != TOK_RPAREN) |
5622 | Error_0 ("syntax error: illegal dot expression"); |
5087 | Error_0 ("syntax error: illegal dot expression"); |
5623 | else |
5088 | |
5624 | { |
|
|
5625 | SCHEME_V->nesting_stack[SCHEME_V->file_i]--; |
5089 | SCHEME_V->nesting_stack[SCHEME_V->file_i]--; |
5626 | s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); |
5090 | s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, args)); |
5627 | } |
|
|
5628 | |
5091 | |
5629 | case OP_RDQUOTE: |
5092 | case OP_RDQUOTE: |
5630 | s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); |
5093 | s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); |
5631 | |
5094 | |
5632 | case OP_RDQQUOTE: |
5095 | case OP_RDQQUOTE: |
… | |
… | |
5654 | SCHEME_V->args = SCHEME_V->value; |
5117 | SCHEME_V->args = SCHEME_V->value; |
5655 | s_goto (OP_VECTOR); |
5118 | s_goto (OP_VECTOR); |
5656 | |
5119 | |
5657 | /* ========== printing part ========== */ |
5120 | /* ========== printing part ========== */ |
5658 | case OP_P0LIST: |
5121 | case OP_P0LIST: |
5659 | if (is_vector (SCHEME_V->args)) |
5122 | if (is_vector (args)) |
5660 | { |
5123 | { |
5661 | putstr (SCHEME_A_ "#("); |
5124 | putstr (SCHEME_A_ "#("); |
5662 | SCHEME_V->args = cons (SCHEME_V->args, mk_integer (SCHEME_A_ 0)); |
5125 | SCHEME_V->args = cons (args, mk_integer (SCHEME_A_ 0)); |
5663 | s_goto (OP_PVECFROM); |
5126 | s_goto (OP_PVECFROM); |
5664 | } |
5127 | } |
5665 | else if (is_environment (SCHEME_V->args)) |
5128 | else if (is_environment (args)) |
5666 | { |
5129 | { |
5667 | putstr (SCHEME_A_ "#<ENVIRONMENT>"); |
5130 | putstr (SCHEME_A_ "#<ENVIRONMENT>"); |
5668 | s_return (S_T); |
5131 | s_return (S_T); |
5669 | } |
5132 | } |
5670 | else if (!is_pair (SCHEME_V->args)) |
5133 | else if (!is_pair (args)) |
5671 | { |
5134 | { |
5672 | printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); |
5135 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5673 | s_return (S_T); |
5136 | s_return (S_T); |
5674 | } |
5137 | } |
5675 | else if (car (SCHEME_V->args) == SCHEME_V->QUOTE && ok_abbrev (cdr (SCHEME_V->args))) |
5138 | else |
5676 | { |
5139 | { |
|
|
5140 | pointer a = car (args); |
|
|
5141 | pointer b = cdr (args); |
|
|
5142 | int ok_abbr = ok_abbrev (b); |
|
|
5143 | SCHEME_V->args = car (b); |
|
|
5144 | |
|
|
5145 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5677 | putstr (SCHEME_A_ "'"); |
5146 | putstr (SCHEME_A_ "'"); |
5678 | SCHEME_V->args = cadr (SCHEME_V->args); |
5147 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
|
|
5148 | putstr (SCHEME_A_ "`"); |
|
|
5149 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
|
|
5150 | putstr (SCHEME_A_ ","); |
|
|
5151 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
|
|
5152 | putstr (SCHEME_A_ ",@"); |
|
|
5153 | else |
|
|
5154 | { |
|
|
5155 | putstr (SCHEME_A_ "("); |
|
|
5156 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
|
|
5157 | SCHEME_V->args = a; |
|
|
5158 | } |
|
|
5159 | |
5679 | s_goto (OP_P0LIST); |
5160 | s_goto (OP_P0LIST); |
5680 | } |
5161 | } |
5681 | else if (car (SCHEME_V->args) == SCHEME_V->QQUOTE && ok_abbrev (cdr (SCHEME_V->args))) |
5162 | |
|
|
5163 | case OP_P1LIST: |
|
|
5164 | if (is_pair (args)) |
5682 | { |
5165 | { |
|
|
5166 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5683 | putstr (SCHEME_A_ "`"); |
5167 | putstr (SCHEME_A_ " "); |
5684 | SCHEME_V->args = cadr (SCHEME_V->args); |
5168 | SCHEME_V->args = car (args); |
5685 | s_goto (OP_P0LIST); |
5169 | s_goto (OP_P0LIST); |
5686 | } |
5170 | } |
5687 | else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTE && ok_abbrev (cdr (SCHEME_V->args))) |
|
|
5688 | { |
|
|
5689 | putstr (SCHEME_A_ ","); |
|
|
5690 | SCHEME_V->args = cadr (SCHEME_V->args); |
|
|
5691 | s_goto (OP_P0LIST); |
|
|
5692 | } |
|
|
5693 | else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTESP && ok_abbrev (cdr (SCHEME_V->args))) |
|
|
5694 | { |
|
|
5695 | putstr (SCHEME_A_ ",@"); |
|
|
5696 | SCHEME_V->args = cadr (SCHEME_V->args); |
|
|
5697 | s_goto (OP_P0LIST); |
|
|
5698 | } |
|
|
5699 | else |
|
|
5700 | { |
|
|
5701 | putstr (SCHEME_A_ "("); |
|
|
5702 | s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL); |
|
|
5703 | SCHEME_V->args = car (SCHEME_V->args); |
|
|
5704 | s_goto (OP_P0LIST); |
|
|
5705 | } |
|
|
5706 | |
|
|
5707 | case OP_P1LIST: |
|
|
5708 | if (is_pair (SCHEME_V->args)) |
|
|
5709 | { |
|
|
5710 | s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL); |
|
|
5711 | putstr (SCHEME_A_ " "); |
|
|
5712 | SCHEME_V->args = car (SCHEME_V->args); |
|
|
5713 | s_goto (OP_P0LIST); |
|
|
5714 | } |
|
|
5715 | else if (is_vector (SCHEME_V->args)) |
5171 | else if (is_vector (args)) |
5716 | { |
5172 | { |
5717 | s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); |
5173 | s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); |
5718 | putstr (SCHEME_A_ " . "); |
5174 | putstr (SCHEME_A_ " . "); |
5719 | s_goto (OP_P0LIST); |
5175 | s_goto (OP_P0LIST); |
5720 | } |
5176 | } |
5721 | else |
5177 | else |
5722 | { |
5178 | { |
5723 | if (SCHEME_V->args != NIL) |
5179 | if (args != NIL) |
5724 | { |
5180 | { |
5725 | putstr (SCHEME_A_ " . "); |
5181 | putstr (SCHEME_A_ " . "); |
5726 | printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); |
5182 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5727 | } |
5183 | } |
5728 | |
5184 | |
5729 | putstr (SCHEME_A_ ")"); |
5185 | putstr (SCHEME_A_ ")"); |
5730 | s_return (S_T); |
5186 | s_return (S_T); |
5731 | } |
5187 | } |
5732 | |
5188 | |
5733 | case OP_PVECFROM: |
5189 | case OP_PVECFROM: |
5734 | { |
5190 | { |
5735 | int i = ivalue_unchecked (cdr (SCHEME_V->args)); |
5191 | int i = ivalue_unchecked (cdr (args)); |
5736 | pointer vec = car (SCHEME_V->args); |
5192 | pointer vec = car (args); |
5737 | int len = ivalue_unchecked (vec); |
5193 | int len = veclength (vec); |
5738 | |
5194 | |
5739 | if (i == len) |
5195 | if (i == len) |
5740 | { |
5196 | { |
5741 | putstr (SCHEME_A_ ")"); |
5197 | putstr (SCHEME_A_ ")"); |
5742 | s_return (S_T); |
5198 | s_return (S_T); |
5743 | } |
5199 | } |
5744 | else |
5200 | else |
5745 | { |
5201 | { |
5746 | pointer elem = vector_elem (vec, i); |
5202 | pointer elem = vector_elem (vec, i); |
5747 | |
5203 | |
5748 | ivalue_unchecked (cdr (SCHEME_V->args)) = i + 1; |
5204 | ivalue_unchecked (cdr (args)) = i + 1; |
5749 | s_save (SCHEME_A_ OP_PVECFROM, SCHEME_V->args, NIL); |
5205 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5750 | SCHEME_V->args = elem; |
5206 | SCHEME_V->args = elem; |
5751 | |
5207 | |
5752 | if (i > 0) |
5208 | if (i > 0) |
5753 | putstr (SCHEME_A_ " "); |
5209 | putstr (SCHEME_A_ " "); |
5754 | |
5210 | |
5755 | s_goto (OP_P0LIST); |
5211 | s_goto (OP_P0LIST); |
5756 | } |
5212 | } |
5757 | } |
5213 | } |
5758 | } |
5214 | } |
5759 | |
5215 | |
5760 | return S_T; |
5216 | if (USE_ERROR_CHECKING) abort (); |
5761 | } |
5217 | } |
5762 | |
5218 | |
5763 | static pointer |
5219 | static int |
5764 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5220 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5765 | { |
5221 | { |
|
|
5222 | pointer args = SCHEME_V->args; |
|
|
5223 | pointer a = car (args); |
5766 | pointer x, y; |
5224 | pointer x, y; |
5767 | |
5225 | |
5768 | switch (op) |
5226 | switch (op) |
5769 | { |
5227 | { |
5770 | case OP_LIST_LENGTH: /* length *//* a.k */ |
5228 | case OP_LIST_LENGTH: /* length *//* a.k */ |
5771 | { |
5229 | { |
5772 | long v = list_length (SCHEME_A_ car (SCHEME_V->args)); |
5230 | long v = list_length (SCHEME_A_ a); |
5773 | |
5231 | |
5774 | if (v < 0) |
5232 | if (v < 0) |
5775 | Error_1 ("length: not a list:", car (SCHEME_V->args)); |
5233 | Error_1 ("length: not a list:", a); |
5776 | |
5234 | |
5777 | s_return (mk_integer (SCHEME_A_ v)); |
5235 | s_return (mk_integer (SCHEME_A_ v)); |
5778 | } |
5236 | } |
5779 | |
5237 | |
5780 | case OP_ASSQ: /* assq *//* a.k */ |
5238 | case OP_ASSQ: /* assq *//* a.k */ |
5781 | x = car (SCHEME_V->args); |
5239 | x = a; |
5782 | |
5240 | |
5783 | for (y = cadr (SCHEME_V->args); is_pair (y); y = cdr (y)) |
5241 | for (y = cadr (args); is_pair (y); y = cdr (y)) |
5784 | { |
5242 | { |
5785 | if (!is_pair (car (y))) |
5243 | if (!is_pair (car (y))) |
5786 | Error_0 ("unable to handle non pair element"); |
5244 | Error_0 ("unable to handle non pair element"); |
5787 | |
5245 | |
5788 | if (x == caar (y)) |
5246 | if (x == caar (y)) |
… | |
… | |
5794 | else |
5252 | else |
5795 | s_return (S_F); |
5253 | s_return (S_F); |
5796 | |
5254 | |
5797 | |
5255 | |
5798 | case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ |
5256 | case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ |
5799 | SCHEME_V->args = car (SCHEME_V->args); |
5257 | SCHEME_V->args = a; |
5800 | |
5258 | |
5801 | if (SCHEME_V->args == NIL) |
5259 | if (SCHEME_V->args == NIL) |
5802 | s_return (S_F); |
5260 | s_return (S_F); |
5803 | else if (is_closure (SCHEME_V->args)) |
5261 | else if (is_closure (SCHEME_V->args)) |
5804 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
5262 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
… | |
… | |
5810 | case OP_CLOSUREP: /* closure? */ |
5268 | case OP_CLOSUREP: /* closure? */ |
5811 | /* |
5269 | /* |
5812 | * Note, macro object is also a closure. |
5270 | * Note, macro object is also a closure. |
5813 | * Therefore, (closure? <#MACRO>) ==> #t |
5271 | * Therefore, (closure? <#MACRO>) ==> #t |
5814 | */ |
5272 | */ |
5815 | s_retbool (is_closure (car (SCHEME_V->args))); |
5273 | s_retbool (is_closure (a)); |
5816 | |
5274 | |
5817 | case OP_MACROP: /* macro? */ |
5275 | case OP_MACROP: /* macro? */ |
5818 | s_retbool (is_macro (car (SCHEME_V->args))); |
5276 | s_retbool (is_macro (a)); |
5819 | } |
5277 | } |
5820 | |
5278 | |
5821 | return S_T; /* NOTREACHED */ |
5279 | if (USE_ERROR_CHECKING) abort (); |
5822 | } |
5280 | } |
5823 | |
5281 | |
|
|
5282 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5824 | typedef pointer (*dispatch_func) (SCHEME_P_ enum scheme_opcodes); |
5283 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5825 | |
5284 | |
5826 | typedef int (*test_predicate) (pointer); |
5285 | typedef int (*test_predicate)(pointer); |
5827 | static int |
5286 | static int |
5828 | is_any (pointer p) |
5287 | is_any (pointer p) |
5829 | { |
5288 | { |
5830 | return 1; |
5289 | return 1; |
5831 | } |
5290 | } |
5832 | |
5291 | |
5833 | static int |
5292 | static int |
5834 | is_nonneg (pointer p) |
5293 | is_nonneg (pointer p) |
5835 | { |
5294 | { |
5836 | return ivalue (p) >= 0 && is_integer (p); |
5295 | return ivalue (p) >= 0 && is_integer (p); |
|
|
5296 | } |
|
|
5297 | |
|
|
5298 | static int |
|
|
5299 | tst_is_list (pointer p) |
|
|
5300 | { |
|
|
5301 | return p == NIL || is_pair (p); |
5837 | } |
5302 | } |
5838 | |
5303 | |
5839 | /* Correspond carefully with following defines! */ |
5304 | /* Correspond carefully with following defines! */ |
5840 | static struct |
5305 | static struct |
5841 | { |
5306 | { |
5842 | test_predicate fct; |
5307 | test_predicate fct; |
5843 | const char *kind; |
5308 | const char *kind; |
5844 | } tests[] = |
5309 | } tests[] = |
5845 | { |
5310 | { |
5846 | { 0, 0}, /* unused */ |
5311 | { is_any, 0 }, |
5847 | { is_any, 0}, |
5312 | { is_string, "string" }, |
5848 | { is_string, "string" }, |
5313 | { is_symbol, "symbol" }, |
5849 | { is_symbol, "symbol" }, |
5314 | { is_port, "port" }, |
5850 | { is_port, "port" }, |
|
|
5851 | { is_inport, "input port" }, |
5315 | { is_inport, "input port" }, |
5852 | { is_outport, "output port" }, |
5316 | { is_outport, "output port" }, |
5853 | { is_environment, "environment" }, |
5317 | { is_environment, "environment" }, |
5854 | { is_pair, "pair" }, |
5318 | { is_pair, "pair" }, |
5855 | { 0, "pair or '()" }, |
5319 | { tst_is_list, "pair or '()" }, |
5856 | { is_character, "character" }, |
5320 | { is_character, "character" }, |
5857 | { is_vector, "vector" }, |
5321 | { is_vector, "vector" }, |
5858 | { is_number, "number" }, |
5322 | { is_number, "number" }, |
5859 | { is_integer, "integer" }, |
5323 | { is_integer, "integer" }, |
5860 | { is_nonneg, "non-negative integer" } |
5324 | { is_nonneg, "non-negative integer" } |
5861 | }; |
5325 | }; |
5862 | |
5326 | |
5863 | #define TST_NONE 0 |
5327 | #define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ |
5864 | #define TST_ANY "\001" |
5328 | #define TST_ANY "\001" |
5865 | #define TST_STRING "\002" |
5329 | #define TST_STRING "\002" |
5866 | #define TST_SYMBOL "\003" |
5330 | #define TST_SYMBOL "\003" |
5867 | #define TST_PORT "\004" |
5331 | #define TST_PORT "\004" |
5868 | #define TST_INPORT "\005" |
5332 | #define TST_INPORT "\005" |
5869 | #define TST_OUTPORT "\006" |
5333 | #define TST_OUTPORT "\006" |
5870 | #define TST_ENVIRONMENT "\007" |
5334 | #define TST_ENVIRONMENT "\007" |
5871 | #define TST_PAIR "\010" |
5335 | #define TST_PAIR "\010" |
5872 | #define TST_LIST "\011" |
5336 | #define TST_LIST "\011" |
5873 | #define TST_CHAR "\012" |
5337 | #define TST_CHAR "\012" |
5874 | #define TST_VECTOR "\013" |
5338 | #define TST_VECTOR "\013" |
5875 | #define TST_NUMBER "\014" |
5339 | #define TST_NUMBER "\014" |
5876 | #define TST_INTEGER "\015" |
5340 | #define TST_INTEGER "\015" |
5877 | #define TST_NATURAL "\016" |
5341 | #define TST_NATURAL "\016" |
|
|
5342 | |
|
|
5343 | #define INF_ARG 0xff |
|
|
5344 | #define UNNAMED_OP "" |
|
|
5345 | |
|
|
5346 | static const char opnames[] = |
|
|
5347 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
|
|
5348 | #include "opdefines.h" |
|
|
5349 | #undef OP_DEF |
|
|
5350 | ; |
|
|
5351 | |
|
|
5352 | static const char * |
|
|
5353 | opname (int idx) |
|
|
5354 | { |
|
|
5355 | const char *name = opnames; |
|
|
5356 | |
|
|
5357 | /* should do this at compile time, but would require external program, right? */ |
|
|
5358 | while (idx--) |
|
|
5359 | name += strlen (name) + 1; |
|
|
5360 | |
|
|
5361 | return *name ? name : "ILLEGAL"; |
|
|
5362 | } |
|
|
5363 | |
|
|
5364 | static const char * |
|
|
5365 | procname (pointer x) |
|
|
5366 | { |
|
|
5367 | return opname (procnum (x)); |
|
|
5368 | } |
5878 | |
5369 | |
5879 | typedef struct |
5370 | typedef struct |
5880 | { |
5371 | { |
5881 | dispatch_func func; |
5372 | uint8_t func; |
5882 | char *name; |
5373 | /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ |
|
|
5374 | uint8_t builtin; |
5883 | int min_arity; |
5375 | uint8_t min_arity; |
5884 | int max_arity; |
5376 | uint8_t max_arity; |
5885 | char *arg_tests_encoding; |
5377 | char arg_tests_encoding[3]; |
5886 | } op_code_info; |
5378 | } op_code_info; |
5887 | |
5379 | |
5888 | #define INF_ARG 0xffff |
|
|
5889 | |
|
|
5890 | static op_code_info dispatch_table[] = { |
5380 | static const op_code_info dispatch_table[] = { |
5891 | #define OP_DEF(A,B,C,D,E,OP) {A,B,C,D,E}, |
5381 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, |
5892 | #include "opdefines.h" |
5382 | #include "opdefines.h" |
|
|
5383 | #undef OP_DEF |
5893 | {0} |
5384 | {0} |
5894 | }; |
5385 | }; |
5895 | |
5386 | |
5896 | static const char * |
|
|
5897 | procname (pointer x) |
|
|
5898 | { |
|
|
5899 | int n = procnum (x); |
|
|
5900 | const char *name = dispatch_table[n].name; |
|
|
5901 | |
|
|
5902 | if (name == 0) |
|
|
5903 | { |
|
|
5904 | name = "ILLEGAL!"; |
|
|
5905 | } |
|
|
5906 | |
|
|
5907 | return name; |
|
|
5908 | } |
|
|
5909 | |
|
|
5910 | /* kernel of this interpreter */ |
5387 | /* kernel of this interpreter */ |
5911 | static void |
5388 | static void ecb_hot |
5912 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5389 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5913 | { |
5390 | { |
5914 | SCHEME_V->op = op; |
5391 | SCHEME_V->op = op; |
5915 | |
5392 | |
5916 | for (;;) |
5393 | for (;;) |
5917 | { |
5394 | { |
5918 | op_code_info *pcd = dispatch_table + SCHEME_V->op; |
5395 | const op_code_info *pcd = dispatch_table + SCHEME_V->op; |
5919 | |
5396 | |
|
|
5397 | #if USE_ERROR_CHECKING |
5920 | if (pcd->name != 0) /* if built-in function, check arguments */ |
5398 | if (pcd->builtin) /* if built-in function, check arguments */ |
5921 | { |
5399 | { |
5922 | char msg[STRBUFFSIZE]; |
5400 | char msg[STRBUFFSIZE]; |
5923 | int ok = 1; |
|
|
5924 | int n = list_length (SCHEME_A_ SCHEME_V->args); |
5401 | int n = list_length (SCHEME_A_ SCHEME_V->args); |
5925 | |
5402 | |
5926 | #if USE_ERROR_CHECKING |
|
|
5927 | /* Check number of arguments */ |
5403 | /* Check number of arguments */ |
5928 | if (n < pcd->min_arity) |
5404 | if (ecb_expect_false (n < pcd->min_arity)) |
5929 | { |
5405 | { |
5930 | ok = 0; |
|
|
5931 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5406 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5932 | pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); |
5407 | opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); |
|
|
5408 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5409 | continue; |
5933 | } |
5410 | } |
5934 | |
5411 | else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG)) |
5935 | if (ok && n > pcd->max_arity) |
|
|
5936 | { |
5412 | { |
5937 | ok = 0; |
|
|
5938 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5413 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5939 | pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); |
5414 | opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); |
|
|
5415 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5416 | continue; |
5940 | } |
5417 | } |
5941 | #endif |
5418 | else |
5942 | |
|
|
5943 | if (ok) |
|
|
5944 | { |
5419 | { |
5945 | if (pcd->arg_tests_encoding != 0) |
5420 | if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */ |
5946 | { |
5421 | { |
5947 | int i = 0; |
5422 | int i = 0; |
5948 | int j; |
5423 | int j; |
5949 | const char *t = pcd->arg_tests_encoding; |
5424 | const char *t = pcd->arg_tests_encoding; |
5950 | pointer arglist = SCHEME_V->args; |
5425 | pointer arglist = SCHEME_V->args; |
5951 | |
5426 | |
5952 | do |
5427 | do |
5953 | { |
5428 | { |
5954 | pointer arg = car (arglist); |
5429 | pointer arg = car (arglist); |
5955 | |
5430 | |
5956 | j = (int) t[0]; |
5431 | j = t[0]; |
5957 | |
5432 | |
5958 | if (j == TST_LIST[0]) |
5433 | if (!tests[j - 1].fct (arg)) |
5959 | { |
|
|
5960 | if (arg != NIL && !is_pair (arg)) |
|
|
5961 | break; |
5434 | break; |
5962 | } |
|
|
5963 | else |
|
|
5964 | { |
|
|
5965 | if (!tests[j].fct (arg)) |
|
|
5966 | break; |
|
|
5967 | } |
|
|
5968 | |
5435 | |
5969 | if (t[1] != 0) /* last test is replicated as necessary */ |
5436 | if (t[1]) /* last test is replicated as necessary */ |
5970 | { |
|
|
5971 | t++; |
5437 | t++; |
5972 | } |
|
|
5973 | |
5438 | |
5974 | arglist = cdr (arglist); |
5439 | arglist = cdr (arglist); |
5975 | i++; |
5440 | i++; |
5976 | } |
5441 | } |
5977 | while (i < n); |
5442 | while (i < n); |
5978 | |
5443 | |
5979 | #if USE_ERROR_CHECKING |
|
|
5980 | if (i < n) |
5444 | if (i < n) |
5981 | { |
5445 | { |
5982 | ok = 0; |
|
|
5983 | snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); |
5446 | snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind); |
|
|
5447 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5448 | continue; |
5984 | } |
5449 | } |
5985 | #endif |
|
|
5986 | } |
5450 | } |
5987 | } |
5451 | } |
5988 | |
|
|
5989 | if (!ok) |
|
|
5990 | { |
|
|
5991 | if (xError_1 (SCHEME_A_ msg, 0) == NIL) |
|
|
5992 | return; |
|
|
5993 | |
|
|
5994 | pcd = dispatch_table + SCHEME_V->op; |
|
|
5995 | } |
|
|
5996 | } |
5452 | } |
|
|
5453 | #endif |
5997 | |
5454 | |
5998 | ok_to_freely_gc (SCHEME_A); |
5455 | ok_to_freely_gc (SCHEME_A); |
5999 | |
5456 | |
6000 | if (pcd->func (SCHEME_A_ (enum scheme_opcodes) SCHEME_V->op) == NIL) |
5457 | static const dispatch_func dispatch_funcs[] = { |
|
|
5458 | opexe_0, |
|
|
5459 | opexe_1, |
|
|
5460 | opexe_2, |
|
|
5461 | opexe_3, |
|
|
5462 | opexe_4, |
|
|
5463 | opexe_5, |
|
|
5464 | opexe_6, |
|
|
5465 | }; |
|
|
5466 | |
|
|
5467 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
6001 | return; |
5468 | return; |
6002 | |
5469 | |
6003 | #if USE_ERROR_CHECKING |
5470 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
6004 | if (SCHEME_V->no_memory) |
|
|
6005 | { |
5471 | { |
6006 | xwrstr ("No memory!\n"); |
5472 | xwrstr ("No memory!\n"); |
6007 | return; |
5473 | return; |
6008 | } |
5474 | } |
6009 | #endif |
|
|
6010 | } |
5475 | } |
6011 | } |
5476 | } |
6012 | |
5477 | |
6013 | /* ========== Initialization of internal keywords ========== */ |
5478 | /* ========== Initialization of internal keywords ========== */ |
6014 | |
5479 | |
6015 | static void |
5480 | static void |
6016 | assign_syntax (SCHEME_P_ char *name) |
5481 | assign_syntax (SCHEME_P_ const char *name) |
6017 | { |
5482 | { |
6018 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5483 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
6019 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5484 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
6020 | } |
5485 | } |
6021 | |
5486 | |
6022 | static void |
5487 | static void |
6023 | assign_proc (SCHEME_P_ enum scheme_opcodes op, char *name) |
5488 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
6024 | { |
5489 | { |
6025 | pointer x = mk_symbol (SCHEME_A_ name); |
5490 | pointer x = mk_symbol (SCHEME_A_ name); |
6026 | pointer y = mk_proc (SCHEME_A_ op); |
5491 | pointer y = mk_proc (SCHEME_A_ op); |
6027 | new_slot_in_env (SCHEME_A_ x, y); |
5492 | new_slot_in_env (SCHEME_A_ x, y); |
6028 | } |
5493 | } |
… | |
… | |
6030 | static pointer |
5495 | static pointer |
6031 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5496 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
6032 | { |
5497 | { |
6033 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5498 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
6034 | set_typeflag (y, (T_PROC | T_ATOM)); |
5499 | set_typeflag (y, (T_PROC | T_ATOM)); |
6035 | ivalue_unchecked (y) = (long) op; |
5500 | ivalue_unchecked (y) = op; |
6036 | set_num_integer (y); |
5501 | set_num_integer (y); |
6037 | return y; |
5502 | return y; |
6038 | } |
5503 | } |
6039 | |
5504 | |
6040 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5505 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
… | |
… | |
6065 | |
5530 | |
6066 | case 'd': |
5531 | case 'd': |
6067 | return OP_COND0; /* cond */ |
5532 | return OP_COND0; /* cond */ |
6068 | |
5533 | |
6069 | case '*': |
5534 | case '*': |
6070 | return OP_LET0AST; /* let* */ |
5535 | return OP_LET0AST;/* let* */ |
6071 | |
5536 | |
6072 | default: |
5537 | default: |
6073 | return OP_SET0; /* set! */ |
5538 | return OP_SET0; /* set! */ |
6074 | } |
5539 | } |
6075 | |
5540 | |
… | |
… | |
6097 | |
5562 | |
6098 | case 'f': |
5563 | case 'f': |
6099 | return OP_DEF0; /* define */ |
5564 | return OP_DEF0; /* define */ |
6100 | |
5565 | |
6101 | default: |
5566 | default: |
6102 | return OP_LET0REC; /* letrec */ |
5567 | return OP_LET0REC;/* letrec */ |
6103 | } |
5568 | } |
6104 | |
5569 | |
6105 | default: |
5570 | default: |
6106 | return OP_C0STREAM; /* cons-stream */ |
5571 | return OP_C0STREAM; /* cons-stream */ |
6107 | } |
5572 | } |
6108 | } |
5573 | } |
6109 | |
5574 | |
6110 | #if USE_MULTIPLICITY |
5575 | #if USE_MULTIPLICITY |
6111 | scheme * |
5576 | ecb_cold scheme * |
6112 | scheme_init_new () |
5577 | scheme_init_new () |
6113 | { |
5578 | { |
6114 | scheme *sc = malloc (sizeof (scheme)); |
5579 | scheme *sc = malloc (sizeof (scheme)); |
6115 | |
5580 | |
6116 | if (!scheme_init (SCHEME_A)) |
5581 | if (!scheme_init (SCHEME_A)) |
… | |
… | |
6121 | else |
5586 | else |
6122 | return sc; |
5587 | return sc; |
6123 | } |
5588 | } |
6124 | #endif |
5589 | #endif |
6125 | |
5590 | |
6126 | int |
5591 | ecb_cold int |
6127 | scheme_init (SCHEME_P) |
5592 | scheme_init (SCHEME_P) |
6128 | { |
5593 | { |
6129 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5594 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
6130 | pointer x; |
5595 | pointer x; |
6131 | |
5596 | |
… | |
… | |
6158 | } |
5623 | } |
6159 | |
5624 | |
6160 | SCHEME_V->gc_verbose = 0; |
5625 | SCHEME_V->gc_verbose = 0; |
6161 | dump_stack_initialize (SCHEME_A); |
5626 | dump_stack_initialize (SCHEME_A); |
6162 | SCHEME_V->code = NIL; |
5627 | SCHEME_V->code = NIL; |
|
|
5628 | SCHEME_V->args = NIL; |
|
|
5629 | SCHEME_V->envir = NIL; |
6163 | SCHEME_V->tracing = 0; |
5630 | SCHEME_V->tracing = 0; |
6164 | |
5631 | |
6165 | /* init NIL */ |
5632 | /* init NIL */ |
6166 | set_typeflag (NIL, T_ATOM | MARK); |
5633 | set_typeflag (NIL, T_ATOM | T_MARK); |
6167 | set_car (NIL, NIL); |
5634 | set_car (NIL, NIL); |
6168 | set_cdr (NIL, NIL); |
5635 | set_cdr (NIL, NIL); |
6169 | /* init T */ |
5636 | /* init T */ |
6170 | set_typeflag (S_T, T_ATOM | MARK); |
5637 | set_typeflag (S_T, T_ATOM | T_MARK); |
6171 | set_car (S_T, S_T); |
5638 | set_car (S_T, S_T); |
6172 | set_cdr (S_T, S_T); |
5639 | set_cdr (S_T, S_T); |
6173 | /* init F */ |
5640 | /* init F */ |
6174 | set_typeflag (S_F, T_ATOM | MARK); |
5641 | set_typeflag (S_F, T_ATOM | T_MARK); |
6175 | set_car (S_F, S_F); |
5642 | set_car (S_F, S_F); |
6176 | set_cdr (S_F, S_F); |
5643 | set_cdr (S_F, S_F); |
|
|
5644 | /* init EOF_OBJ */ |
|
|
5645 | set_typeflag (S_EOF, T_ATOM | T_MARK); |
|
|
5646 | set_car (S_EOF, S_EOF); |
|
|
5647 | set_cdr (S_EOF, S_EOF); |
6177 | /* init sink */ |
5648 | /* init sink */ |
6178 | set_typeflag (S_SINK, T_PAIR | MARK); |
5649 | set_typeflag (S_SINK, T_PAIR | T_MARK); |
6179 | set_car (S_SINK, NIL); |
5650 | set_car (S_SINK, NIL); |
|
|
5651 | |
6180 | /* init c_nest */ |
5652 | /* init c_nest */ |
6181 | SCHEME_V->c_nest = NIL; |
5653 | SCHEME_V->c_nest = NIL; |
6182 | |
5654 | |
6183 | SCHEME_V->oblist = oblist_initial_value (SCHEME_A); |
5655 | SCHEME_V->oblist = oblist_initial_value (SCHEME_A); |
6184 | /* init global_env */ |
5656 | /* init global_env */ |
… | |
… | |
6186 | SCHEME_V->global_env = SCHEME_V->envir; |
5658 | SCHEME_V->global_env = SCHEME_V->envir; |
6187 | /* init else */ |
5659 | /* init else */ |
6188 | x = mk_symbol (SCHEME_A_ "else"); |
5660 | x = mk_symbol (SCHEME_A_ "else"); |
6189 | new_slot_in_env (SCHEME_A_ x, S_T); |
5661 | new_slot_in_env (SCHEME_A_ x, S_T); |
6190 | |
5662 | |
6191 | assign_syntax (SCHEME_A_ "lambda"); |
5663 | { |
6192 | assign_syntax (SCHEME_A_ "quote"); |
5664 | static const char *syntax_names[] = { |
6193 | assign_syntax (SCHEME_A_ "define"); |
5665 | "lambda", "quote", "define", "if", "begin", "set!", |
6194 | assign_syntax (SCHEME_A_ "if"); |
5666 | "let", "let*", "letrec", "cond", "delay", "and", |
6195 | assign_syntax (SCHEME_A_ "begin"); |
5667 | "or", "cons-stream", "macro", "case" |
6196 | assign_syntax (SCHEME_A_ "set!"); |
5668 | }; |
6197 | assign_syntax (SCHEME_A_ "let"); |
5669 | |
6198 | assign_syntax (SCHEME_A_ "let*"); |
5670 | for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) |
6199 | assign_syntax (SCHEME_A_ "letrec"); |
|
|
6200 | assign_syntax (SCHEME_A_ "cond"); |
|
|
6201 | assign_syntax (SCHEME_A_ "delay"); |
|
|
6202 | assign_syntax (SCHEME_A_ "and"); |
5671 | assign_syntax (SCHEME_A_ syntax_names[i]); |
6203 | assign_syntax (SCHEME_A_ "or"); |
5672 | } |
6204 | assign_syntax (SCHEME_A_ "cons-stream"); |
|
|
6205 | assign_syntax (SCHEME_A_ "macro"); |
|
|
6206 | assign_syntax (SCHEME_A_ "case"); |
|
|
6207 | |
5673 | |
|
|
5674 | // TODO: should iterate via strlen, to avoid n² complexity |
6208 | for (i = 0; i < n; i++) |
5675 | for (i = 0; i < n; i++) |
6209 | { |
|
|
6210 | if (dispatch_table[i].name != 0) |
5676 | if (dispatch_table[i].builtin) |
6211 | { |
5677 | assign_proc (SCHEME_A_ i, opname (i)); |
6212 | assign_proc (SCHEME_A_ (enum scheme_opcodes) i, dispatch_table[i].name); |
|
|
6213 | } |
|
|
6214 | } |
|
|
6215 | |
5678 | |
6216 | /* initialization of global pointers to special symbols */ |
5679 | /* initialization of global pointers to special symbols */ |
6217 | SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); |
5680 | SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); |
6218 | SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); |
5681 | SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); |
6219 | SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); |
5682 | SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); |
6220 | SCHEME_V->UNQUOTE = mk_symbol (SCHEME_A_ "unquote"); |
5683 | SCHEME_V->UNQUOTE = mk_symbol (SCHEME_A_ "unquote"); |
6221 | SCHEME_V->UNQUOTESP = mk_symbol (SCHEME_A_ "unquote-splicing"); |
5684 | SCHEME_V->UNQUOTESP = mk_symbol (SCHEME_A_ "unquote-splicing"); |
6222 | SCHEME_V->FEED_TO = mk_symbol (SCHEME_A_ "=>"); |
5685 | SCHEME_V->FEED_TO = mk_symbol (SCHEME_A_ "=>"); |
6223 | SCHEME_V->COLON_HOOK = mk_symbol (SCHEME_A_ "*colon-hook*"); |
5686 | SCHEME_V->COLON_HOOK = mk_symbol (SCHEME_A_ "*colon-hook*"); |
6224 | SCHEME_V->ERROR_HOOK = mk_symbol (SCHEME_A_ "*error-hook*"); |
5687 | SCHEME_V->ERROR_HOOK = mk_symbol (SCHEME_A_ "*error-hook*"); |
6225 | SCHEME_V->SHARP_HOOK = mk_symbol (SCHEME_A_ "*sharp-hook*"); |
5688 | SCHEME_V->SHARP_HOOK = mk_symbol (SCHEME_A_ "*sharp-hook*"); |
6226 | SCHEME_V->COMPILE_HOOK = mk_symbol (SCHEME_A_ "*compile-hook*"); |
5689 | SCHEME_V->COMPILE_HOOK = mk_symbol (SCHEME_A_ "*compile-hook*"); |
6227 | |
5690 | |
6228 | return !SCHEME_V->no_memory; |
5691 | return !SCHEME_V->no_memory; |
6229 | } |
5692 | } |
6230 | |
5693 | |
… | |
… | |
6258 | scheme_set_external_data (SCHEME_P_ void *p) |
5721 | scheme_set_external_data (SCHEME_P_ void *p) |
6259 | { |
5722 | { |
6260 | SCHEME_V->ext_data = p; |
5723 | SCHEME_V->ext_data = p; |
6261 | } |
5724 | } |
6262 | |
5725 | |
6263 | void |
5726 | ecb_cold void |
6264 | scheme_deinit (SCHEME_P) |
5727 | scheme_deinit (SCHEME_P) |
6265 | { |
5728 | { |
6266 | int i; |
5729 | int i; |
6267 | |
5730 | |
6268 | #if SHOW_ERROR_LINE |
5731 | #if SHOW_ERROR_LINE |
… | |
… | |
6360 | { |
5823 | { |
6361 | dump_stack_reset (SCHEME_A); |
5824 | dump_stack_reset (SCHEME_A); |
6362 | SCHEME_V->envir = SCHEME_V->global_env; |
5825 | SCHEME_V->envir = SCHEME_V->global_env; |
6363 | SCHEME_V->file_i = 0; |
5826 | SCHEME_V->file_i = 0; |
6364 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5827 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
6365 | SCHEME_V->load_stack[0].rep.string.start = (char *) cmd; /* This func respects const */ |
5828 | SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ |
6366 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *) cmd + strlen (cmd); |
5829 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); |
6367 | SCHEME_V->load_stack[0].rep.string.curr = (char *) cmd; |
5830 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
6368 | #if USE_PORTS |
5831 | #if USE_PORTS |
6369 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5832 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
6370 | #endif |
5833 | #endif |
6371 | SCHEME_V->retcode = 0; |
5834 | SCHEME_V->retcode = 0; |
6372 | SCHEME_V->interactive_repl = 0; |
5835 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
6385 | pointer x; |
5848 | pointer x; |
6386 | |
5849 | |
6387 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
5850 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
6388 | |
5851 | |
6389 | if (x != NIL) |
5852 | if (x != NIL) |
6390 | { |
|
|
6391 | set_slot_in_env (SCHEME_A_ x, value); |
5853 | set_slot_in_env (SCHEME_A_ x, value); |
6392 | } |
|
|
6393 | else |
5854 | else |
6394 | { |
|
|
6395 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
5855 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
6396 | } |
|
|
6397 | } |
5856 | } |
6398 | |
5857 | |
6399 | #if !STANDALONE |
5858 | #if !STANDALONE |
|
|
5859 | |
6400 | void |
5860 | void |
6401 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
5861 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
6402 | { |
5862 | { |
6403 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
5863 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
6404 | } |
5864 | } |
… | |
… | |
6407 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
5867 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
6408 | { |
5868 | { |
6409 | int i; |
5869 | int i; |
6410 | |
5870 | |
6411 | for (i = 0; i < count; i++) |
5871 | for (i = 0; i < count; i++) |
6412 | { |
|
|
6413 | scheme_register_foreign_func (SCHEME_A_ list + i); |
5872 | scheme_register_foreign_func (SCHEME_A_ list + i); |
6414 | } |
|
|
6415 | } |
5873 | } |
6416 | |
5874 | |
6417 | pointer |
5875 | pointer |
6418 | scheme_apply0 (SCHEME_P_ const char *procname) |
5876 | scheme_apply0 (SCHEME_P_ const char *procname) |
6419 | { |
5877 | { |
… | |
… | |
6476 | SCHEME_V->interactive_repl = old_repl; |
5934 | SCHEME_V->interactive_repl = old_repl; |
6477 | restore_from_C_call (SCHEME_A); |
5935 | restore_from_C_call (SCHEME_A); |
6478 | return SCHEME_V->value; |
5936 | return SCHEME_V->value; |
6479 | } |
5937 | } |
6480 | |
5938 | |
6481 | |
|
|
6482 | #endif |
5939 | #endif |
6483 | |
5940 | |
6484 | /* ========== Main ========== */ |
5941 | /* ========== Main ========== */ |
6485 | |
5942 | |
6486 | #if STANDALONE |
5943 | #if STANDALONE |
6487 | |
5944 | |
6488 | # if defined(__APPLE__) && !defined (OSX) |
|
|
6489 | int |
|
|
6490 | main () |
|
|
6491 | { |
|
|
6492 | extern MacTS_main (int argc, char **argv); |
|
|
6493 | char **argv; |
|
|
6494 | int argc = ccommand (&argv); |
|
|
6495 | |
|
|
6496 | MacTS_main (argc, argv); |
|
|
6497 | return 0; |
|
|
6498 | } |
|
|
6499 | |
|
|
6500 | int |
|
|
6501 | MacTS_main (int argc, char **argv) |
|
|
6502 | { |
|
|
6503 | # else |
|
|
6504 | int |
5945 | int |
6505 | main (int argc, char **argv) |
5946 | main (int argc, char **argv) |
6506 | { |
5947 | { |
6507 | # endif |
|
|
6508 | # if USE_MULTIPLICITY |
5948 | # if USE_MULTIPLICITY |
6509 | scheme ssc; |
5949 | scheme ssc; |
6510 | scheme *const sc = &ssc; |
5950 | scheme *const SCHEME_V = &ssc; |
6511 | # else |
5951 | # else |
6512 | # endif |
5952 | # endif |
6513 | int fin; |
5953 | int fin; |
6514 | char *file_name = InitFile; |
5954 | char *file_name = InitFile; |
6515 | int retcode; |
5955 | int retcode; |
6516 | int isfile = 1; |
5956 | int isfile = 1; |
6517 | |
|
|
6518 | if (argc == 1) |
|
|
6519 | xwrstr (banner); |
|
|
6520 | |
5957 | |
6521 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5958 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6522 | { |
5959 | { |
6523 | xwrstr ("Usage: tinyscheme -?\n"); |
5960 | xwrstr ("Usage: tinyscheme -?\n"); |
6524 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
5961 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
… | |
… | |
6547 | if (access (file_name, 0) != 0) |
5984 | if (access (file_name, 0) != 0) |
6548 | { |
5985 | { |
6549 | char *p = getenv ("TINYSCHEMEINIT"); |
5986 | char *p = getenv ("TINYSCHEMEINIT"); |
6550 | |
5987 | |
6551 | if (p != 0) |
5988 | if (p != 0) |
6552 | { |
|
|
6553 | file_name = p; |
5989 | file_name = p; |
6554 | } |
|
|
6555 | } |
5990 | } |
6556 | #endif |
5991 | #endif |
6557 | |
5992 | |
6558 | do |
5993 | do |
6559 | { |
5994 | { |