… | |
… | |
16 | * (MINISCM) This is a revised and modified version by Akira KIDA. |
16 | * (MINISCM) This is a revised and modified version by Akira KIDA. |
17 | * (MINISCM) current version is 0.85k4 (15 May 1994) |
17 | * (MINISCM) current version is 0.85k4 (15 May 1994) |
18 | * |
18 | * |
19 | */ |
19 | */ |
20 | |
20 | |
|
|
21 | #define EXPERIMENT 1 |
|
|
22 | |
|
|
23 | #if 1 |
21 | #define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
24 | #define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
22 | #include "malloc.c" |
25 | #include "malloc.c" |
|
|
26 | #endif |
23 | |
27 | |
24 | #define SCHEME_SOURCE |
28 | #define SCHEME_SOURCE |
25 | #include "scheme-private.h" |
29 | #include "scheme-private.h" |
26 | #ifndef WIN32 |
30 | #ifndef WIN32 |
27 | # include <unistd.h> |
31 | # include <unistd.h> |
… | |
… | |
34 | |
38 | |
35 | #include <sys/types.h> |
39 | #include <sys/types.h> |
36 | #include <sys/stat.h> |
40 | #include <sys/stat.h> |
37 | #include <fcntl.h> |
41 | #include <fcntl.h> |
38 | |
42 | |
|
|
43 | #if !USE_ERROR_CHECKING |
|
|
44 | # define NDEBUG |
|
|
45 | #endif |
|
|
46 | |
|
|
47 | #include <assert.h> |
|
|
48 | #include <stdlib.h> |
39 | #include <string.h> |
49 | #include <string.h> |
40 | #include <stdlib.h> |
|
|
41 | |
50 | |
42 | #include <limits.h> |
51 | #include <limits.h> |
43 | #include <inttypes.h> |
52 | #include <inttypes.h> |
44 | #include <float.h> |
53 | #include <float.h> |
45 | //#include <ctype.h> |
54 | //#include <ctype.h> |
|
|
55 | |
|
|
56 | #if '1' != '0' + 1 \ |
|
|
57 | || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \ |
|
|
58 | || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \ |
|
|
59 | || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \ |
|
|
60 | || 'f' != 'a' + 5 |
|
|
61 | # error "execution character set digits not consecutive" |
|
|
62 | #endif |
46 | |
63 | |
47 | enum { |
64 | enum { |
48 | TOK_EOF, |
65 | TOK_EOF, |
49 | TOK_LPAREN, |
66 | TOK_LPAREN, |
50 | TOK_RPAREN, |
67 | TOK_RPAREN, |
51 | TOK_DOT, |
68 | TOK_DOT, |
52 | TOK_ATOM, |
69 | TOK_ATOM, |
|
|
70 | TOK_DOTATOM, /* atom name starting with '.' */ |
|
|
71 | TOK_STRATOM, /* atom name enclosed in | */ |
53 | TOK_QUOTE, |
72 | TOK_QUOTE, |
54 | TOK_DQUOTE, |
73 | TOK_DQUOTE, |
55 | TOK_BQUOTE, |
74 | TOK_BQUOTE, |
56 | TOK_COMMA, |
75 | TOK_COMMA, |
57 | TOK_ATMARK, |
76 | TOK_ATMARK, |
… | |
… | |
59 | TOK_SHARP_CONST, |
78 | TOK_SHARP_CONST, |
60 | TOK_VEC |
79 | TOK_VEC |
61 | }; |
80 | }; |
62 | |
81 | |
63 | #define BACKQUOTE '`' |
82 | #define BACKQUOTE '`' |
64 | #define DELIMITERS "()\";\f\t\v\n\r " |
83 | #define WHITESPACE " \t\r\n\v\f" |
|
|
84 | #define DELIMITERS "()\";" WHITESPACE |
65 | |
85 | |
66 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
86 | #define NIL POINTER (&SCHEME_V->xNIL) |
67 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
87 | #define S_T POINTER (&SCHEME_V->xT) |
68 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
88 | #define S_F POINTER (&SCHEME_V->xF) |
69 | #define S_SINK (&SCHEME_V->xsink) |
89 | #define S_SINK POINTER (&SCHEME_V->xsink) |
70 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
90 | #define S_EOF POINTER (&SCHEME_V->xEOF_OBJ) |
71 | |
91 | |
72 | #if !USE_MULTIPLICITY |
92 | #if !USE_MULTIPLICITY |
73 | static scheme sc; |
93 | static scheme sc; |
74 | #endif |
94 | #endif |
75 | |
95 | |
76 | static void |
96 | ecb_cold static void |
77 | xbase (char *s, long n, int base) |
97 | xbase (char *s, long n, int base) |
78 | { |
98 | { |
79 | if (n < 0) |
99 | if (n < 0) |
80 | { |
100 | { |
81 | *s++ = '-'; |
101 | *s++ = '-'; |
… | |
… | |
83 | } |
103 | } |
84 | |
104 | |
85 | char *p = s; |
105 | char *p = s; |
86 | |
106 | |
87 | do { |
107 | do { |
88 | *p++ = '0' + n % base; |
108 | *p++ = "0123456789abcdef"[n % base]; |
89 | n /= base; |
109 | n /= base; |
90 | } while (n); |
110 | } while (n); |
91 | |
111 | |
92 | *p-- = 0; |
112 | *p-- = 0; |
93 | |
113 | |
… | |
… | |
96 | char x = *s; *s = *p; *p = x; |
116 | char x = *s; *s = *p; *p = x; |
97 | --p; ++s; |
117 | --p; ++s; |
98 | } |
118 | } |
99 | } |
119 | } |
100 | |
120 | |
101 | static void |
121 | ecb_cold static void |
102 | xnum (char *s, long n) |
122 | xnum (char *s, long n) |
103 | { |
123 | { |
104 | xbase (s, n, 10); |
124 | xbase (s, n, 10); |
105 | } |
125 | } |
106 | |
126 | |
107 | static void |
127 | ecb_cold static void |
108 | xwrstr (const char *s) |
128 | putnum (SCHEME_P_ long n) |
109 | { |
|
|
110 | write (1, s, strlen (s)); |
|
|
111 | } |
|
|
112 | |
|
|
113 | static void |
|
|
114 | xwrnum (long n) |
|
|
115 | { |
129 | { |
116 | char buf[64]; |
130 | char buf[64]; |
117 | |
131 | |
118 | xnum (buf, n); |
132 | xnum (buf, n); |
119 | xwrstr (buf); |
133 | putstr (SCHEME_A_ buf); |
120 | } |
134 | } |
121 | |
135 | |
122 | static char |
136 | ecb_cold static char |
123 | xtoupper (char c) |
137 | xtoupper (char c) |
124 | { |
138 | { |
125 | if (c >= 'a' && c <= 'z') |
139 | if (c >= 'a' && c <= 'z') |
126 | c -= 'a' - 'A'; |
140 | c -= 'a' - 'A'; |
127 | |
141 | |
128 | return c; |
142 | return c; |
129 | } |
143 | } |
130 | |
144 | |
131 | static char |
145 | ecb_cold static char |
132 | xtolower (char c) |
146 | xtolower (char c) |
133 | { |
147 | { |
134 | if (c >= 'A' && c <= 'Z') |
148 | if (c >= 'A' && c <= 'Z') |
135 | c += 'a' - 'A'; |
149 | c += 'a' - 'A'; |
136 | |
150 | |
137 | return c; |
151 | return c; |
138 | } |
152 | } |
139 | |
153 | |
140 | static int |
154 | ecb_cold static int |
141 | xisdigit (char c) |
155 | xisdigit (char c) |
142 | { |
156 | { |
143 | return c >= '0' && c <= '9'; |
157 | return c >= '0' && c <= '9'; |
144 | } |
158 | } |
145 | |
159 | |
146 | #define toupper(c) xtoupper (c) |
160 | #define toupper(c) xtoupper (c) |
147 | #define tolower(c) xtolower (c) |
161 | #define tolower(c) xtolower (c) |
148 | #define isdigit(c) xisdigit (c) |
162 | #define isdigit(c) xisdigit (c) |
149 | |
163 | |
150 | #if USE_IGNORECASE |
164 | #if USE_IGNORECASE |
151 | static const char * |
165 | ecb_cold static const char * |
152 | xstrlwr (char *s) |
166 | xstrlwr (char *s) |
153 | { |
167 | { |
154 | const char *p = s; |
168 | const char *p = s; |
155 | |
169 | |
156 | while (*s) |
170 | while (*s) |
… | |
… | |
176 | |
190 | |
177 | #ifndef InitFile |
191 | #ifndef InitFile |
178 | # define InitFile "init.scm" |
192 | # define InitFile "init.scm" |
179 | #endif |
193 | #endif |
180 | |
194 | |
181 | #ifndef FIRST_CELLSEGS |
|
|
182 | # define FIRST_CELLSEGS 3 |
|
|
183 | #endif |
|
|
184 | |
|
|
185 | enum scheme_types |
195 | enum scheme_types |
186 | { |
196 | { |
187 | T_INTEGER, |
197 | T_INTEGER, |
|
|
198 | T_CHARACTER, |
188 | T_REAL, |
199 | T_REAL, |
189 | T_STRING, |
200 | T_STRING, |
190 | T_SYMBOL, |
201 | T_SYMBOL, |
191 | T_PROC, |
202 | T_PROC, |
192 | T_PAIR, /* also used for free cells */ |
203 | T_PAIR, /* also used for free cells */ |
193 | T_CLOSURE, |
204 | T_CLOSURE, |
|
|
205 | T_MACRO, |
194 | T_CONTINUATION, |
206 | T_CONTINUATION, |
195 | T_FOREIGN, |
207 | T_FOREIGN, |
196 | T_CHARACTER, |
|
|
197 | T_PORT, |
208 | T_PORT, |
198 | T_VECTOR, |
209 | T_VECTOR, |
199 | T_MACRO, |
|
|
200 | T_PROMISE, |
210 | T_PROMISE, |
201 | T_ENVIRONMENT, |
211 | T_ENVIRONMENT, |
202 | /* one more... */ |
212 | /* one more... */ |
203 | T_NUM_SYSTEM_TYPES |
213 | T_NUM_SYSTEM_TYPES |
204 | }; |
214 | }; |
… | |
… | |
240 | static num num_op (enum num_op op, num a, num b); |
250 | static num num_op (enum num_op op, num a, num b); |
241 | static num num_intdiv (num a, num b); |
251 | static num num_intdiv (num a, num b); |
242 | static num num_rem (num a, num b); |
252 | static num num_rem (num a, num b); |
243 | static num num_mod (num a, num b); |
253 | static num num_mod (num a, num b); |
244 | |
254 | |
245 | #if USE_MATH |
|
|
246 | static double round_per_R5RS (double x); |
|
|
247 | #endif |
|
|
248 | static int is_zero_rvalue (RVALUE x); |
255 | static int is_zero_rvalue (RVALUE x); |
249 | |
256 | |
250 | static num num_zero; |
257 | static num num_zero; |
251 | static num num_one; |
258 | static num num_one; |
252 | |
259 | |
|
|
260 | /* convert "pointer" to cell* / cell* to pointer */ |
|
|
261 | #define CELL(p) ((struct cell *)(p) + 0) |
|
|
262 | #define POINTER(c) ((void *)((c) - 0)) |
|
|
263 | |
253 | /* macros for cell operations */ |
264 | /* macros for cell operations */ |
254 | #define typeflag(p) ((p)->flag + 0) |
265 | #define typeflag(p) (CELL(p)->flag + 0) |
255 | #define set_typeflag(p,v) ((p)->flag = (v)) |
266 | #define set_typeflag(p,v) (CELL(p)->flag = (v)) |
256 | #define type(p) (typeflag (p) & T_MASKTYPE) |
267 | #define type(p) (typeflag (p) & T_MASKTYPE) |
257 | |
268 | |
258 | INTERFACE int |
269 | INTERFACE int |
259 | is_string (pointer p) |
270 | is_string (pointer p) |
260 | { |
271 | { |
261 | return type (p) == T_STRING; |
272 | return type (p) == T_STRING; |
262 | } |
273 | } |
263 | |
274 | |
264 | #define strvalue(p) ((p)->object.string.svalue) |
275 | #define strvalue(p) (CELL(p)->object.string.svalue) |
265 | #define strlength(p) ((p)->object.string.length) |
276 | #define strlength(p) (CELL(p)->object.string.length) |
266 | |
277 | |
267 | INTERFACE int |
278 | INTERFACE int |
268 | is_vector (pointer p) |
279 | is_vector (pointer p) |
269 | { |
280 | { |
270 | return type (p) == T_VECTOR; |
281 | return type (p) == T_VECTOR; |
271 | } |
282 | } |
272 | |
283 | |
273 | #define vecvalue(p) ((p)->object.vector.vvalue) |
284 | #define vecvalue(p) (CELL(p)->object.vector.vvalue) |
274 | #define veclength(p) ((p)->object.vector.length) |
285 | #define veclength(p) (CELL(p)->object.vector.length) |
275 | INTERFACE void fill_vector (pointer vec, pointer obj); |
286 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
276 | INTERFACE uint32_t vector_length (pointer vec); |
|
|
277 | INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); |
287 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
278 | INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); |
288 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
279 | |
|
|
280 | INTERFACE uint32_t |
|
|
281 | vector_length (pointer vec) |
|
|
282 | { |
|
|
283 | return vec->object.vector.length; |
|
|
284 | } |
|
|
285 | |
289 | |
286 | INTERFACE int |
290 | INTERFACE int |
287 | is_integer (pointer p) |
291 | is_integer (pointer p) |
288 | { |
292 | { |
289 | return type (p) == T_INTEGER; |
293 | return type (p) == T_INTEGER; |
… | |
… | |
312 | string_value (pointer p) |
316 | string_value (pointer p) |
313 | { |
317 | { |
314 | return strvalue (p); |
318 | return strvalue (p); |
315 | } |
319 | } |
316 | |
320 | |
317 | #define ivalue_unchecked(p) (p)->object.ivalue |
321 | #define ivalue_unchecked(p) CELL(p)->object.ivalue |
318 | #define set_ivalue(p,v) (p)->object.ivalue = (v) |
322 | #define set_ivalue(p,v) CELL(p)->object.ivalue = (v) |
319 | |
323 | |
320 | #if USE_REAL |
324 | #if USE_REAL |
321 | #define rvalue_unchecked(p) (p)->object.rvalue |
325 | #define rvalue_unchecked(p) CELL(p)->object.rvalue |
322 | #define set_rvalue(p,v) (p)->object.rvalue = (v) |
326 | #define set_rvalue(p,v) CELL(p)->object.rvalue = (v) |
323 | #else |
327 | #else |
324 | #define rvalue_unchecked(p) (p)->object.ivalue |
328 | #define rvalue_unchecked(p) CELL(p)->object.ivalue |
325 | #define set_rvalue(p,v) (p)->object.ivalue = (v) |
329 | #define set_rvalue(p,v) CELL(p)->object.ivalue = (v) |
326 | #endif |
330 | #endif |
327 | |
331 | |
328 | INTERFACE long |
332 | INTERFACE long |
329 | charvalue (pointer p) |
333 | charvalue (pointer p) |
330 | { |
334 | { |
331 | return ivalue_unchecked (p); |
335 | return ivalue_unchecked (p); |
332 | } |
336 | } |
333 | |
337 | |
|
|
338 | #define port(p) CELL(p)->object.port |
|
|
339 | #define set_port(p,v) port(p) = (v) |
334 | INTERFACE int |
340 | INTERFACE int |
335 | is_port (pointer p) |
341 | is_port (pointer p) |
336 | { |
342 | { |
337 | return type (p) == T_PORT; |
343 | return type (p) == T_PORT; |
338 | } |
344 | } |
339 | |
345 | |
340 | INTERFACE int |
346 | INTERFACE int |
341 | is_inport (pointer p) |
347 | is_inport (pointer p) |
342 | { |
348 | { |
343 | return is_port (p) && p->object.port->kind & port_input; |
349 | return is_port (p) && port (p)->kind & port_input; |
344 | } |
350 | } |
345 | |
351 | |
346 | INTERFACE int |
352 | INTERFACE int |
347 | is_outport (pointer p) |
353 | is_outport (pointer p) |
348 | { |
354 | { |
349 | return is_port (p) && p->object.port->kind & port_output; |
355 | return is_port (p) && port (p)->kind & port_output; |
350 | } |
356 | } |
351 | |
357 | |
352 | INTERFACE int |
358 | INTERFACE int |
353 | is_pair (pointer p) |
359 | is_pair (pointer p) |
354 | { |
360 | { |
355 | return type (p) == T_PAIR; |
361 | return type (p) == T_PAIR; |
356 | } |
362 | } |
357 | |
363 | |
358 | #define car(p) ((p)->object.cons.car + 0) |
364 | #define car(p) (POINTER (CELL(p)->object.cons.car)) |
359 | #define cdr(p) ((p)->object.cons.cdr + 0) |
365 | #define cdr(p) (POINTER (CELL(p)->object.cons.cdr)) |
360 | |
366 | |
361 | static pointer caar (pointer p) { return car (car (p)); } |
367 | static pointer caar (pointer p) { return car (car (p)); } |
362 | static pointer cadr (pointer p) { return car (cdr (p)); } |
368 | static pointer cadr (pointer p) { return car (cdr (p)); } |
363 | static pointer cdar (pointer p) { return cdr (car (p)); } |
369 | static pointer cdar (pointer p) { return cdr (car (p)); } |
364 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
370 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
… | |
… | |
368 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
374 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
369 | |
375 | |
370 | INTERFACE void |
376 | INTERFACE void |
371 | set_car (pointer p, pointer q) |
377 | set_car (pointer p, pointer q) |
372 | { |
378 | { |
373 | p->object.cons.car = q; |
379 | CELL(p)->object.cons.car = CELL (q); |
374 | } |
380 | } |
375 | |
381 | |
376 | INTERFACE void |
382 | INTERFACE void |
377 | set_cdr (pointer p, pointer q) |
383 | set_cdr (pointer p, pointer q) |
378 | { |
384 | { |
379 | p->object.cons.cdr = q; |
385 | CELL(p)->object.cons.cdr = CELL (q); |
380 | } |
386 | } |
381 | |
387 | |
382 | INTERFACE pointer |
388 | INTERFACE pointer |
383 | pair_car (pointer p) |
389 | pair_car (pointer p) |
384 | { |
390 | { |
… | |
… | |
398 | } |
404 | } |
399 | |
405 | |
400 | INTERFACE char * |
406 | INTERFACE char * |
401 | symname (pointer p) |
407 | symname (pointer p) |
402 | { |
408 | { |
403 | return strvalue (car (p)); |
409 | return strvalue (p); |
404 | } |
410 | } |
405 | |
411 | |
406 | #if USE_PLIST |
412 | #if USE_PLIST |
|
|
413 | #error plists are broken because symbols are no longer pairs |
|
|
414 | #define symprop(p) cdr(p) |
407 | SCHEME_EXPORT int |
415 | SCHEME_EXPORT int |
408 | hasprop (pointer p) |
416 | hasprop (pointer p) |
409 | { |
417 | { |
410 | return typeflag (p) & T_SYMBOL; |
418 | return typeflag (p) & T_SYMBOL; |
411 | } |
419 | } |
412 | |
|
|
413 | # define symprop(p) cdr(p) |
|
|
414 | #endif |
420 | #endif |
415 | |
421 | |
416 | INTERFACE int |
422 | INTERFACE int |
417 | is_syntax (pointer p) |
423 | is_syntax (pointer p) |
418 | { |
424 | { |
… | |
… | |
432 | } |
438 | } |
433 | |
439 | |
434 | INTERFACE char * |
440 | INTERFACE char * |
435 | syntaxname (pointer p) |
441 | syntaxname (pointer p) |
436 | { |
442 | { |
437 | return strvalue (car (p)); |
443 | return strvalue (p); |
438 | } |
444 | } |
439 | |
445 | |
440 | #define procnum(p) ivalue_unchecked (p) |
446 | #define procnum(p) ivalue_unchecked (p) |
441 | static const char *procname (pointer x); |
447 | static const char *procname (pointer x); |
442 | |
448 | |
… | |
… | |
514 | proper list: length |
520 | proper list: length |
515 | circular list: -1 |
521 | circular list: -1 |
516 | not even a pair: -2 |
522 | not even a pair: -2 |
517 | dotted list: -2 minus length before dot |
523 | dotted list: -2 minus length before dot |
518 | */ |
524 | */ |
519 | INTERFACE int |
525 | ecb_hot INTERFACE int |
520 | list_length (SCHEME_P_ pointer a) |
526 | list_length (SCHEME_P_ pointer a) |
521 | { |
527 | { |
522 | int i = 0; |
528 | int i = 0; |
523 | pointer slow, fast; |
529 | pointer slow, fast; |
524 | |
530 | |
… | |
… | |
630 | "gs", |
636 | "gs", |
631 | "rs", |
637 | "rs", |
632 | "us" |
638 | "us" |
633 | }; |
639 | }; |
634 | |
640 | |
635 | static int |
641 | ecb_cold static int |
636 | is_ascii_name (const char *name, int *pc) |
642 | is_ascii_name (const char *name, int *pc) |
637 | { |
643 | { |
638 | int i; |
644 | int i; |
639 | |
645 | |
640 | for (i = 0; i < 32; i++) |
646 | for (i = 0; i < 32; i++) |
… | |
… | |
658 | #endif |
664 | #endif |
659 | |
665 | |
660 | static int file_push (SCHEME_P_ const char *fname); |
666 | static int file_push (SCHEME_P_ const char *fname); |
661 | static void file_pop (SCHEME_P); |
667 | static void file_pop (SCHEME_P); |
662 | static int file_interactive (SCHEME_P); |
668 | static int file_interactive (SCHEME_P); |
663 | ecb_inline int is_one_of (char *s, int c); |
669 | ecb_inline int is_one_of (const char *s, int c); |
664 | static int alloc_cellseg (SCHEME_P_ int n); |
670 | static int alloc_cellseg (SCHEME_P); |
665 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
671 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
666 | static void finalize_cell (SCHEME_P_ pointer a); |
672 | static void finalize_cell (SCHEME_P_ pointer a); |
667 | static int count_consecutive_cells (pointer x, int needed); |
673 | static int count_consecutive_cells (pointer x, int needed); |
668 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
674 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
669 | static pointer mk_number (SCHEME_P_ const num n); |
675 | static pointer mk_number (SCHEME_P_ const num n); |
… | |
… | |
685 | static void mark (pointer a); |
691 | static void mark (pointer a); |
686 | static void gc (SCHEME_P_ pointer a, pointer b); |
692 | static void gc (SCHEME_P_ pointer a, pointer b); |
687 | static int basic_inchar (port *pt); |
693 | static int basic_inchar (port *pt); |
688 | static int inchar (SCHEME_P); |
694 | static int inchar (SCHEME_P); |
689 | static void backchar (SCHEME_P_ int c); |
695 | static void backchar (SCHEME_P_ int c); |
690 | static char *readstr_upto (SCHEME_P_ char *delim); |
696 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
691 | static pointer readstrexp (SCHEME_P); |
697 | static pointer readstrexp (SCHEME_P_ char delim); |
692 | ecb_inline int skipspace (SCHEME_P); |
698 | ecb_inline int skipspace (SCHEME_P); |
693 | static int token (SCHEME_P); |
699 | static int token (SCHEME_P); |
694 | static void printslashstring (SCHEME_P_ char *s, int len); |
700 | static void printslashstring (SCHEME_P_ char *s, int len); |
695 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
701 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
696 | static void printatom (SCHEME_P_ pointer l, int f); |
702 | static void printatom (SCHEME_P_ pointer l, int f); |
… | |
… | |
863 | } |
869 | } |
864 | |
870 | |
865 | return ret; |
871 | return ret; |
866 | } |
872 | } |
867 | |
873 | |
868 | #if USE_MATH |
|
|
869 | |
|
|
870 | /* Round to nearest. Round to even if midway */ |
|
|
871 | static double |
|
|
872 | round_per_R5RS (double x) |
|
|
873 | { |
|
|
874 | double fl = floor (x); |
|
|
875 | double ce = ceil (x); |
|
|
876 | double dfl = x - fl; |
|
|
877 | double dce = ce - x; |
|
|
878 | |
|
|
879 | if (dfl > dce) |
|
|
880 | return ce; |
|
|
881 | else if (dfl < dce) |
|
|
882 | return fl; |
|
|
883 | else |
|
|
884 | { |
|
|
885 | if (fmod (fl, 2) == 0) /* I imagine this holds */ |
|
|
886 | return fl; |
|
|
887 | else |
|
|
888 | return ce; |
|
|
889 | } |
|
|
890 | } |
|
|
891 | #endif |
|
|
892 | |
|
|
893 | static int |
874 | static int |
894 | is_zero_rvalue (RVALUE x) |
875 | is_zero_rvalue (RVALUE x) |
895 | { |
876 | { |
896 | return x == 0; |
877 | return x == 0; |
897 | #if 0 |
878 | #if 0 |
… | |
… | |
902 | #endif |
883 | #endif |
903 | #endif |
884 | #endif |
904 | } |
885 | } |
905 | |
886 | |
906 | /* allocate new cell segment */ |
887 | /* allocate new cell segment */ |
907 | static int |
888 | ecb_cold static int |
908 | alloc_cellseg (SCHEME_P_ int n) |
889 | alloc_cellseg (SCHEME_P) |
909 | { |
890 | { |
910 | pointer newp; |
891 | struct cell *newp; |
911 | pointer last; |
892 | struct cell *last; |
912 | pointer p; |
893 | struct cell *p; |
913 | char *cp; |
894 | char *cp; |
914 | long i; |
895 | long i; |
915 | int k; |
896 | int k; |
916 | |
897 | |
917 | static int segsize = CELL_SEGSIZE >> 1; |
898 | static int segsize = CELL_SEGSIZE >> 1; |
918 | segsize <<= 1; |
899 | segsize <<= 1; |
919 | |
900 | |
920 | for (k = 0; k < n; k++) |
|
|
921 | { |
|
|
922 | if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1) |
|
|
923 | return k; |
|
|
924 | |
|
|
925 | cp = malloc (segsize * sizeof (struct cell)); |
901 | cp = malloc (segsize * sizeof (struct cell)); |
926 | |
902 | |
927 | if (!cp && USE_ERROR_CHECKING) |
903 | if (!cp && USE_ERROR_CHECKING) |
928 | return k; |
904 | return k; |
929 | |
905 | |
930 | i = ++SCHEME_V->last_cell_seg; |
906 | i = ++SCHEME_V->last_cell_seg; |
931 | SCHEME_V->alloc_seg[i] = cp; |
907 | SCHEME_V->alloc_seg[i] = cp; |
932 | |
908 | |
933 | /* insert new segment in address order */ |
909 | newp = (struct cell *)cp; |
934 | newp = (pointer)cp; |
|
|
935 | SCHEME_V->cell_seg[i] = newp; |
910 | SCHEME_V->cell_seg[i] = newp; |
936 | SCHEME_V->cell_segsize[i] = segsize; |
911 | SCHEME_V->cell_segsize[i] = segsize; |
937 | |
|
|
938 | //TODO: insert, not swap |
|
|
939 | while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i]) |
|
|
940 | { |
|
|
941 | p = SCHEME_V->cell_seg[i]; |
|
|
942 | SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1]; |
|
|
943 | SCHEME_V->cell_seg[i - 1] = p; |
|
|
944 | |
|
|
945 | k = SCHEME_V->cell_segsize[i]; |
|
|
946 | SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1]; |
|
|
947 | SCHEME_V->cell_segsize[i - 1] = k; |
|
|
948 | |
|
|
949 | --i; |
|
|
950 | } |
|
|
951 | |
|
|
952 | SCHEME_V->fcells += segsize; |
912 | SCHEME_V->fcells += segsize; |
953 | last = newp + segsize - 1; |
913 | last = newp + segsize - 1; |
954 | |
914 | |
955 | for (p = newp; p <= last; p++) |
915 | for (p = newp; p <= last; p++) |
956 | { |
916 | { |
|
|
917 | pointer cp = POINTER (p); |
957 | set_typeflag (p, T_PAIR); |
918 | set_typeflag (cp, T_PAIR); |
958 | set_car (p, NIL); |
919 | set_car (cp, NIL); |
959 | set_cdr (p, p + 1); |
920 | set_cdr (cp, POINTER (p + 1)); |
960 | } |
921 | } |
961 | |
922 | |
962 | /* insert new cells in address order on free list */ |
|
|
963 | if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell) |
|
|
964 | { |
|
|
965 | set_cdr (last, SCHEME_V->free_cell); |
923 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
966 | SCHEME_V->free_cell = newp; |
924 | SCHEME_V->free_cell = POINTER (newp); |
967 | } |
|
|
968 | else |
|
|
969 | { |
|
|
970 | p = SCHEME_V->free_cell; |
|
|
971 | |
925 | |
972 | while (cdr (p) != NIL && newp > cdr (p)) |
|
|
973 | p = cdr (p); |
|
|
974 | |
|
|
975 | set_cdr (last, cdr (p)); |
|
|
976 | set_cdr (p, newp); |
|
|
977 | } |
|
|
978 | } |
|
|
979 | |
|
|
980 | return n; |
926 | return 1; |
981 | } |
927 | } |
982 | |
928 | |
983 | /* get new cell. parameter a, b is marked by gc. */ |
929 | /* get new cell. parameter a, b is marked by gc. */ |
984 | ecb_inline pointer |
930 | ecb_inline pointer |
985 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
931 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
… | |
… | |
989 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
935 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
990 | return S_SINK; |
936 | return S_SINK; |
991 | |
937 | |
992 | if (SCHEME_V->free_cell == NIL) |
938 | if (SCHEME_V->free_cell == NIL) |
993 | { |
939 | { |
994 | const int min_to_be_recovered = SCHEME_V->last_cell_seg < 128 ? 128 * 8 : SCHEME_V->last_cell_seg * 8; |
940 | const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1; |
995 | |
941 | |
996 | gc (SCHEME_A_ a, b); |
942 | gc (SCHEME_A_ a, b); |
997 | |
943 | |
998 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
944 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
999 | { |
945 | { |
1000 | /* if only a few recovered, get more to avoid fruitless gc's */ |
946 | /* if only a few recovered, get more to avoid fruitless gc's */ |
1001 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
947 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
1002 | { |
948 | { |
1003 | #if USE_ERROR_CHECKING |
949 | #if USE_ERROR_CHECKING |
1004 | SCHEME_V->no_memory = 1; |
950 | SCHEME_V->no_memory = 1; |
1005 | return S_SINK; |
951 | return S_SINK; |
1006 | #endif |
952 | #endif |
… | |
… | |
1018 | } |
964 | } |
1019 | } |
965 | } |
1020 | |
966 | |
1021 | /* To retain recent allocs before interpreter knows about them - |
967 | /* To retain recent allocs before interpreter knows about them - |
1022 | Tehom */ |
968 | Tehom */ |
1023 | |
969 | ecb_hot static void |
1024 | static void |
|
|
1025 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
970 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
1026 | { |
971 | { |
1027 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
972 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
1028 | |
973 | |
1029 | set_typeflag (holder, T_PAIR); |
974 | set_typeflag (holder, T_PAIR); |
… | |
… | |
1031 | set_car (holder, recent); |
976 | set_car (holder, recent); |
1032 | set_cdr (holder, car (S_SINK)); |
977 | set_cdr (holder, car (S_SINK)); |
1033 | set_car (S_SINK, holder); |
978 | set_car (S_SINK, holder); |
1034 | } |
979 | } |
1035 | |
980 | |
1036 | static pointer |
981 | ecb_hot static pointer |
1037 | get_cell (SCHEME_P_ pointer a, pointer b) |
982 | get_cell (SCHEME_P_ pointer a, pointer b) |
1038 | { |
983 | { |
1039 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
984 | pointer cell = get_cell_x (SCHEME_A_ a, b); |
1040 | |
985 | |
1041 | /* For right now, include "a" and "b" in "cell" so that gc doesn't |
986 | /* For right now, include "a" and "b" in "cell" so that gc doesn't |
… | |
… | |
1050 | } |
995 | } |
1051 | |
996 | |
1052 | static pointer |
997 | static pointer |
1053 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
998 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1054 | { |
999 | { |
1055 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
1000 | pointer v = get_cell_x (SCHEME_A_ NIL, NIL); |
1056 | pointer *e = malloc (len * sizeof (pointer)); |
1001 | pointer *e = malloc (len * sizeof (pointer)); |
1057 | |
1002 | |
1058 | if (!e && USE_ERROR_CHECKING) |
1003 | if (!e && USE_ERROR_CHECKING) |
1059 | return S_SINK; |
1004 | return S_SINK; |
1060 | |
1005 | |
1061 | /* Record it as a vector so that gc understands it. */ |
1006 | /* Record it as a vector so that gc understands it. */ |
1062 | set_typeflag (v, T_VECTOR | T_ATOM); |
1007 | set_typeflag (v, T_VECTOR | T_ATOM); |
1063 | |
1008 | |
1064 | v->object.vector.vvalue = e; |
1009 | CELL(v)->object.vector.vvalue = e; |
1065 | v->object.vector.length = len; |
1010 | CELL(v)->object.vector.length = len; |
1066 | fill_vector (v, init); |
1011 | fill_vector (v, 0, init); |
1067 | push_recent_alloc (SCHEME_A_ v, NIL); |
1012 | push_recent_alloc (SCHEME_A_ v, NIL); |
1068 | |
1013 | |
1069 | return v; |
1014 | return v; |
1070 | } |
1015 | } |
1071 | |
1016 | |
… | |
… | |
1079 | static void |
1024 | static void |
1080 | check_cell_alloced (pointer p, int expect_alloced) |
1025 | check_cell_alloced (pointer p, int expect_alloced) |
1081 | { |
1026 | { |
1082 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1027 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1083 | if (typeflag (p) & !expect_alloced) |
1028 | if (typeflag (p) & !expect_alloced) |
1084 | xwrstr ("Cell is already allocated!\n"); |
1029 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1085 | |
1030 | |
1086 | if (!(typeflag (p)) & expect_alloced) |
1031 | if (!(typeflag (p)) & expect_alloced) |
1087 | xwrstr ("Cell is not allocated!\n"); |
1032 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1088 | } |
1033 | } |
1089 | |
1034 | |
1090 | static void |
1035 | static void |
1091 | check_range_alloced (pointer p, int n, int expect_alloced) |
1036 | check_range_alloced (pointer p, int n, int expect_alloced) |
1092 | { |
1037 | { |
… | |
… | |
1098 | #endif |
1043 | #endif |
1099 | |
1044 | |
1100 | /* Medium level cell allocation */ |
1045 | /* Medium level cell allocation */ |
1101 | |
1046 | |
1102 | /* get new cons cell */ |
1047 | /* get new cons cell */ |
1103 | pointer |
1048 | ecb_hot pointer |
1104 | xcons (SCHEME_P_ pointer a, pointer b, int immutable) |
1049 | xcons (SCHEME_P_ pointer a, pointer b, int immutable) |
1105 | { |
1050 | { |
1106 | pointer x = get_cell (SCHEME_A_ a, b); |
1051 | pointer x = get_cell (SCHEME_A_ a, b); |
1107 | |
1052 | |
1108 | set_typeflag (x, T_PAIR); |
1053 | set_typeflag (x, T_PAIR); |
… | |
… | |
1114 | set_cdr (x, b); |
1059 | set_cdr (x, b); |
1115 | |
1060 | |
1116 | return x; |
1061 | return x; |
1117 | } |
1062 | } |
1118 | |
1063 | |
|
|
1064 | ecb_cold static pointer |
|
|
1065 | generate_symbol (SCHEME_P_ const char *name) |
|
|
1066 | { |
|
|
1067 | pointer x = mk_string (SCHEME_A_ name); |
|
|
1068 | setimmutable (x); |
|
|
1069 | set_typeflag (x, T_SYMBOL | T_ATOM); |
|
|
1070 | return x; |
|
|
1071 | } |
|
|
1072 | |
1119 | /* ========== oblist implementation ========== */ |
1073 | /* ========== oblist implementation ========== */ |
1120 | |
1074 | |
1121 | #ifndef USE_OBJECT_LIST |
1075 | #ifndef USE_OBJECT_LIST |
1122 | |
1076 | |
|
|
1077 | static int |
1123 | static int hash_fn (const char *key, int table_size); |
1078 | hash_fn (const char *key, int table_size) |
|
|
1079 | { |
|
|
1080 | const unsigned char *p = key; |
|
|
1081 | uint32_t hash = 2166136261; |
1124 | |
1082 | |
|
|
1083 | while (*p) |
|
|
1084 | hash = (hash ^ *p++) * 16777619; |
|
|
1085 | |
|
|
1086 | return hash % table_size; |
|
|
1087 | } |
|
|
1088 | |
1125 | static pointer |
1089 | ecb_cold static pointer |
1126 | oblist_initial_value (SCHEME_P) |
1090 | oblist_initial_value (SCHEME_P) |
1127 | { |
1091 | { |
1128 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1092 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1129 | } |
1093 | } |
1130 | |
1094 | |
1131 | /* returns the new symbol */ |
1095 | /* returns the new symbol */ |
1132 | static pointer |
1096 | ecb_cold static pointer |
1133 | oblist_add_by_name (SCHEME_P_ const char *name) |
1097 | oblist_add_by_name (SCHEME_P_ const char *name) |
1134 | { |
1098 | { |
1135 | int location; |
1099 | pointer x = generate_symbol (SCHEME_A_ name); |
1136 | |
|
|
1137 | pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1138 | set_typeflag (x, T_SYMBOL); |
|
|
1139 | setimmutable (car (x)); |
|
|
1140 | |
|
|
1141 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1100 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1142 | set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); |
1101 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1143 | return x; |
1102 | return x; |
1144 | } |
1103 | } |
1145 | |
1104 | |
1146 | ecb_inline pointer |
1105 | ecb_cold static pointer |
1147 | oblist_find_by_name (SCHEME_P_ const char *name) |
1106 | oblist_find_by_name (SCHEME_P_ const char *name) |
1148 | { |
1107 | { |
1149 | int location; |
1108 | int location; |
1150 | pointer x; |
1109 | pointer x; |
1151 | char *s; |
1110 | char *s; |
1152 | |
1111 | |
1153 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1112 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1154 | |
1113 | |
1155 | for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1114 | for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1156 | { |
1115 | { |
1157 | s = symname (car (x)); |
1116 | s = symname (car (x)); |
1158 | |
1117 | |
1159 | /* case-insensitive, per R5RS section 2 */ |
1118 | /* case-insensitive, per R5RS section 2 */ |
1160 | if (stricmp (name, s) == 0) |
1119 | if (stricmp (name, s) == 0) |
… | |
… | |
1162 | } |
1121 | } |
1163 | |
1122 | |
1164 | return NIL; |
1123 | return NIL; |
1165 | } |
1124 | } |
1166 | |
1125 | |
1167 | static pointer |
1126 | ecb_cold static pointer |
1168 | oblist_all_symbols (SCHEME_P) |
1127 | oblist_all_symbols (SCHEME_P) |
1169 | { |
1128 | { |
1170 | int i; |
1129 | int i; |
1171 | pointer x; |
1130 | pointer x; |
1172 | pointer ob_list = NIL; |
1131 | pointer ob_list = NIL; |
1173 | |
1132 | |
1174 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1133 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1175 | for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1134 | for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1176 | ob_list = cons (x, ob_list); |
1135 | ob_list = cons (x, ob_list); |
1177 | |
1136 | |
1178 | return ob_list; |
1137 | return ob_list; |
1179 | } |
1138 | } |
1180 | |
1139 | |
1181 | #else |
1140 | #else |
1182 | |
1141 | |
1183 | static pointer |
1142 | ecb_cold static pointer |
1184 | oblist_initial_value (SCHEME_P) |
1143 | oblist_initial_value (SCHEME_P) |
1185 | { |
1144 | { |
1186 | return NIL; |
1145 | return NIL; |
1187 | } |
1146 | } |
1188 | |
1147 | |
1189 | ecb_inline pointer |
1148 | ecb_cold static pointer |
1190 | oblist_find_by_name (SCHEME_P_ const char *name) |
1149 | oblist_find_by_name (SCHEME_P_ const char *name) |
1191 | { |
1150 | { |
1192 | pointer x; |
1151 | pointer x; |
1193 | char *s; |
1152 | char *s; |
1194 | |
1153 | |
… | |
… | |
1203 | |
1162 | |
1204 | return NIL; |
1163 | return NIL; |
1205 | } |
1164 | } |
1206 | |
1165 | |
1207 | /* returns the new symbol */ |
1166 | /* returns the new symbol */ |
1208 | static pointer |
1167 | ecb_cold static pointer |
1209 | oblist_add_by_name (SCHEME_P_ const char *name) |
1168 | oblist_add_by_name (SCHEME_P_ const char *name) |
1210 | { |
1169 | { |
1211 | pointer x; |
1170 | pointer x = generate_symbol (SCHEME_A_ name); |
1212 | |
|
|
1213 | x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1214 | set_typeflag (x, T_SYMBOL); |
|
|
1215 | setimmutable (car (x)); |
|
|
1216 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1171 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1217 | return x; |
1172 | return x; |
1218 | } |
1173 | } |
1219 | |
1174 | |
1220 | static pointer |
1175 | ecb_cold static pointer |
1221 | oblist_all_symbols (SCHEME_P) |
1176 | oblist_all_symbols (SCHEME_P) |
1222 | { |
1177 | { |
1223 | return SCHEME_V->oblist; |
1178 | return SCHEME_V->oblist; |
1224 | } |
1179 | } |
1225 | |
1180 | |
1226 | #endif |
1181 | #endif |
1227 | |
1182 | |
1228 | #if USE_PORTS |
1183 | #if USE_PORTS |
1229 | static pointer |
1184 | ecb_cold static pointer |
1230 | mk_port (SCHEME_P_ port *p) |
1185 | mk_port (SCHEME_P_ port *p) |
1231 | { |
1186 | { |
1232 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1187 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1233 | |
1188 | |
1234 | set_typeflag (x, T_PORT | T_ATOM); |
1189 | set_typeflag (x, T_PORT | T_ATOM); |
1235 | x->object.port = p; |
1190 | set_port (x, p); |
1236 | |
1191 | |
1237 | return x; |
1192 | return x; |
1238 | } |
1193 | } |
1239 | #endif |
1194 | #endif |
1240 | |
1195 | |
1241 | pointer |
1196 | ecb_cold pointer |
1242 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1197 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1243 | { |
1198 | { |
1244 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1199 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1245 | |
1200 | |
1246 | set_typeflag (x, (T_FOREIGN | T_ATOM)); |
1201 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1247 | x->object.ff = f; |
1202 | CELL(x)->object.ff = f; |
1248 | |
1203 | |
1249 | return x; |
1204 | return x; |
1250 | } |
1205 | } |
1251 | |
1206 | |
1252 | INTERFACE pointer |
1207 | INTERFACE pointer |
1253 | mk_character (SCHEME_P_ int c) |
1208 | mk_character (SCHEME_P_ int c) |
1254 | { |
1209 | { |
1255 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1210 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1256 | |
1211 | |
1257 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1212 | set_typeflag (x, T_CHARACTER | T_ATOM); |
1258 | set_ivalue (x, c & 0xff); |
1213 | set_ivalue (x, c & 0xff); |
1259 | |
1214 | |
1260 | return x; |
1215 | return x; |
1261 | } |
1216 | } |
1262 | |
1217 | |
1263 | /* get number atom (integer) */ |
1218 | /* get number atom (integer) */ |
1264 | INTERFACE pointer |
1219 | INTERFACE pointer |
1265 | mk_integer (SCHEME_P_ long n) |
1220 | mk_integer (SCHEME_P_ long n) |
1266 | { |
1221 | { |
|
|
1222 | pointer p = 0; |
|
|
1223 | pointer *pp = &p; |
|
|
1224 | |
|
|
1225 | #if USE_INTCACHE |
|
|
1226 | if (n >= INTCACHE_MIN && n <= INTCACHE_MAX) |
|
|
1227 | pp = &SCHEME_V->intcache[n - INTCACHE_MIN]; |
|
|
1228 | #endif |
|
|
1229 | |
|
|
1230 | if (!*pp) |
|
|
1231 | { |
1267 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1232 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1268 | |
1233 | |
1269 | set_typeflag (x, (T_INTEGER | T_ATOM)); |
1234 | set_typeflag (x, T_INTEGER | T_ATOM); |
|
|
1235 | setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */ |
1270 | set_ivalue (x, n); |
1236 | set_ivalue (x, n); |
1271 | |
1237 | |
|
|
1238 | *pp = x; |
|
|
1239 | } |
|
|
1240 | |
1272 | return x; |
1241 | return *pp; |
1273 | } |
1242 | } |
1274 | |
1243 | |
1275 | INTERFACE pointer |
1244 | INTERFACE pointer |
1276 | mk_real (SCHEME_P_ RVALUE n) |
1245 | mk_real (SCHEME_P_ RVALUE n) |
1277 | { |
1246 | { |
1278 | #if USE_REAL |
1247 | #if USE_REAL |
1279 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1248 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1280 | |
1249 | |
1281 | set_typeflag (x, (T_REAL | T_ATOM)); |
1250 | set_typeflag (x, T_REAL | T_ATOM); |
1282 | set_rvalue (x, n); |
1251 | set_rvalue (x, n); |
1283 | |
1252 | |
1284 | return x; |
1253 | return x; |
1285 | #else |
1254 | #else |
1286 | return mk_integer (SCHEME_A_ n); |
1255 | return mk_integer (SCHEME_A_ n); |
… | |
… | |
1310 | SCHEME_V->no_memory = 1; |
1279 | SCHEME_V->no_memory = 1; |
1311 | return SCHEME_V->strbuff; |
1280 | return SCHEME_V->strbuff; |
1312 | } |
1281 | } |
1313 | |
1282 | |
1314 | if (str) |
1283 | if (str) |
1315 | { |
1284 | memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */ |
1316 | int l = strlen (str); |
|
|
1317 | |
|
|
1318 | if (l > len_str) |
|
|
1319 | l = len_str; |
|
|
1320 | |
|
|
1321 | memcpy (q, str, l); |
|
|
1322 | q[l] = 0; |
|
|
1323 | } |
|
|
1324 | else |
1285 | else |
1325 | { |
|
|
1326 | memset (q, fill, len_str); |
1286 | memset (q, fill, len_str); |
|
|
1287 | |
1327 | q[len_str] = 0; |
1288 | q[len_str] = 0; |
1328 | } |
|
|
1329 | |
1289 | |
1330 | return q; |
1290 | return q; |
1331 | } |
1291 | } |
1332 | |
1292 | |
1333 | INTERFACE pointer |
1293 | INTERFACE pointer |
… | |
… | |
1347 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1307 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1348 | |
1308 | |
1349 | set_typeflag (x, T_STRING | T_ATOM); |
1309 | set_typeflag (x, T_STRING | T_ATOM); |
1350 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1310 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1351 | strlength (x) = len; |
1311 | strlength (x) = len; |
|
|
1312 | |
1352 | return x; |
1313 | return x; |
1353 | } |
1314 | } |
1354 | |
1315 | |
1355 | INTERFACE pointer |
1316 | INTERFACE pointer |
1356 | mk_string (SCHEME_P_ const char *str) |
1317 | mk_string (SCHEME_P_ const char *str) |
… | |
… | |
1363 | { |
1324 | { |
1364 | return get_vector_object (SCHEME_A_ len, NIL); |
1325 | return get_vector_object (SCHEME_A_ len, NIL); |
1365 | } |
1326 | } |
1366 | |
1327 | |
1367 | INTERFACE void |
1328 | INTERFACE void |
1368 | fill_vector (pointer vec, pointer obj) |
1329 | fill_vector (pointer vec, uint32_t start, pointer obj) |
1369 | { |
1330 | { |
1370 | int i; |
1331 | int i; |
1371 | |
1332 | |
1372 | for (i = 0; i < vec->object.vector.length; i++) |
1333 | for (i = start; i < veclength (vec); i++) |
1373 | vecvalue (vec)[i] = obj; |
1334 | vecvalue (vec)[i] = obj; |
1374 | } |
1335 | } |
1375 | |
1336 | |
|
|
1337 | INTERFACE void |
|
|
1338 | vector_resize (pointer vec, uint32_t newsize, pointer fill) |
|
|
1339 | { |
|
|
1340 | uint32_t oldsize = veclength (vec); |
|
|
1341 | vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer)); |
|
|
1342 | veclength (vec) = newsize; |
|
|
1343 | fill_vector (vec, oldsize, fill); |
|
|
1344 | } |
|
|
1345 | |
1376 | INTERFACE pointer |
1346 | INTERFACE pointer |
1377 | vector_elem (pointer vec, uint32_t ielem) |
1347 | vector_get (pointer vec, uint32_t ielem) |
1378 | { |
1348 | { |
1379 | return vecvalue(vec)[ielem]; |
1349 | return vecvalue(vec)[ielem]; |
1380 | } |
1350 | } |
1381 | |
1351 | |
1382 | INTERFACE void |
1352 | INTERFACE void |
1383 | set_vector_elem (pointer vec, uint32_t ielem, pointer a) |
1353 | vector_set (pointer vec, uint32_t ielem, pointer a) |
1384 | { |
1354 | { |
1385 | vecvalue(vec)[ielem] = a; |
1355 | vecvalue(vec)[ielem] = a; |
1386 | } |
1356 | } |
1387 | |
1357 | |
1388 | /* get new symbol */ |
1358 | /* get new symbol */ |
… | |
… | |
1396 | x = oblist_add_by_name (SCHEME_A_ name); |
1366 | x = oblist_add_by_name (SCHEME_A_ name); |
1397 | |
1367 | |
1398 | return x; |
1368 | return x; |
1399 | } |
1369 | } |
1400 | |
1370 | |
1401 | INTERFACE pointer |
1371 | ecb_cold INTERFACE pointer |
1402 | gensym (SCHEME_P) |
1372 | gensym (SCHEME_P) |
1403 | { |
1373 | { |
1404 | pointer x; |
1374 | pointer x; |
1405 | |
|
|
1406 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
|
|
1407 | { |
|
|
1408 | char name[40] = "gensym-"; |
1375 | char name[40] = "gensym-"; |
1409 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1376 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1410 | |
1377 | |
1411 | /* first check oblist */ |
1378 | return generate_symbol (SCHEME_A_ name); |
1412 | x = oblist_find_by_name (SCHEME_A_ name); |
1379 | } |
1413 | |
1380 | |
1414 | if (x == NIL) |
1381 | static int |
1415 | { |
1382 | is_gensym (SCHEME_P_ pointer x) |
1416 | x = oblist_add_by_name (SCHEME_A_ name); |
1383 | { |
1417 | return x; |
1384 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1418 | } |
|
|
1419 | } |
|
|
1420 | |
|
|
1421 | return NIL; |
|
|
1422 | } |
1385 | } |
1423 | |
1386 | |
1424 | /* make symbol or number atom from string */ |
1387 | /* make symbol or number atom from string */ |
1425 | static pointer |
1388 | ecb_cold static pointer |
1426 | mk_atom (SCHEME_P_ char *q) |
1389 | mk_atom (SCHEME_P_ char *q) |
1427 | { |
1390 | { |
1428 | char c, *p; |
1391 | char c, *p; |
1429 | int has_dec_point = 0; |
1392 | int has_dec_point = 0; |
1430 | int has_fp_exp = 0; |
1393 | int has_fp_exp = 0; |
… | |
… | |
1501 | |
1464 | |
1502 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1465 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1503 | } |
1466 | } |
1504 | |
1467 | |
1505 | /* make constant */ |
1468 | /* make constant */ |
1506 | static pointer |
1469 | ecb_cold static pointer |
1507 | mk_sharp_const (SCHEME_P_ char *name) |
1470 | mk_sharp_const (SCHEME_P_ char *name) |
1508 | { |
1471 | { |
1509 | if (!strcmp (name, "t")) |
1472 | if (!strcmp (name, "t")) |
1510 | return S_T; |
1473 | return S_T; |
1511 | else if (!strcmp (name, "f")) |
1474 | else if (!strcmp (name, "f")) |
1512 | return S_F; |
1475 | return S_F; |
1513 | else if (*name == '\\') /* #\w (character) */ |
1476 | else if (*name == '\\') /* #\w (character) */ |
1514 | { |
1477 | { |
1515 | int c; |
1478 | int c; |
1516 | |
1479 | |
|
|
1480 | // TODO: optimise |
1517 | if (stricmp (name + 1, "space") == 0) |
1481 | if (stricmp (name + 1, "space") == 0) |
1518 | c = ' '; |
1482 | c = ' '; |
1519 | else if (stricmp (name + 1, "newline") == 0) |
1483 | else if (stricmp (name + 1, "newline") == 0) |
1520 | c = '\n'; |
1484 | c = '\n'; |
1521 | else if (stricmp (name + 1, "return") == 0) |
1485 | else if (stricmp (name + 1, "return") == 0) |
1522 | c = '\r'; |
1486 | c = '\r'; |
1523 | else if (stricmp (name + 1, "tab") == 0) |
1487 | else if (stricmp (name + 1, "tab") == 0) |
1524 | c = '\t'; |
1488 | c = '\t'; |
|
|
1489 | else if (stricmp (name + 1, "alarm") == 0) |
|
|
1490 | c = 0x07; |
|
|
1491 | else if (stricmp (name + 1, "backspace") == 0) |
|
|
1492 | c = 0x08; |
|
|
1493 | else if (stricmp (name + 1, "escape") == 0) |
|
|
1494 | c = 0x1b; |
|
|
1495 | else if (stricmp (name + 1, "delete") == 0) |
|
|
1496 | c = 0x7f; |
|
|
1497 | else if (stricmp (name + 1, "null") == 0) |
|
|
1498 | c = 0; |
1525 | else if (name[1] == 'x' && name[2] != 0) |
1499 | else if (name[1] == 'x' && name[2] != 0) |
1526 | { |
1500 | { |
1527 | long c1 = strtol (name + 2, 0, 16); |
1501 | long c1 = strtol (name + 2, 0, 16); |
1528 | |
1502 | |
1529 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1503 | if (0 <= c1 && c1 <= UCHAR_MAX) |
… | |
… | |
1554 | return NIL; |
1528 | return NIL; |
1555 | } |
1529 | } |
1556 | } |
1530 | } |
1557 | |
1531 | |
1558 | /* ========== garbage collector ========== */ |
1532 | /* ========== garbage collector ========== */ |
|
|
1533 | |
|
|
1534 | static void |
|
|
1535 | finalize_cell (SCHEME_P_ pointer a) |
|
|
1536 | { |
|
|
1537 | /* TODO, fast bitmap check? */ |
|
|
1538 | if (is_string (a) || is_symbol (a)) |
|
|
1539 | free (strvalue (a)); |
|
|
1540 | else if (is_vector (a)) |
|
|
1541 | free (vecvalue (a)); |
|
|
1542 | #if USE_PORTS |
|
|
1543 | else if (is_port (a)) |
|
|
1544 | { |
|
|
1545 | if (port(a)->kind & port_file && port (a)->rep.stdio.closeit) |
|
|
1546 | port_close (SCHEME_A_ a, port_input | port_output); |
|
|
1547 | |
|
|
1548 | free (port (a)); |
|
|
1549 | } |
|
|
1550 | #endif |
|
|
1551 | } |
1559 | |
1552 | |
1560 | /*-- |
1553 | /*-- |
1561 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1554 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1562 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1555 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1563 | * for marking. |
1556 | * for marking. |
1564 | * |
1557 | * |
1565 | * The exception is vectors - vectors are currently marked recursively, |
1558 | * The exception is vectors - vectors are currently marked recursively, |
1566 | * which is inherited form tinyscheme and could be fixed by having another |
1559 | * which is inherited form tinyscheme and could be fixed by having another |
1567 | * word of context in the vector |
1560 | * word of context in the vector |
1568 | */ |
1561 | */ |
1569 | static void |
1562 | ecb_hot static void |
1570 | mark (pointer a) |
1563 | mark (pointer a) |
1571 | { |
1564 | { |
1572 | pointer t, q, p; |
1565 | pointer t, q, p; |
1573 | |
1566 | |
1574 | t = 0; |
1567 | t = 0; |
… | |
… | |
1578 | |
1571 | |
1579 | if (ecb_expect_false (is_vector (p))) |
1572 | if (ecb_expect_false (is_vector (p))) |
1580 | { |
1573 | { |
1581 | int i; |
1574 | int i; |
1582 | |
1575 | |
1583 | for (i = 0; i < p->object.vector.length; i++) |
1576 | for (i = 0; i < veclength (p); i++) |
1584 | mark (vecvalue (p)[i]); |
1577 | mark (vecvalue (p)[i]); |
1585 | } |
1578 | } |
1586 | |
1579 | |
1587 | if (is_atom (p)) |
1580 | if (is_atom (p)) |
1588 | goto E6; |
1581 | goto E6; |
… | |
… | |
1631 | p = q; |
1624 | p = q; |
1632 | goto E6; |
1625 | goto E6; |
1633 | } |
1626 | } |
1634 | } |
1627 | } |
1635 | |
1628 | |
|
|
1629 | ecb_hot static void |
|
|
1630 | gc_free (SCHEME_P) |
|
|
1631 | { |
|
|
1632 | int i; |
|
|
1633 | uint32_t total = 0; |
|
|
1634 | |
|
|
1635 | /* Here we scan the cells to build the free-list. */ |
|
|
1636 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
|
|
1637 | { |
|
|
1638 | struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1639 | struct cell *p; |
|
|
1640 | total += SCHEME_V->cell_segsize [i]; |
|
|
1641 | |
|
|
1642 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
|
|
1643 | { |
|
|
1644 | pointer c = POINTER (p); |
|
|
1645 | |
|
|
1646 | if (is_mark (c)) |
|
|
1647 | clrmark (c); |
|
|
1648 | else |
|
|
1649 | { |
|
|
1650 | /* reclaim cell */ |
|
|
1651 | if (typeflag (c) != T_PAIR) |
|
|
1652 | { |
|
|
1653 | finalize_cell (SCHEME_A_ c); |
|
|
1654 | set_typeflag (c, T_PAIR); |
|
|
1655 | set_car (c, NIL); |
|
|
1656 | } |
|
|
1657 | |
|
|
1658 | ++SCHEME_V->fcells; |
|
|
1659 | set_cdr (c, SCHEME_V->free_cell); |
|
|
1660 | SCHEME_V->free_cell = c; |
|
|
1661 | } |
|
|
1662 | } |
|
|
1663 | } |
|
|
1664 | |
|
|
1665 | if (SCHEME_V->gc_verbose) |
|
|
1666 | { |
|
|
1667 | putstr (SCHEME_A_ "done: "); putnum (SCHEME_A_ SCHEME_V->fcells); putstr (SCHEME_A_ " out of "); putnum (SCHEME_A_ total); putstr (SCHEME_A_ " cells were recovered.\n"); |
|
|
1668 | } |
|
|
1669 | } |
|
|
1670 | |
1636 | /* garbage collection. parameter a, b is marked. */ |
1671 | /* garbage collection. parameter a, b is marked. */ |
1637 | static void |
1672 | ecb_cold static void |
1638 | gc (SCHEME_P_ pointer a, pointer b) |
1673 | gc (SCHEME_P_ pointer a, pointer b) |
1639 | { |
1674 | { |
1640 | pointer p; |
|
|
1641 | int i; |
1675 | int i; |
1642 | |
1676 | |
1643 | if (SCHEME_V->gc_verbose) |
1677 | if (SCHEME_V->gc_verbose) |
1644 | putstr (SCHEME_A_ "gc..."); |
1678 | putstr (SCHEME_A_ "gc..."); |
1645 | |
1679 | |
… | |
… | |
1661 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1695 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1662 | mark (car (S_SINK)); |
1696 | mark (car (S_SINK)); |
1663 | /* Mark any older stuff above nested C calls */ |
1697 | /* Mark any older stuff above nested C calls */ |
1664 | mark (SCHEME_V->c_nest); |
1698 | mark (SCHEME_V->c_nest); |
1665 | |
1699 | |
|
|
1700 | #if USE_INTCACHE |
|
|
1701 | /* mark intcache */ |
|
|
1702 | for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i) |
|
|
1703 | if (SCHEME_V->intcache[i - INTCACHE_MIN]) |
|
|
1704 | mark (SCHEME_V->intcache[i - INTCACHE_MIN]); |
|
|
1705 | #endif |
|
|
1706 | |
1666 | /* mark variables a, b */ |
1707 | /* mark variables a, b */ |
1667 | mark (a); |
1708 | mark (a); |
1668 | mark (b); |
1709 | mark (b); |
1669 | |
1710 | |
1670 | /* garbage collect */ |
1711 | /* garbage collect */ |
1671 | clrmark (NIL); |
1712 | clrmark (NIL); |
1672 | SCHEME_V->fcells = 0; |
1713 | SCHEME_V->fcells = 0; |
1673 | SCHEME_V->free_cell = NIL; |
1714 | SCHEME_V->free_cell = NIL; |
1674 | |
1715 | |
1675 | /* free-list is kept sorted by address so as to maintain consecutive |
|
|
1676 | ranges, if possible, for use with vectors. Here we scan the cells |
|
|
1677 | (which are also kept sorted by address) downwards to build the |
|
|
1678 | free-list in sorted order. |
|
|
1679 | */ |
|
|
1680 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
|
|
1681 | { |
|
|
1682 | p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1683 | |
|
|
1684 | while (--p >= SCHEME_V->cell_seg[i]) |
|
|
1685 | { |
|
|
1686 | if (is_mark (p)) |
|
|
1687 | clrmark (p); |
|
|
1688 | else |
|
|
1689 | { |
|
|
1690 | /* reclaim cell */ |
|
|
1691 | if (typeflag (p) != T_PAIR) |
|
|
1692 | { |
|
|
1693 | finalize_cell (SCHEME_A_ p); |
|
|
1694 | set_typeflag (p, T_PAIR); |
|
|
1695 | set_car (p, NIL); |
|
|
1696 | } |
|
|
1697 | |
|
|
1698 | ++SCHEME_V->fcells; |
|
|
1699 | set_cdr (p, SCHEME_V->free_cell); |
|
|
1700 | SCHEME_V->free_cell = p; |
|
|
1701 | } |
|
|
1702 | } |
|
|
1703 | } |
|
|
1704 | |
|
|
1705 | if (SCHEME_V->gc_verbose) |
1716 | if (SCHEME_V->gc_verbose) |
1706 | { |
1717 | putstr (SCHEME_A_ "freeing..."); |
1707 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); |
|
|
1708 | } |
|
|
1709 | } |
|
|
1710 | |
1718 | |
1711 | static void |
1719 | gc_free (SCHEME_A); |
1712 | finalize_cell (SCHEME_P_ pointer a) |
|
|
1713 | { |
|
|
1714 | /* TODO, fast bitmap check? */ |
|
|
1715 | if (is_string (a)) |
|
|
1716 | free (strvalue (a)); |
|
|
1717 | else if (is_vector (a)) |
|
|
1718 | free (vecvalue (a)); |
|
|
1719 | #if USE_PORTS |
|
|
1720 | else if (is_port (a)) |
|
|
1721 | { |
|
|
1722 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
|
|
1723 | port_close (SCHEME_A_ a, port_input | port_output); |
|
|
1724 | |
|
|
1725 | free (a->object.port); |
|
|
1726 | } |
|
|
1727 | #endif |
|
|
1728 | } |
1720 | } |
1729 | |
1721 | |
1730 | /* ========== Routines for Reading ========== */ |
1722 | /* ========== Routines for Reading ========== */ |
1731 | |
1723 | |
1732 | static int |
1724 | ecb_cold static int |
1733 | file_push (SCHEME_P_ const char *fname) |
1725 | file_push (SCHEME_P_ const char *fname) |
1734 | { |
1726 | { |
1735 | #if USE_PORTS |
1727 | #if USE_PORTS |
1736 | int fin; |
1728 | int fin; |
1737 | |
1729 | |
… | |
… | |
1746 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1738 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1747 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1739 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1748 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; |
1740 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; |
1749 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1741 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1750 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1742 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1751 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1743 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1752 | |
1744 | |
1753 | #if SHOW_ERROR_LINE |
1745 | #if SHOW_ERROR_LINE |
1754 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1746 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1755 | |
1747 | |
1756 | if (fname) |
1748 | if (fname) |
… | |
… | |
1763 | #else |
1755 | #else |
1764 | return 1; |
1756 | return 1; |
1765 | #endif |
1757 | #endif |
1766 | } |
1758 | } |
1767 | |
1759 | |
1768 | static void |
1760 | ecb_cold static void |
1769 | file_pop (SCHEME_P) |
1761 | file_pop (SCHEME_P) |
1770 | { |
1762 | { |
1771 | if (SCHEME_V->file_i != 0) |
1763 | if (SCHEME_V->file_i != 0) |
1772 | { |
1764 | { |
1773 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1765 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1774 | #if USE_PORTS |
1766 | #if USE_PORTS |
1775 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1767 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1776 | #endif |
1768 | #endif |
1777 | SCHEME_V->file_i--; |
1769 | SCHEME_V->file_i--; |
1778 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1770 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1779 | } |
1771 | } |
1780 | } |
1772 | } |
1781 | |
1773 | |
1782 | static int |
1774 | ecb_cold static int |
1783 | file_interactive (SCHEME_P) |
1775 | file_interactive (SCHEME_P) |
1784 | { |
1776 | { |
1785 | #if USE_PORTS |
1777 | #if USE_PORTS |
1786 | return SCHEME_V->file_i == 0 |
1778 | return SCHEME_V->file_i == 0 |
1787 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1779 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1788 | && (SCHEME_V->inport->object.port->kind & port_file); |
1780 | && (port (SCHEME_V->inport)->kind & port_file); |
1789 | #else |
1781 | #else |
1790 | return 0; |
1782 | return 0; |
1791 | #endif |
1783 | #endif |
1792 | } |
1784 | } |
1793 | |
1785 | |
1794 | #if USE_PORTS |
1786 | #if USE_PORTS |
1795 | static port * |
1787 | ecb_cold static port * |
1796 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1788 | port_rep_from_filename (SCHEME_P_ const char *fn, int prop) |
1797 | { |
1789 | { |
1798 | int fd; |
1790 | int fd; |
1799 | int flags; |
1791 | int flags; |
1800 | char *rw; |
1792 | char *rw; |
… | |
… | |
1823 | # endif |
1815 | # endif |
1824 | |
1816 | |
1825 | return pt; |
1817 | return pt; |
1826 | } |
1818 | } |
1827 | |
1819 | |
1828 | static pointer |
1820 | ecb_cold static pointer |
1829 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1821 | port_from_filename (SCHEME_P_ const char *fn, int prop) |
1830 | { |
1822 | { |
1831 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1823 | port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); |
1832 | |
1824 | |
1833 | if (!pt && USE_ERROR_CHECKING) |
1825 | if (!pt && USE_ERROR_CHECKING) |
1834 | return NIL; |
1826 | return NIL; |
1835 | |
1827 | |
1836 | return mk_port (SCHEME_A_ pt); |
1828 | return mk_port (SCHEME_A_ pt); |
1837 | } |
1829 | } |
1838 | |
1830 | |
1839 | static port * |
1831 | ecb_cold static port * |
1840 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1832 | port_rep_from_file (SCHEME_P_ int f, int prop) |
1841 | { |
1833 | { |
1842 | port *pt = malloc (sizeof *pt); |
1834 | port *pt = malloc (sizeof *pt); |
1843 | |
1835 | |
1844 | if (!pt && USE_ERROR_CHECKING) |
1836 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1849 | pt->rep.stdio.file = f; |
1841 | pt->rep.stdio.file = f; |
1850 | pt->rep.stdio.closeit = 0; |
1842 | pt->rep.stdio.closeit = 0; |
1851 | return pt; |
1843 | return pt; |
1852 | } |
1844 | } |
1853 | |
1845 | |
1854 | static pointer |
1846 | ecb_cold static pointer |
1855 | port_from_file (SCHEME_P_ int f, int prop) |
1847 | port_from_file (SCHEME_P_ int f, int prop) |
1856 | { |
1848 | { |
1857 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1849 | port *pt = port_rep_from_file (SCHEME_A_ f, prop); |
1858 | |
1850 | |
1859 | if (!pt && USE_ERROR_CHECKING) |
1851 | if (!pt && USE_ERROR_CHECKING) |
1860 | return NIL; |
1852 | return NIL; |
1861 | |
1853 | |
1862 | return mk_port (SCHEME_A_ pt); |
1854 | return mk_port (SCHEME_A_ pt); |
1863 | } |
1855 | } |
1864 | |
1856 | |
1865 | static port * |
1857 | ecb_cold static port * |
1866 | port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1858 | port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1867 | { |
1859 | { |
1868 | port *pt = malloc (sizeof (port)); |
1860 | port *pt = malloc (sizeof (port)); |
1869 | |
1861 | |
1870 | if (!pt && USE_ERROR_CHECKING) |
1862 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1876 | pt->rep.string.curr = start; |
1868 | pt->rep.string.curr = start; |
1877 | pt->rep.string.past_the_end = past_the_end; |
1869 | pt->rep.string.past_the_end = past_the_end; |
1878 | return pt; |
1870 | return pt; |
1879 | } |
1871 | } |
1880 | |
1872 | |
1881 | static pointer |
1873 | ecb_cold static pointer |
1882 | port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1874 | port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) |
1883 | { |
1875 | { |
1884 | port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); |
1876 | port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); |
1885 | |
1877 | |
1886 | if (!pt && USE_ERROR_CHECKING) |
1878 | if (!pt && USE_ERROR_CHECKING) |
… | |
… | |
1889 | return mk_port (SCHEME_A_ pt); |
1881 | return mk_port (SCHEME_A_ pt); |
1890 | } |
1882 | } |
1891 | |
1883 | |
1892 | # define BLOCK_SIZE 256 |
1884 | # define BLOCK_SIZE 256 |
1893 | |
1885 | |
1894 | static port * |
1886 | ecb_cold static port * |
1895 | port_rep_from_scratch (SCHEME_P) |
1887 | port_rep_from_scratch (SCHEME_P) |
1896 | { |
1888 | { |
1897 | char *start; |
1889 | char *start; |
1898 | port *pt = malloc (sizeof (port)); |
1890 | port *pt = malloc (sizeof (port)); |
1899 | |
1891 | |
… | |
… | |
1913 | pt->rep.string.curr = start; |
1905 | pt->rep.string.curr = start; |
1914 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1906 | pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; |
1915 | return pt; |
1907 | return pt; |
1916 | } |
1908 | } |
1917 | |
1909 | |
1918 | static pointer |
1910 | ecb_cold static pointer |
1919 | port_from_scratch (SCHEME_P) |
1911 | port_from_scratch (SCHEME_P) |
1920 | { |
1912 | { |
1921 | port *pt = port_rep_from_scratch (SCHEME_A); |
1913 | port *pt = port_rep_from_scratch (SCHEME_A); |
1922 | |
1914 | |
1923 | if (!pt && USE_ERROR_CHECKING) |
1915 | if (!pt && USE_ERROR_CHECKING) |
1924 | return NIL; |
1916 | return NIL; |
1925 | |
1917 | |
1926 | return mk_port (SCHEME_A_ pt); |
1918 | return mk_port (SCHEME_A_ pt); |
1927 | } |
1919 | } |
1928 | |
1920 | |
1929 | static void |
1921 | ecb_cold static void |
1930 | port_close (SCHEME_P_ pointer p, int flag) |
1922 | port_close (SCHEME_P_ pointer p, int flag) |
1931 | { |
1923 | { |
1932 | port *pt = p->object.port; |
1924 | port *pt = port (p); |
1933 | |
1925 | |
1934 | pt->kind &= ~flag; |
1926 | pt->kind &= ~flag; |
1935 | |
1927 | |
1936 | if ((pt->kind & (port_input | port_output)) == 0) |
1928 | if ((pt->kind & (port_input | port_output)) == 0) |
1937 | { |
1929 | { |
… | |
… | |
1958 | /* get new character from input file */ |
1950 | /* get new character from input file */ |
1959 | static int |
1951 | static int |
1960 | inchar (SCHEME_P) |
1952 | inchar (SCHEME_P) |
1961 | { |
1953 | { |
1962 | int c; |
1954 | int c; |
1963 | port *pt; |
1955 | port *pt = port (SCHEME_V->inport); |
1964 | |
|
|
1965 | pt = SCHEME_V->inport->object.port; |
|
|
1966 | |
1956 | |
1967 | if (pt->kind & port_saw_EOF) |
1957 | if (pt->kind & port_saw_EOF) |
1968 | return EOF; |
1958 | return EOF; |
1969 | |
1959 | |
1970 | c = basic_inchar (pt); |
1960 | c = basic_inchar (pt); |
… | |
… | |
2037 | port *pt; |
2027 | port *pt; |
2038 | |
2028 | |
2039 | if (c == EOF) |
2029 | if (c == EOF) |
2040 | return; |
2030 | return; |
2041 | |
2031 | |
2042 | pt = SCHEME_V->inport->object.port; |
2032 | pt = port (SCHEME_V->inport); |
2043 | pt->unget = c; |
2033 | pt->unget = c; |
2044 | #else |
2034 | #else |
2045 | if (c == EOF) |
2035 | if (c == EOF) |
2046 | return; |
2036 | return; |
2047 | |
2037 | |
2048 | ungot = c; |
2038 | ungot = c; |
2049 | #endif |
2039 | #endif |
2050 | } |
2040 | } |
2051 | |
2041 | |
2052 | #if USE_PORTS |
2042 | #if USE_PORTS |
2053 | static int |
2043 | ecb_cold static int |
2054 | realloc_port_string (SCHEME_P_ port *p) |
2044 | realloc_port_string (SCHEME_P_ port *p) |
2055 | { |
2045 | { |
2056 | char *start = p->rep.string.start; |
2046 | char *start = p->rep.string.start; |
2057 | size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; |
2047 | size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; |
2058 | char *str = malloc (new_size); |
2048 | char *str = malloc (new_size); |
… | |
… | |
2071 | else |
2061 | else |
2072 | return 0; |
2062 | return 0; |
2073 | } |
2063 | } |
2074 | #endif |
2064 | #endif |
2075 | |
2065 | |
2076 | INTERFACE void |
2066 | ecb_cold INTERFACE void |
2077 | putstr (SCHEME_P_ const char *s) |
2067 | putstr (SCHEME_P_ const char *s) |
2078 | { |
2068 | { |
2079 | #if USE_PORTS |
2069 | #if USE_PORTS |
2080 | port *pt = SCHEME_V->outport->object.port; |
2070 | port *pt = port (SCHEME_V->outport); |
2081 | |
2071 | |
2082 | if (pt->kind & port_file) |
2072 | if (pt->kind & port_file) |
2083 | write (pt->rep.stdio.file, s, strlen (s)); |
2073 | write (pt->rep.stdio.file, s, strlen (s)); |
2084 | else |
2074 | else |
2085 | for (; *s; s++) |
2075 | for (; *s; s++) |
… | |
… | |
2087 | *pt->rep.string.curr++ = *s; |
2077 | *pt->rep.string.curr++ = *s; |
2088 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
2078 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
2089 | *pt->rep.string.curr++ = *s; |
2079 | *pt->rep.string.curr++ = *s; |
2090 | |
2080 | |
2091 | #else |
2081 | #else |
2092 | xwrstr (s); |
2082 | write (pt->rep.stdio.file, s, strlen (s)); |
2093 | #endif |
2083 | #endif |
2094 | } |
2084 | } |
2095 | |
2085 | |
2096 | static void |
2086 | ecb_cold static void |
2097 | putchars (SCHEME_P_ const char *s, int len) |
2087 | putchars (SCHEME_P_ const char *s, int len) |
2098 | { |
2088 | { |
2099 | #if USE_PORTS |
2089 | #if USE_PORTS |
2100 | port *pt = SCHEME_V->outport->object.port; |
2090 | port *pt = port (SCHEME_V->outport); |
2101 | |
2091 | |
2102 | if (pt->kind & port_file) |
2092 | if (pt->kind & port_file) |
2103 | write (pt->rep.stdio.file, s, len); |
2093 | write (pt->rep.stdio.file, s, len); |
2104 | else |
2094 | else |
2105 | { |
2095 | { |
… | |
… | |
2115 | #else |
2105 | #else |
2116 | write (1, s, len); |
2106 | write (1, s, len); |
2117 | #endif |
2107 | #endif |
2118 | } |
2108 | } |
2119 | |
2109 | |
2120 | INTERFACE void |
2110 | ecb_cold INTERFACE void |
2121 | putcharacter (SCHEME_P_ int c) |
2111 | putcharacter (SCHEME_P_ int c) |
2122 | { |
2112 | { |
2123 | #if USE_PORTS |
2113 | #if USE_PORTS |
2124 | port *pt = SCHEME_V->outport->object.port; |
2114 | port *pt = port (SCHEME_V->outport); |
2125 | |
2115 | |
2126 | if (pt->kind & port_file) |
2116 | if (pt->kind & port_file) |
2127 | { |
2117 | { |
2128 | char cc = c; |
2118 | char cc = c; |
2129 | write (pt->rep.stdio.file, &cc, 1); |
2119 | write (pt->rep.stdio.file, &cc, 1); |
… | |
… | |
2141 | write (1, &c, 1); |
2131 | write (1, &c, 1); |
2142 | #endif |
2132 | #endif |
2143 | } |
2133 | } |
2144 | |
2134 | |
2145 | /* read characters up to delimiter, but cater to character constants */ |
2135 | /* read characters up to delimiter, but cater to character constants */ |
2146 | static char * |
2136 | ecb_cold static char * |
2147 | readstr_upto (SCHEME_P_ char *delim) |
2137 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2148 | { |
2138 | { |
2149 | char *p = SCHEME_V->strbuff; |
2139 | char *p = SCHEME_V->strbuff + skip; |
2150 | |
2140 | |
2151 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2141 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2152 | |
2142 | |
2153 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2143 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2154 | *p = 0; |
2144 | *p = 0; |
… | |
… | |
2160 | |
2150 | |
2161 | return SCHEME_V->strbuff; |
2151 | return SCHEME_V->strbuff; |
2162 | } |
2152 | } |
2163 | |
2153 | |
2164 | /* read string expression "xxx...xxx" */ |
2154 | /* read string expression "xxx...xxx" */ |
2165 | static pointer |
2155 | ecb_cold static pointer |
2166 | readstrexp (SCHEME_P) |
2156 | readstrexp (SCHEME_P_ char delim) |
2167 | { |
2157 | { |
2168 | char *p = SCHEME_V->strbuff; |
2158 | char *p = SCHEME_V->strbuff; |
2169 | int c; |
2159 | int c; |
2170 | int c1 = 0; |
2160 | int c1 = 0; |
2171 | enum |
|
|
2172 | { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2161 | enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2173 | |
2162 | |
2174 | for (;;) |
2163 | for (;;) |
2175 | { |
2164 | { |
2176 | c = inchar (SCHEME_A); |
2165 | c = inchar (SCHEME_A); |
2177 | |
2166 | |
… | |
… | |
2179 | return S_F; |
2168 | return S_F; |
2180 | |
2169 | |
2181 | switch (state) |
2170 | switch (state) |
2182 | { |
2171 | { |
2183 | case st_ok: |
2172 | case st_ok: |
2184 | switch (c) |
2173 | if (ecb_expect_false (c == delim)) |
2185 | { |
|
|
2186 | case '\\': |
|
|
2187 | state = st_bsl; |
|
|
2188 | break; |
|
|
2189 | |
|
|
2190 | case '"': |
|
|
2191 | *p = 0; |
|
|
2192 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2174 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2193 | |
2175 | |
2194 | default: |
2176 | if (ecb_expect_false (c == '\\')) |
|
|
2177 | state = st_bsl; |
|
|
2178 | else |
2195 | *p++ = c; |
2179 | *p++ = c; |
2196 | break; |
|
|
2197 | } |
|
|
2198 | |
2180 | |
2199 | break; |
2181 | break; |
2200 | |
2182 | |
2201 | case st_bsl: |
2183 | case st_bsl: |
2202 | switch (c) |
2184 | switch (c) |
… | |
… | |
2211 | case '7': |
2193 | case '7': |
2212 | state = st_oct1; |
2194 | state = st_oct1; |
2213 | c1 = c - '0'; |
2195 | c1 = c - '0'; |
2214 | break; |
2196 | break; |
2215 | |
2197 | |
|
|
2198 | case 'a': *p++ = '\a'; state = st_ok; break; |
|
|
2199 | case 'n': *p++ = '\n'; state = st_ok; break; |
|
|
2200 | case 'r': *p++ = '\r'; state = st_ok; break; |
|
|
2201 | case 't': *p++ = '\t'; state = st_ok; break; |
|
|
2202 | |
|
|
2203 | //TODO: \whitespace eol whitespace |
|
|
2204 | |
|
|
2205 | //TODO: x should end in ;, not two-digit hex |
2216 | case 'x': |
2206 | case 'x': |
2217 | case 'X': |
2207 | case 'X': |
2218 | state = st_x1; |
2208 | state = st_x1; |
2219 | c1 = 0; |
2209 | c1 = 0; |
2220 | break; |
2210 | break; |
2221 | |
2211 | |
2222 | case 'n': |
|
|
2223 | *p++ = '\n'; |
|
|
2224 | state = st_ok; |
|
|
2225 | break; |
|
|
2226 | |
|
|
2227 | case 't': |
|
|
2228 | *p++ = '\t'; |
|
|
2229 | state = st_ok; |
|
|
2230 | break; |
|
|
2231 | |
|
|
2232 | case 'r': |
|
|
2233 | *p++ = '\r'; |
|
|
2234 | state = st_ok; |
|
|
2235 | break; |
|
|
2236 | |
|
|
2237 | case '"': |
|
|
2238 | *p++ = '"'; |
|
|
2239 | state = st_ok; |
|
|
2240 | break; |
|
|
2241 | |
|
|
2242 | default: |
2212 | default: |
2243 | *p++ = c; |
2213 | *p++ = c; |
2244 | state = st_ok; |
2214 | state = st_ok; |
2245 | break; |
2215 | break; |
2246 | } |
2216 | } |
2247 | |
2217 | |
2248 | break; |
2218 | break; |
2249 | |
2219 | |
2250 | case st_x1: |
2220 | case st_x1: |
2251 | case st_x2: |
2221 | case st_x2: |
2252 | c = toupper (c); |
2222 | c = tolower (c); |
2253 | |
2223 | |
2254 | if (c >= '0' && c <= 'F') |
2224 | if (c >= '0' && c <= '9') |
2255 | { |
|
|
2256 | if (c <= '9') |
|
|
2257 | c1 = (c1 << 4) + c - '0'; |
2225 | c1 = (c1 << 4) + c - '0'; |
2258 | else |
2226 | else if (c >= 'a' && c <= 'f') |
2259 | c1 = (c1 << 4) + c - 'A' + 10; |
2227 | c1 = (c1 << 4) + c - 'a' + 10; |
2260 | |
|
|
2261 | if (state == st_x1) |
|
|
2262 | state = st_x2; |
|
|
2263 | else |
|
|
2264 | { |
|
|
2265 | *p++ = c1; |
|
|
2266 | state = st_ok; |
|
|
2267 | } |
|
|
2268 | } |
|
|
2269 | else |
2228 | else |
2270 | return S_F; |
2229 | return S_F; |
|
|
2230 | |
|
|
2231 | if (state == st_x1) |
|
|
2232 | state = st_x2; |
|
|
2233 | else |
|
|
2234 | { |
|
|
2235 | *p++ = c1; |
|
|
2236 | state = st_ok; |
|
|
2237 | } |
2271 | |
2238 | |
2272 | break; |
2239 | break; |
2273 | |
2240 | |
2274 | case st_oct1: |
2241 | case st_oct1: |
2275 | case st_oct2: |
2242 | case st_oct2: |
… | |
… | |
2279 | backchar (SCHEME_A_ c); |
2246 | backchar (SCHEME_A_ c); |
2280 | state = st_ok; |
2247 | state = st_ok; |
2281 | } |
2248 | } |
2282 | else |
2249 | else |
2283 | { |
2250 | { |
2284 | if (state == st_oct2 && c1 >= 32) |
2251 | if (state == st_oct2 && c1 >= ' ') |
2285 | return S_F; |
2252 | return S_F; |
2286 | |
2253 | |
2287 | c1 = (c1 << 3) + (c - '0'); |
2254 | c1 = (c1 << 3) + (c - '0'); |
2288 | |
2255 | |
2289 | if (state == st_oct1) |
2256 | if (state == st_oct1) |
… | |
… | |
2294 | state = st_ok; |
2261 | state = st_ok; |
2295 | } |
2262 | } |
2296 | } |
2263 | } |
2297 | |
2264 | |
2298 | break; |
2265 | break; |
2299 | |
|
|
2300 | } |
2266 | } |
2301 | } |
2267 | } |
2302 | } |
2268 | } |
2303 | |
2269 | |
2304 | /* check c is in chars */ |
2270 | /* check c is in chars */ |
2305 | ecb_inline int |
2271 | ecb_cold int |
2306 | is_one_of (char *s, int c) |
2272 | is_one_of (const char *s, int c) |
2307 | { |
2273 | { |
2308 | if (c == EOF) |
|
|
2309 | return 1; |
|
|
2310 | |
|
|
2311 | return !!strchr (s, c); |
2274 | return c == EOF || !!strchr (s, c); |
2312 | } |
2275 | } |
2313 | |
2276 | |
2314 | /* skip white characters */ |
2277 | /* skip white characters */ |
2315 | ecb_inline int |
2278 | ecb_cold int |
2316 | skipspace (SCHEME_P) |
2279 | skipspace (SCHEME_P) |
2317 | { |
2280 | { |
2318 | int c, curr_line = 0; |
2281 | int c, curr_line = 0; |
2319 | |
2282 | |
2320 | do |
2283 | do |
2321 | { |
2284 | { |
2322 | c = inchar (SCHEME_A); |
2285 | c = inchar (SCHEME_A); |
|
|
2286 | |
2323 | #if SHOW_ERROR_LINE |
2287 | #if SHOW_ERROR_LINE |
2324 | if (c == '\n') |
2288 | if (ecb_expect_false (c == '\n')) |
2325 | curr_line++; |
2289 | curr_line++; |
2326 | #endif |
2290 | #endif |
|
|
2291 | |
|
|
2292 | if (ecb_expect_false (c == EOF)) |
|
|
2293 | return c; |
2327 | } |
2294 | } |
2328 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2295 | while (is_one_of (WHITESPACE, c)); |
2329 | |
2296 | |
2330 | /* record it */ |
2297 | /* record it */ |
2331 | #if SHOW_ERROR_LINE |
2298 | #if SHOW_ERROR_LINE |
2332 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2299 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2333 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2300 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2334 | #endif |
2301 | #endif |
2335 | |
2302 | |
2336 | if (c != EOF) |
|
|
2337 | { |
|
|
2338 | backchar (SCHEME_A_ c); |
2303 | backchar (SCHEME_A_ c); |
2339 | return 1; |
2304 | return 1; |
2340 | } |
|
|
2341 | else |
|
|
2342 | return EOF; |
|
|
2343 | } |
2305 | } |
2344 | |
2306 | |
2345 | /* get token */ |
2307 | /* get token */ |
2346 | static int |
2308 | ecb_cold static int |
2347 | token (SCHEME_P) |
2309 | token (SCHEME_P) |
2348 | { |
2310 | { |
2349 | int c = skipspace (SCHEME_A); |
2311 | int c = skipspace (SCHEME_A); |
2350 | |
2312 | |
2351 | if (c == EOF) |
2313 | if (c == EOF) |
… | |
… | |
2363 | return TOK_RPAREN; |
2325 | return TOK_RPAREN; |
2364 | |
2326 | |
2365 | case '.': |
2327 | case '.': |
2366 | c = inchar (SCHEME_A); |
2328 | c = inchar (SCHEME_A); |
2367 | |
2329 | |
2368 | if (is_one_of (" \n\t", c)) |
2330 | if (is_one_of (WHITESPACE, c)) |
2369 | return TOK_DOT; |
2331 | return TOK_DOT; |
2370 | else |
2332 | else |
2371 | { |
2333 | { |
2372 | //TODO: ungetc twice in a row is not supported in C |
|
|
2373 | backchar (SCHEME_A_ c); |
2334 | backchar (SCHEME_A_ c); |
2374 | backchar (SCHEME_A_ '.'); |
|
|
2375 | return TOK_ATOM; |
2335 | return TOK_DOTATOM; |
2376 | } |
2336 | } |
|
|
2337 | |
|
|
2338 | case '|': |
|
|
2339 | return TOK_STRATOM; |
2377 | |
2340 | |
2378 | case '\'': |
2341 | case '\'': |
2379 | return TOK_QUOTE; |
2342 | return TOK_QUOTE; |
2380 | |
2343 | |
2381 | case ';': |
2344 | case ';': |
… | |
… | |
2448 | } |
2411 | } |
2449 | |
2412 | |
2450 | /* ========== Routines for Printing ========== */ |
2413 | /* ========== Routines for Printing ========== */ |
2451 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2414 | #define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) |
2452 | |
2415 | |
2453 | static void |
2416 | ecb_cold static void |
2454 | printslashstring (SCHEME_P_ char *p, int len) |
2417 | printslashstring (SCHEME_P_ char *p, int len) |
2455 | { |
2418 | { |
2456 | int i; |
2419 | int i; |
2457 | unsigned char *s = (unsigned char *) p; |
2420 | unsigned char *s = (unsigned char *) p; |
2458 | |
2421 | |
… | |
… | |
2513 | } |
2476 | } |
2514 | |
2477 | |
2515 | putcharacter (SCHEME_A_ '"'); |
2478 | putcharacter (SCHEME_A_ '"'); |
2516 | } |
2479 | } |
2517 | |
2480 | |
2518 | |
|
|
2519 | /* print atoms */ |
2481 | /* print atoms */ |
2520 | static void |
2482 | ecb_cold static void |
2521 | printatom (SCHEME_P_ pointer l, int f) |
2483 | printatom (SCHEME_P_ pointer l, int f) |
2522 | { |
2484 | { |
2523 | char *p; |
2485 | char *p; |
2524 | int len; |
2486 | int len; |
2525 | |
2487 | |
2526 | atom2str (SCHEME_A_ l, f, &p, &len); |
2488 | atom2str (SCHEME_A_ l, f, &p, &len); |
2527 | putchars (SCHEME_A_ p, len); |
2489 | putchars (SCHEME_A_ p, len); |
2528 | } |
2490 | } |
2529 | |
2491 | |
2530 | |
|
|
2531 | /* Uses internal buffer unless string pointer is already available */ |
2492 | /* Uses internal buffer unless string pointer is already available */ |
2532 | static void |
2493 | ecb_cold static void |
2533 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2494 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2534 | { |
2495 | { |
2535 | char *p; |
2496 | char *p; |
2536 | |
2497 | |
2537 | if (l == NIL) |
2498 | if (l == NIL) |
… | |
… | |
2695 | #endif |
2656 | #endif |
2696 | } |
2657 | } |
2697 | else if (is_continuation (l)) |
2658 | else if (is_continuation (l)) |
2698 | p = "#<CONTINUATION>"; |
2659 | p = "#<CONTINUATION>"; |
2699 | else |
2660 | else |
|
|
2661 | { |
|
|
2662 | #if USE_PRINTF |
|
|
2663 | p = SCHEME_V->strbuff; |
|
|
2664 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2665 | #else |
2700 | p = "#<ERROR>"; |
2666 | p = "#<ERROR>"; |
|
|
2667 | #endif |
|
|
2668 | } |
2701 | |
2669 | |
2702 | *pp = p; |
2670 | *pp = p; |
2703 | *plen = strlen (p); |
2671 | *plen = strlen (p); |
2704 | } |
2672 | } |
2705 | |
2673 | |
… | |
… | |
2737 | return car (d); |
2705 | return car (d); |
2738 | |
2706 | |
2739 | p = cons (car (d), cdr (d)); |
2707 | p = cons (car (d), cdr (d)); |
2740 | q = p; |
2708 | q = p; |
2741 | |
2709 | |
2742 | while (cdr (cdr (p)) != NIL) |
2710 | while (cddr (p) != NIL) |
2743 | { |
2711 | { |
2744 | d = cons (car (p), cdr (p)); |
2712 | d = cons (car (p), cdr (p)); |
2745 | |
2713 | |
2746 | if (cdr (cdr (p)) != NIL) |
2714 | if (cddr (p) != NIL) |
2747 | p = cdr (d); |
2715 | p = cdr (d); |
2748 | } |
2716 | } |
2749 | |
2717 | |
2750 | set_cdr (p, car (cdr (p))); |
2718 | set_cdr (p, cadr (p)); |
2751 | return q; |
2719 | return q; |
2752 | } |
2720 | } |
2753 | |
2721 | |
2754 | /* reverse list -- produce new list */ |
2722 | /* reverse list -- produce new list */ |
2755 | static pointer |
2723 | ecb_hot static pointer |
2756 | reverse (SCHEME_P_ pointer a) |
2724 | reverse (SCHEME_P_ pointer a) |
2757 | { |
2725 | { |
2758 | /* a must be checked by gc */ |
2726 | /* a must be checked by gc */ |
2759 | pointer p = NIL; |
2727 | pointer p = NIL; |
2760 | |
2728 | |
… | |
… | |
2763 | |
2731 | |
2764 | return p; |
2732 | return p; |
2765 | } |
2733 | } |
2766 | |
2734 | |
2767 | /* reverse list --- in-place */ |
2735 | /* reverse list --- in-place */ |
2768 | static pointer |
2736 | ecb_hot static pointer |
2769 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2737 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2770 | { |
2738 | { |
2771 | pointer result = term; |
2739 | pointer result = term; |
2772 | pointer p = list; |
2740 | pointer p = list; |
2773 | |
2741 | |
… | |
… | |
2781 | |
2749 | |
2782 | return result; |
2750 | return result; |
2783 | } |
2751 | } |
2784 | |
2752 | |
2785 | /* append list -- produce new list (in reverse order) */ |
2753 | /* append list -- produce new list (in reverse order) */ |
2786 | static pointer |
2754 | ecb_hot static pointer |
2787 | revappend (SCHEME_P_ pointer a, pointer b) |
2755 | revappend (SCHEME_P_ pointer a, pointer b) |
2788 | { |
2756 | { |
2789 | pointer result = a; |
2757 | pointer result = a; |
2790 | pointer p = b; |
2758 | pointer p = b; |
2791 | |
2759 | |
… | |
… | |
2800 | |
2768 | |
2801 | return S_F; /* signal an error */ |
2769 | return S_F; /* signal an error */ |
2802 | } |
2770 | } |
2803 | |
2771 | |
2804 | /* equivalence of atoms */ |
2772 | /* equivalence of atoms */ |
2805 | int |
2773 | ecb_hot int |
2806 | eqv (pointer a, pointer b) |
2774 | eqv (pointer a, pointer b) |
2807 | { |
2775 | { |
2808 | if (is_string (a)) |
2776 | if (is_string (a)) |
2809 | { |
2777 | { |
2810 | if (is_string (b)) |
2778 | if (is_string (b)) |
… | |
… | |
2849 | /* () is #t in R5RS */ |
2817 | /* () is #t in R5RS */ |
2850 | #define is_true(p) ((p) != S_F) |
2818 | #define is_true(p) ((p) != S_F) |
2851 | #define is_false(p) ((p) == S_F) |
2819 | #define is_false(p) ((p) == S_F) |
2852 | |
2820 | |
2853 | /* ========== Environment implementation ========== */ |
2821 | /* ========== Environment implementation ========== */ |
2854 | |
|
|
2855 | #if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) |
|
|
2856 | |
|
|
2857 | static int |
|
|
2858 | hash_fn (const char *key, int table_size) |
|
|
2859 | { |
|
|
2860 | const unsigned char *p = key; |
|
|
2861 | uint32_t hash = 2166136261; |
|
|
2862 | |
|
|
2863 | while (*p) |
|
|
2864 | hash = (hash ^ *p++) * 16777619; |
|
|
2865 | |
|
|
2866 | return hash % table_size; |
|
|
2867 | } |
|
|
2868 | #endif |
|
|
2869 | |
2822 | |
2870 | #ifndef USE_ALIST_ENV |
2823 | #ifndef USE_ALIST_ENV |
2871 | |
2824 | |
2872 | /* |
2825 | /* |
2873 | * In this implementation, each frame of the environment may be |
2826 | * In this implementation, each frame of the environment may be |
… | |
… | |
2890 | |
2843 | |
2891 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2844 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2892 | setenvironment (SCHEME_V->envir); |
2845 | setenvironment (SCHEME_V->envir); |
2893 | } |
2846 | } |
2894 | |
2847 | |
|
|
2848 | static uint32_t |
|
|
2849 | sym_hash (pointer sym, uint32_t size) |
|
|
2850 | { |
|
|
2851 | uintptr_t ptr = (uintptr_t)sym; |
|
|
2852 | |
|
|
2853 | #if 0 |
|
|
2854 | /* table size is prime, so why mix */ |
|
|
2855 | ptr += ptr >> 32; |
|
|
2856 | ptr += ptr >> 16; |
|
|
2857 | ptr += ptr >> 8; |
|
|
2858 | #endif |
|
|
2859 | |
|
|
2860 | return ptr % size; |
|
|
2861 | } |
|
|
2862 | |
2895 | ecb_inline void |
2863 | ecb_inline void |
2896 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2864 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2897 | { |
2865 | { |
2898 | pointer slot = immutable_cons (variable, value); |
2866 | pointer slot = immutable_cons (variable, value); |
2899 | |
2867 | |
2900 | if (is_vector (car (env))) |
2868 | if (is_vector (car (env))) |
2901 | { |
2869 | { |
2902 | int location = hash_fn (symname (variable), veclength (car (env))); |
2870 | int location = sym_hash (variable, veclength (car (env))); |
2903 | |
|
|
2904 | set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); |
2871 | vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); |
2905 | } |
2872 | } |
2906 | else |
2873 | else |
2907 | set_car (env, immutable_cons (slot, car (env))); |
2874 | set_car (env, immutable_cons (slot, car (env))); |
2908 | } |
2875 | } |
2909 | |
2876 | |
2910 | static pointer |
2877 | ecb_hot static pointer |
2911 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2878 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2912 | { |
2879 | { |
2913 | pointer x, y; |
2880 | pointer x, y; |
2914 | int location; |
|
|
2915 | |
2881 | |
2916 | for (x = env; x != NIL; x = cdr (x)) |
2882 | for (x = env; x != NIL; x = cdr (x)) |
2917 | { |
2883 | { |
2918 | if (is_vector (car (x))) |
2884 | if (is_vector (car (x))) |
2919 | { |
2885 | { |
2920 | location = hash_fn (symname (hdl), veclength (car (x))); |
2886 | int location = sym_hash (hdl, veclength (car (x))); |
2921 | y = vector_elem (car (x), location); |
2887 | y = vector_get (car (x), location); |
2922 | } |
2888 | } |
2923 | else |
2889 | else |
2924 | y = car (x); |
2890 | y = car (x); |
2925 | |
2891 | |
2926 | for (; y != NIL; y = cdr (y)) |
2892 | for (; y != NIL; y = cdr (y)) |
2927 | if (caar (y) == hdl) |
2893 | if (caar (y) == hdl) |
2928 | break; |
2894 | break; |
2929 | |
2895 | |
2930 | if (y != NIL) |
2896 | if (y != NIL) |
|
|
2897 | return car (y); |
|
|
2898 | |
|
|
2899 | if (!all) |
2931 | break; |
2900 | break; |
2932 | |
|
|
2933 | if (!all) |
|
|
2934 | return NIL; |
|
|
2935 | } |
2901 | } |
2936 | |
|
|
2937 | if (x != NIL) |
|
|
2938 | return car (y); |
|
|
2939 | |
2902 | |
2940 | return NIL; |
2903 | return NIL; |
2941 | } |
2904 | } |
2942 | |
2905 | |
2943 | #else /* USE_ALIST_ENV */ |
2906 | #else /* USE_ALIST_ENV */ |
2944 | |
2907 | |
2945 | ecb_inline void |
2908 | static void |
2946 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2909 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2947 | { |
2910 | { |
2948 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2911 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2949 | setenvironment (SCHEME_V->envir); |
2912 | setenvironment (SCHEME_V->envir); |
2950 | } |
2913 | } |
2951 | |
2914 | |
2952 | ecb_inline void |
2915 | static void |
2953 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2916 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2954 | { |
2917 | { |
2955 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2918 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2956 | } |
2919 | } |
2957 | |
2920 | |
2958 | static pointer |
2921 | ecb_hot static pointer |
2959 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2922 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2960 | { |
2923 | { |
2961 | pointer x, y; |
2924 | pointer x, y; |
2962 | |
2925 | |
2963 | for (x = env; x != NIL; x = cdr (x)) |
2926 | for (x = env; x != NIL; x = cdr (x)) |
… | |
… | |
2965 | for (y = car (x); y != NIL; y = cdr (y)) |
2928 | for (y = car (x); y != NIL; y = cdr (y)) |
2966 | if (caar (y) == hdl) |
2929 | if (caar (y) == hdl) |
2967 | break; |
2930 | break; |
2968 | |
2931 | |
2969 | if (y != NIL) |
2932 | if (y != NIL) |
|
|
2933 | return car (y); |
2970 | break; |
2934 | break; |
2971 | |
2935 | |
2972 | if (!all) |
2936 | if (!all) |
2973 | return NIL; |
2937 | break; |
2974 | } |
2938 | } |
2975 | |
|
|
2976 | if (x != NIL) |
|
|
2977 | return car (y); |
|
|
2978 | |
2939 | |
2979 | return NIL; |
2940 | return NIL; |
2980 | } |
2941 | } |
2981 | |
2942 | |
2982 | #endif /* USE_ALIST_ENV else */ |
2943 | #endif /* USE_ALIST_ENV else */ |
2983 | |
2944 | |
2984 | ecb_inline void |
2945 | static void |
2985 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2946 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2986 | { |
2947 | { |
|
|
2948 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2987 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2949 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2988 | } |
2950 | } |
2989 | |
2951 | |
2990 | ecb_inline void |
2952 | static void |
2991 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2953 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2992 | { |
2954 | { |
2993 | set_cdr (slot, value); |
2955 | set_cdr (slot, value); |
2994 | } |
2956 | } |
2995 | |
2957 | |
2996 | ecb_inline pointer |
2958 | static pointer |
2997 | slot_value_in_env (pointer slot) |
2959 | slot_value_in_env (pointer slot) |
2998 | { |
2960 | { |
2999 | return cdr (slot); |
2961 | return cdr (slot); |
3000 | } |
2962 | } |
3001 | |
2963 | |
3002 | /* ========== Evaluation Cycle ========== */ |
2964 | /* ========== Evaluation Cycle ========== */ |
3003 | |
2965 | |
3004 | static int |
2966 | ecb_cold static int |
3005 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2967 | xError_1 (SCHEME_P_ const char *s, pointer a) |
3006 | { |
2968 | { |
3007 | #if USE_ERROR_HOOK |
2969 | #if USE_ERROR_HOOK |
3008 | pointer x; |
2970 | pointer x; |
3009 | pointer hdl = SCHEME_V->ERROR_HOOK; |
2971 | pointer hdl = SCHEME_V->ERROR_HOOK; |
… | |
… | |
3085 | pointer code; |
3047 | pointer code; |
3086 | }; |
3048 | }; |
3087 | |
3049 | |
3088 | # define STACK_GROWTH 3 |
3050 | # define STACK_GROWTH 3 |
3089 | |
3051 | |
3090 | static void |
3052 | ecb_hot static void |
3091 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3053 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3092 | { |
3054 | { |
3093 | int nframes = (uintptr_t)SCHEME_V->dump; |
3055 | int nframes = (uintptr_t)SCHEME_V->dump; |
3094 | struct dump_stack_frame *next_frame; |
3056 | struct dump_stack_frame *next_frame; |
3095 | |
3057 | |
3096 | /* enough room for the next frame? */ |
3058 | /* enough room for the next frame? */ |
3097 | if (nframes >= SCHEME_V->dump_size) |
3059 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3098 | { |
3060 | { |
3099 | SCHEME_V->dump_size += STACK_GROWTH; |
3061 | SCHEME_V->dump_size += STACK_GROWTH; |
3100 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3062 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3101 | } |
3063 | } |
3102 | |
3064 | |
… | |
… | |
3108 | next_frame->code = code; |
3070 | next_frame->code = code; |
3109 | |
3071 | |
3110 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3072 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3111 | } |
3073 | } |
3112 | |
3074 | |
3113 | static int |
3075 | static ecb_hot int |
3114 | xs_return (SCHEME_P_ pointer a) |
3076 | xs_return (SCHEME_P_ pointer a) |
3115 | { |
3077 | { |
3116 | int nframes = (uintptr_t)SCHEME_V->dump; |
3078 | int nframes = (uintptr_t)SCHEME_V->dump; |
3117 | struct dump_stack_frame *frame; |
3079 | struct dump_stack_frame *frame; |
3118 | |
3080 | |
… | |
… | |
3129 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3091 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3130 | |
3092 | |
3131 | return 0; |
3093 | return 0; |
3132 | } |
3094 | } |
3133 | |
3095 | |
3134 | ecb_inline void |
3096 | ecb_cold void |
3135 | dump_stack_reset (SCHEME_P) |
3097 | dump_stack_reset (SCHEME_P) |
3136 | { |
3098 | { |
3137 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3099 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3138 | SCHEME_V->dump = (pointer)+0; |
3100 | SCHEME_V->dump = (pointer)+0; |
3139 | } |
3101 | } |
3140 | |
3102 | |
3141 | ecb_inline void |
3103 | ecb_cold void |
3142 | dump_stack_initialize (SCHEME_P) |
3104 | dump_stack_initialize (SCHEME_P) |
3143 | { |
3105 | { |
3144 | SCHEME_V->dump_size = 0; |
3106 | SCHEME_V->dump_size = 0; |
3145 | SCHEME_V->dump_base = 0; |
3107 | SCHEME_V->dump_base = 0; |
3146 | dump_stack_reset (SCHEME_A); |
3108 | dump_stack_reset (SCHEME_A); |
3147 | } |
3109 | } |
3148 | |
3110 | |
3149 | static void |
3111 | ecb_cold static void |
3150 | dump_stack_free (SCHEME_P) |
3112 | dump_stack_free (SCHEME_P) |
3151 | { |
3113 | { |
3152 | free (SCHEME_V->dump_base); |
3114 | free (SCHEME_V->dump_base); |
3153 | SCHEME_V->dump_base = 0; |
3115 | SCHEME_V->dump_base = 0; |
3154 | SCHEME_V->dump = (pointer)0; |
3116 | SCHEME_V->dump = (pointer)0; |
3155 | SCHEME_V->dump_size = 0; |
3117 | SCHEME_V->dump_size = 0; |
3156 | } |
3118 | } |
3157 | |
3119 | |
3158 | static void |
3120 | ecb_cold static void |
3159 | dump_stack_mark (SCHEME_P) |
3121 | dump_stack_mark (SCHEME_P) |
3160 | { |
3122 | { |
3161 | int nframes = (uintptr_t)SCHEME_V->dump; |
3123 | int nframes = (uintptr_t)SCHEME_V->dump; |
3162 | int i; |
3124 | int i; |
3163 | |
3125 | |
… | |
… | |
3169 | mark (frame->envir); |
3131 | mark (frame->envir); |
3170 | mark (frame->code); |
3132 | mark (frame->code); |
3171 | } |
3133 | } |
3172 | } |
3134 | } |
3173 | |
3135 | |
3174 | static pointer |
3136 | ecb_cold static pointer |
3175 | ss_get_cont (SCHEME_P) |
3137 | ss_get_cont (SCHEME_P) |
3176 | { |
3138 | { |
3177 | int nframes = (uintptr_t)SCHEME_V->dump; |
3139 | int nframes = (uintptr_t)SCHEME_V->dump; |
3178 | int i; |
3140 | int i; |
3179 | |
3141 | |
… | |
… | |
3191 | } |
3153 | } |
3192 | |
3154 | |
3193 | return cont; |
3155 | return cont; |
3194 | } |
3156 | } |
3195 | |
3157 | |
3196 | static void |
3158 | ecb_cold static void |
3197 | ss_set_cont (SCHEME_P_ pointer cont) |
3159 | ss_set_cont (SCHEME_P_ pointer cont) |
3198 | { |
3160 | { |
3199 | int i = 0; |
3161 | int i = 0; |
3200 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3162 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3201 | |
3163 | |
… | |
… | |
3213 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3175 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3214 | } |
3176 | } |
3215 | |
3177 | |
3216 | #else |
3178 | #else |
3217 | |
3179 | |
3218 | ecb_inline void |
3180 | ecb_cold void |
3219 | dump_stack_reset (SCHEME_P) |
3181 | dump_stack_reset (SCHEME_P) |
3220 | { |
3182 | { |
3221 | SCHEME_V->dump = NIL; |
3183 | SCHEME_V->dump = NIL; |
3222 | } |
3184 | } |
3223 | |
3185 | |
3224 | ecb_inline void |
3186 | ecb_cold void |
3225 | dump_stack_initialize (SCHEME_P) |
3187 | dump_stack_initialize (SCHEME_P) |
3226 | { |
3188 | { |
3227 | dump_stack_reset (SCHEME_A); |
3189 | dump_stack_reset (SCHEME_A); |
3228 | } |
3190 | } |
3229 | |
3191 | |
3230 | static void |
3192 | ecb_cold static void |
3231 | dump_stack_free (SCHEME_P) |
3193 | dump_stack_free (SCHEME_P) |
3232 | { |
3194 | { |
3233 | SCHEME_V->dump = NIL; |
3195 | SCHEME_V->dump = NIL; |
3234 | } |
3196 | } |
3235 | |
3197 | |
3236 | static int |
3198 | ecb_hot static int |
3237 | xs_return (SCHEME_P_ pointer a) |
3199 | xs_return (SCHEME_P_ pointer a) |
3238 | { |
3200 | { |
3239 | pointer dump = SCHEME_V->dump; |
3201 | pointer dump = SCHEME_V->dump; |
3240 | |
3202 | |
3241 | SCHEME_V->value = a; |
3203 | SCHEME_V->value = a; |
… | |
… | |
3251 | SCHEME_V->dump = dump; |
3213 | SCHEME_V->dump = dump; |
3252 | |
3214 | |
3253 | return 0; |
3215 | return 0; |
3254 | } |
3216 | } |
3255 | |
3217 | |
3256 | static void |
3218 | ecb_hot static void |
3257 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3219 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3258 | { |
3220 | { |
3259 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3221 | SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), |
3260 | cons (args, |
3222 | cons (args, |
3261 | cons (SCHEME_V->envir, |
3223 | cons (SCHEME_V->envir, |
3262 | cons (code, |
3224 | cons (code, |
3263 | SCHEME_V->dump)))); |
3225 | SCHEME_V->dump)))); |
3264 | } |
3226 | } |
3265 | |
3227 | |
3266 | static void |
3228 | ecb_cold static void |
3267 | dump_stack_mark (SCHEME_P) |
3229 | dump_stack_mark (SCHEME_P) |
3268 | { |
3230 | { |
3269 | mark (SCHEME_V->dump); |
3231 | mark (SCHEME_V->dump); |
3270 | } |
3232 | } |
3271 | |
3233 | |
3272 | static pointer |
3234 | ecb_cold static pointer |
3273 | ss_get_cont (SCHEME_P) |
3235 | ss_get_cont (SCHEME_P) |
3274 | { |
3236 | { |
3275 | return SCHEME_V->dump; |
3237 | return SCHEME_V->dump; |
3276 | } |
3238 | } |
3277 | |
3239 | |
3278 | static void |
3240 | ecb_cold static void |
3279 | ss_set_cont (SCHEME_P_ pointer cont) |
3241 | ss_set_cont (SCHEME_P_ pointer cont) |
3280 | { |
3242 | { |
3281 | SCHEME_V->dump = cont; |
3243 | SCHEME_V->dump = cont; |
3282 | } |
3244 | } |
3283 | |
3245 | |
3284 | #endif |
3246 | #endif |
3285 | |
3247 | |
3286 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3248 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3287 | |
3249 | |
|
|
3250 | #if EXPERIMENT |
3288 | static int |
3251 | static int |
|
|
3252 | debug (SCHEME_P_ int indent, pointer x) |
|
|
3253 | { |
|
|
3254 | int c; |
|
|
3255 | |
|
|
3256 | if (is_syntax (x)) |
|
|
3257 | { |
|
|
3258 | printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x)); |
|
|
3259 | return 8 + 8; |
|
|
3260 | } |
|
|
3261 | |
|
|
3262 | if (x == NIL) |
|
|
3263 | { |
|
|
3264 | printf ("%*sNIL\n", indent, ""); |
|
|
3265 | return 3; |
|
|
3266 | } |
|
|
3267 | |
|
|
3268 | switch (type (x)) |
|
|
3269 | { |
|
|
3270 | case T_INTEGER: |
|
|
3271 | printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x); |
|
|
3272 | return 32+8; |
|
|
3273 | |
|
|
3274 | case T_SYMBOL: |
|
|
3275 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
|
|
3276 | return 24+8; |
|
|
3277 | |
|
|
3278 | case T_CLOSURE: |
|
|
3279 | printf ("%*sS<%s>\n", indent, "", "closure"); |
|
|
3280 | debug (SCHEME_A_ indent + 3, cdr(x)); |
|
|
3281 | return 32 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3282 | |
|
|
3283 | case T_PAIR: |
|
|
3284 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
|
|
3285 | c = debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3286 | c += debug (SCHEME_A_ indent + 3, cdr (x)); |
|
|
3287 | return c + 1; |
|
|
3288 | |
|
|
3289 | case T_PORT: |
|
|
3290 | printf ("%*sS<%s>\n", indent, "", "port"); |
|
|
3291 | return 24+8; |
|
|
3292 | |
|
|
3293 | case T_VECTOR: |
|
|
3294 | printf ("%*sS<%s>\n", indent, "", "vector"); |
|
|
3295 | return 24+8; |
|
|
3296 | |
|
|
3297 | case T_ENVIRONMENT: |
|
|
3298 | printf ("%*sS<%s>\n", indent, "", "environment"); |
|
|
3299 | return 0 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3300 | |
|
|
3301 | default: |
|
|
3302 | printf ("unhandled type %d\n", type (x)); |
|
|
3303 | break; |
|
|
3304 | } |
|
|
3305 | } |
|
|
3306 | #endif |
|
|
3307 | |
|
|
3308 | /* syntax, eval, core, ... */ |
|
|
3309 | ecb_hot static int |
3289 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3310 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3290 | { |
3311 | { |
3291 | pointer args = SCHEME_V->args; |
3312 | pointer args = SCHEME_V->args; |
3292 | pointer x, y; |
3313 | pointer x, y; |
3293 | |
3314 | |
3294 | switch (op) |
3315 | switch (op) |
3295 | { |
3316 | { |
|
|
3317 | #if EXPERIMENT //D |
|
|
3318 | case OP_DEBUG: |
|
|
3319 | printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); |
|
|
3320 | printf ("\n"); |
|
|
3321 | s_return (S_T); |
|
|
3322 | #endif |
3296 | case OP_LOAD: /* load */ |
3323 | case OP_LOAD: /* load */ |
3297 | if (file_interactive (SCHEME_A)) |
3324 | if (file_interactive (SCHEME_A)) |
3298 | { |
3325 | { |
3299 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3326 | putstr (SCHEME_A_ "Loading "); putstr (SCHEME_A_ strvalue (car (args))); putstr (SCHEME_A_ "\n"); |
3300 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3327 | //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3301 | } |
3328 | } |
3302 | |
3329 | |
3303 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3330 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3304 | Error_1 ("unable to open", car (args)); |
3331 | Error_1 ("unable to open", car (args)); |
3305 | else |
3332 | else |
… | |
… | |
3309 | } |
3336 | } |
3310 | |
3337 | |
3311 | case OP_T0LVL: /* top level */ |
3338 | case OP_T0LVL: /* top level */ |
3312 | |
3339 | |
3313 | /* If we reached the end of file, this loop is done. */ |
3340 | /* If we reached the end of file, this loop is done. */ |
3314 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3341 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3315 | { |
3342 | { |
3316 | if (SCHEME_V->file_i == 0) |
3343 | if (SCHEME_V->file_i == 0) |
3317 | { |
3344 | { |
3318 | SCHEME_V->args = NIL; |
3345 | SCHEME_V->args = NIL; |
3319 | s_goto (OP_QUIT); |
3346 | s_goto (OP_QUIT); |
… | |
… | |
3397 | #endif |
3424 | #endif |
3398 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3425 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3399 | { |
3426 | { |
3400 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3427 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3401 | |
3428 | |
3402 | if (x != NIL) |
3429 | if (x == NIL) |
3403 | s_return (slot_value_in_env (x)); |
|
|
3404 | else |
|
|
3405 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3430 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
|
|
3431 | |
|
|
3432 | s_return (slot_value_in_env (x)); |
3406 | } |
3433 | } |
3407 | else if (is_pair (SCHEME_V->code)) |
3434 | else if (is_pair (SCHEME_V->code)) |
3408 | { |
3435 | { |
3409 | x = car (SCHEME_V->code); |
3436 | x = car (SCHEME_V->code); |
3410 | |
3437 | |
… | |
… | |
3423 | } |
3450 | } |
3424 | else |
3451 | else |
3425 | s_return (SCHEME_V->code); |
3452 | s_return (SCHEME_V->code); |
3426 | |
3453 | |
3427 | case OP_E0ARGS: /* eval arguments */ |
3454 | case OP_E0ARGS: /* eval arguments */ |
3428 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3455 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3429 | { |
3456 | { |
3430 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3457 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3431 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3458 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3432 | SCHEME_V->code = SCHEME_V->value; |
3459 | SCHEME_V->code = SCHEME_V->value; |
3433 | s_goto (OP_APPLY); |
3460 | s_goto (OP_APPLY); |
… | |
… | |
3487 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3514 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3488 | else if (is_foreign (SCHEME_V->code)) |
3515 | else if (is_foreign (SCHEME_V->code)) |
3489 | { |
3516 | { |
3490 | /* Keep nested calls from GC'ing the arglist */ |
3517 | /* Keep nested calls from GC'ing the arglist */ |
3491 | push_recent_alloc (SCHEME_A_ args, NIL); |
3518 | push_recent_alloc (SCHEME_A_ args, NIL); |
3492 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3519 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3493 | |
3520 | |
3494 | s_return (x); |
3521 | s_return (x); |
3495 | } |
3522 | } |
3496 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3523 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3497 | { |
3524 | { |
… | |
… | |
3534 | |
3561 | |
3535 | case OP_DOMACRO: /* do macro */ |
3562 | case OP_DOMACRO: /* do macro */ |
3536 | SCHEME_V->code = SCHEME_V->value; |
3563 | SCHEME_V->code = SCHEME_V->value; |
3537 | s_goto (OP_EVAL); |
3564 | s_goto (OP_EVAL); |
3538 | |
3565 | |
3539 | #if 1 |
|
|
3540 | |
|
|
3541 | case OP_LAMBDA: /* lambda */ |
3566 | case OP_LAMBDA: /* lambda */ |
3542 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3567 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3543 | set SCHEME_V->value fall thru */ |
3568 | set SCHEME_V->value fall thru */ |
3544 | { |
3569 | { |
3545 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3570 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
… | |
… | |
3551 | SCHEME_V->code = slot_value_in_env (f); |
3576 | SCHEME_V->code = slot_value_in_env (f); |
3552 | s_goto (OP_APPLY); |
3577 | s_goto (OP_APPLY); |
3553 | } |
3578 | } |
3554 | |
3579 | |
3555 | SCHEME_V->value = SCHEME_V->code; |
3580 | SCHEME_V->value = SCHEME_V->code; |
3556 | /* Fallthru */ |
|
|
3557 | } |
3581 | } |
|
|
3582 | /* Fallthru */ |
3558 | |
3583 | |
3559 | case OP_LAMBDA1: |
3584 | case OP_LAMBDA1: |
3560 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3585 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3561 | |
|
|
3562 | #else |
|
|
3563 | |
|
|
3564 | case OP_LAMBDA: /* lambda */ |
|
|
3565 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
|
|
3566 | |
|
|
3567 | #endif |
|
|
3568 | |
3586 | |
3569 | case OP_MKCLOSURE: /* make-closure */ |
3587 | case OP_MKCLOSURE: /* make-closure */ |
3570 | x = car (args); |
3588 | x = car (args); |
3571 | |
3589 | |
3572 | if (car (x) == SCHEME_V->LAMBDA) |
3590 | if (car (x) == SCHEME_V->LAMBDA) |
… | |
… | |
3982 | } |
4000 | } |
3983 | |
4001 | |
3984 | if (USE_ERROR_CHECKING) abort (); |
4002 | if (USE_ERROR_CHECKING) abort (); |
3985 | } |
4003 | } |
3986 | |
4004 | |
3987 | static int |
4005 | /* math, cxr */ |
|
|
4006 | ecb_hot static int |
3988 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4007 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
3989 | { |
4008 | { |
3990 | pointer args = SCHEME_V->args; |
4009 | pointer args = SCHEME_V->args; |
3991 | pointer x = car (args); |
4010 | pointer x = car (args); |
3992 | num v; |
4011 | num v; |
3993 | |
4012 | |
3994 | switch (op) |
4013 | switch (op) |
3995 | { |
4014 | { |
3996 | #if USE_MATH |
4015 | #if USE_MATH |
3997 | case OP_INEX2EX: /* inexact->exact */ |
4016 | case OP_INEX2EX: /* inexact->exact */ |
3998 | { |
|
|
3999 | if (is_integer (x)) |
4017 | if (!is_integer (x)) |
4000 | s_return (x); |
4018 | { |
4001 | |
|
|
4002 | RVALUE r = rvalue_unchecked (x); |
4019 | RVALUE r = rvalue_unchecked (x); |
4003 | |
4020 | |
4004 | if (r == (RVALUE)(IVALUE)r) |
4021 | if (r == (RVALUE)(IVALUE)r) |
4005 | s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); |
4022 | x = mk_integer (SCHEME_A_ rvalue_unchecked (x)); |
4006 | else |
4023 | else |
4007 | Error_1 ("inexact->exact: not integral:", x); |
4024 | Error_1 ("inexact->exact: not integral:", x); |
4008 | } |
4025 | } |
4009 | |
4026 | |
|
|
4027 | s_return (x); |
|
|
4028 | |
|
|
4029 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4030 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4031 | case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x)))); |
|
|
4032 | case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x)))); |
|
|
4033 | |
|
|
4034 | case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
4010 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4035 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4011 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); |
4036 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)) |
|
|
4037 | / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args)))))); |
4012 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4038 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4013 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
4039 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
4014 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
4040 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
4015 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
4041 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
4016 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4042 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4017 | |
4043 | |
4018 | case OP_ATAN: |
4044 | case OP_ATAN: |
|
|
4045 | s_return (mk_real (SCHEME_A_ |
4019 | if (cdr (args) == NIL) |
4046 | cdr (args) == NIL |
4020 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4047 | ? atan (rvalue (x)) |
4021 | else |
4048 | : atan2 (rvalue (x), rvalue (cadr (args))))); |
4022 | { |
|
|
4023 | pointer y = cadr (args); |
|
|
4024 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
|
|
4025 | } |
|
|
4026 | |
|
|
4027 | case OP_SQRT: |
|
|
4028 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
|
|
4029 | |
4049 | |
4030 | case OP_EXPT: |
4050 | case OP_EXPT: |
4031 | { |
4051 | { |
4032 | RVALUE result; |
4052 | RVALUE result; |
4033 | int real_result = 1; |
4053 | int real_result = 1; |
… | |
… | |
4056 | if (real_result) |
4076 | if (real_result) |
4057 | s_return (mk_real (SCHEME_A_ result)); |
4077 | s_return (mk_real (SCHEME_A_ result)); |
4058 | else |
4078 | else |
4059 | s_return (mk_integer (SCHEME_A_ result)); |
4079 | s_return (mk_integer (SCHEME_A_ result)); |
4060 | } |
4080 | } |
4061 | |
|
|
4062 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4063 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4064 | |
|
|
4065 | case OP_TRUNCATE: |
|
|
4066 | { |
|
|
4067 | RVALUE n = rvalue (x); |
|
|
4068 | s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n))); |
|
|
4069 | } |
|
|
4070 | |
|
|
4071 | case OP_ROUND: |
|
|
4072 | if (is_integer (x)) |
|
|
4073 | s_return (x); |
|
|
4074 | |
|
|
4075 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
|
|
4076 | #endif |
4081 | #endif |
4077 | |
4082 | |
4078 | case OP_ADD: /* + */ |
4083 | case OP_ADD: /* + */ |
4079 | v = num_zero; |
4084 | v = num_zero; |
4080 | |
4085 | |
… | |
… | |
4168 | else |
4173 | else |
4169 | Error_0 ("modulo: division by zero"); |
4174 | Error_0 ("modulo: division by zero"); |
4170 | |
4175 | |
4171 | s_return (mk_number (SCHEME_A_ v)); |
4176 | s_return (mk_number (SCHEME_A_ v)); |
4172 | |
4177 | |
4173 | case OP_CAR: /* car */ |
4178 | /* the compiler will optimize this mess... */ |
4174 | s_return (caar (args)); |
4179 | case OP_CAR: op_car: s_return (car (x)); |
4175 | |
4180 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4176 | case OP_CDR: /* cdr */ |
4181 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4177 | s_return (cdar (args)); |
4182 | case OP_CADR: op_cadr: x = cdr (x); goto op_car; |
|
|
4183 | case OP_CDAR: op_cdar: x = car (x); goto op_cdr; |
|
|
4184 | case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr; |
|
|
4185 | case OP_CAAAR: op_caaar: x = car (x); goto op_caar; |
|
|
4186 | case OP_CAADR: op_caadr: x = cdr (x); goto op_caar; |
|
|
4187 | case OP_CADAR: op_cadar: x = car (x); goto op_cadr; |
|
|
4188 | case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr; |
|
|
4189 | case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar; |
|
|
4190 | case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar; |
|
|
4191 | case OP_CDDAR: op_cddar: x = car (x); goto op_cddr; |
|
|
4192 | case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr; |
|
|
4193 | case OP_CAAAAR: x = car (x); goto op_caaar; |
|
|
4194 | case OP_CAAADR: x = cdr (x); goto op_caaar; |
|
|
4195 | case OP_CAADAR: x = car (x); goto op_caadr; |
|
|
4196 | case OP_CAADDR: x = cdr (x); goto op_caadr; |
|
|
4197 | case OP_CADAAR: x = car (x); goto op_cadar; |
|
|
4198 | case OP_CADADR: x = cdr (x); goto op_cadar; |
|
|
4199 | case OP_CADDAR: x = car (x); goto op_caddr; |
|
|
4200 | case OP_CADDDR: x = cdr (x); goto op_caddr; |
|
|
4201 | case OP_CDAAAR: x = car (x); goto op_cdaar; |
|
|
4202 | case OP_CDAADR: x = cdr (x); goto op_cdaar; |
|
|
4203 | case OP_CDADAR: x = car (x); goto op_cdadr; |
|
|
4204 | case OP_CDADDR: x = cdr (x); goto op_cdadr; |
|
|
4205 | case OP_CDDAAR: x = car (x); goto op_cddar; |
|
|
4206 | case OP_CDDADR: x = cdr (x); goto op_cddar; |
|
|
4207 | case OP_CDDDAR: x = car (x); goto op_cdddr; |
|
|
4208 | case OP_CDDDDR: x = cdr (x); goto op_cdddr; |
4178 | |
4209 | |
4179 | case OP_CONS: /* cons */ |
4210 | case OP_CONS: /* cons */ |
4180 | set_cdr (args, cadr (args)); |
4211 | set_cdr (args, cadr (args)); |
4181 | s_return (args); |
4212 | s_return (args); |
4182 | |
4213 | |
… | |
… | |
4356 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4387 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4357 | |
4388 | |
4358 | s_return (newstr); |
4389 | s_return (newstr); |
4359 | } |
4390 | } |
4360 | |
4391 | |
4361 | case OP_SUBSTR: /* substring */ |
4392 | case OP_STRING_COPY: /* substring/string-copy */ |
4362 | { |
4393 | { |
4363 | char *str = strvalue (x); |
4394 | char *str = strvalue (x); |
4364 | int index0 = ivalue_unchecked (cadr (args)); |
4395 | int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args)); |
4365 | int index1; |
4396 | int index1; |
4366 | int len; |
4397 | int len; |
4367 | |
4398 | |
4368 | if (index0 > strlength (x)) |
4399 | if (index0 > strlength (x)) |
4369 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4400 | Error_1 ("string->copy: start out of bounds:", cadr (args)); |
4370 | |
4401 | |
4371 | if (cddr (args) != NIL) |
4402 | if (cddr (args) != NIL) |
4372 | { |
4403 | { |
4373 | index1 = ivalue_unchecked (caddr (args)); |
4404 | index1 = ivalue_unchecked (caddr (args)); |
4374 | |
4405 | |
4375 | if (index1 > strlength (x) || index1 < index0) |
4406 | if (index1 > strlength (x) || index1 < index0) |
4376 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4407 | Error_1 ("string->copy: end out of bounds:", caddr (args)); |
4377 | } |
4408 | } |
4378 | else |
4409 | else |
4379 | index1 = strlength (x); |
4410 | index1 = strlength (x); |
4380 | |
4411 | |
4381 | len = index1 - index0; |
4412 | len = index1 - index0; |
4382 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4413 | x = mk_counted_string (SCHEME_A_ str + index0, len); |
4383 | memcpy (strvalue (x), str + index0, len); |
|
|
4384 | strvalue (x)[len] = 0; |
|
|
4385 | |
4414 | |
4386 | s_return (x); |
4415 | s_return (x); |
4387 | } |
4416 | } |
4388 | |
4417 | |
4389 | case OP_VECTOR: /* vector */ |
4418 | case OP_VECTOR: /* vector */ |
… | |
… | |
4401 | if (SCHEME_V->no_memory) |
4430 | if (SCHEME_V->no_memory) |
4402 | s_return (S_SINK); |
4431 | s_return (S_SINK); |
4403 | #endif |
4432 | #endif |
4404 | |
4433 | |
4405 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4434 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4406 | set_vector_elem (vec, i, car (x)); |
4435 | vector_set (vec, i, car (x)); |
4407 | |
4436 | |
4408 | s_return (vec); |
4437 | s_return (vec); |
4409 | } |
4438 | } |
4410 | |
4439 | |
4411 | case OP_MKVECTOR: /* make-vector */ |
4440 | case OP_MKVECTOR: /* make-vector */ |
… | |
… | |
4423 | if (SCHEME_V->no_memory) |
4452 | if (SCHEME_V->no_memory) |
4424 | s_return (S_SINK); |
4453 | s_return (S_SINK); |
4425 | #endif |
4454 | #endif |
4426 | |
4455 | |
4427 | if (fill != NIL) |
4456 | if (fill != NIL) |
4428 | fill_vector (vec, fill); |
4457 | fill_vector (vec, 0, fill); |
4429 | |
4458 | |
4430 | s_return (vec); |
4459 | s_return (vec); |
4431 | } |
4460 | } |
4432 | |
4461 | |
4433 | case OP_VECLEN: /* vector-length */ |
4462 | case OP_VECLEN: /* vector-length */ |
4434 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4463 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
|
|
4464 | |
|
|
4465 | case OP_VECRESIZE: |
|
|
4466 | vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args)); |
|
|
4467 | s_return (x); |
4435 | |
4468 | |
4436 | case OP_VECREF: /* vector-ref */ |
4469 | case OP_VECREF: /* vector-ref */ |
4437 | { |
4470 | { |
4438 | int index = ivalue_unchecked (cadr (args)); |
4471 | int index = ivalue_unchecked (cadr (args)); |
4439 | |
4472 | |
4440 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4473 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4441 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4474 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4442 | |
4475 | |
4443 | s_return (vector_elem (x, index)); |
4476 | s_return (vector_get (x, index)); |
4444 | } |
4477 | } |
4445 | |
4478 | |
4446 | case OP_VECSET: /* vector-set! */ |
4479 | case OP_VECSET: /* vector-set! */ |
4447 | { |
4480 | { |
4448 | int index = ivalue_unchecked (cadr (args)); |
4481 | int index = ivalue_unchecked (cadr (args)); |
… | |
… | |
4451 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4484 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4452 | |
4485 | |
4453 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4486 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4454 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4487 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4455 | |
4488 | |
4456 | set_vector_elem (x, index, caddr (args)); |
4489 | vector_set (x, index, caddr (args)); |
4457 | s_return (x); |
4490 | s_return (x); |
4458 | } |
4491 | } |
4459 | } |
4492 | } |
4460 | |
4493 | |
4461 | if (USE_ERROR_CHECKING) abort (); |
4494 | if (USE_ERROR_CHECKING) abort (); |
4462 | } |
4495 | } |
4463 | |
4496 | |
4464 | static int |
4497 | /* relational ops */ |
|
|
4498 | ecb_hot static int |
4465 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4499 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4466 | { |
4500 | { |
4467 | pointer x = SCHEME_V->args; |
4501 | pointer x = SCHEME_V->args; |
4468 | |
4502 | |
4469 | for (;;) |
4503 | for (;;) |
… | |
… | |
4490 | } |
4524 | } |
4491 | |
4525 | |
4492 | s_return (S_T); |
4526 | s_return (S_T); |
4493 | } |
4527 | } |
4494 | |
4528 | |
4495 | static int |
4529 | /* predicates */ |
|
|
4530 | ecb_hot static int |
4496 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4531 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4497 | { |
4532 | { |
4498 | pointer args = SCHEME_V->args; |
4533 | pointer args = SCHEME_V->args; |
4499 | pointer a = car (args); |
4534 | pointer a = car (args); |
4500 | pointer d = cdr (args); |
4535 | pointer d = cdr (args); |
4501 | int r; |
4536 | int r; |
4502 | |
4537 | |
4503 | switch (op) |
4538 | switch (op) |
4504 | { |
4539 | { |
4505 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4540 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4506 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; |
4541 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break; |
4507 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4542 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4508 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4543 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4509 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
4544 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
|
|
4545 | case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break; |
4510 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4546 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4511 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4547 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4512 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4548 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4513 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4549 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4514 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4550 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4515 | |
4551 | |
4516 | #if USE_CHAR_CLASSIFIERS |
4552 | #if USE_CHAR_CLASSIFIERS |
4517 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4553 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4518 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4554 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4519 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4555 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
… | |
… | |
4546 | } |
4582 | } |
4547 | |
4583 | |
4548 | s_retbool (r); |
4584 | s_retbool (r); |
4549 | } |
4585 | } |
4550 | |
4586 | |
4551 | static int |
4587 | /* promises, list ops, ports */ |
|
|
4588 | ecb_hot static int |
4552 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4589 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4553 | { |
4590 | { |
4554 | pointer args = SCHEME_V->args; |
4591 | pointer args = SCHEME_V->args; |
4555 | pointer a = car (args); |
4592 | pointer a = car (args); |
4556 | pointer x, y; |
4593 | pointer x, y; |
… | |
… | |
4569 | } |
4606 | } |
4570 | else |
4607 | else |
4571 | s_return (SCHEME_V->code); |
4608 | s_return (SCHEME_V->code); |
4572 | |
4609 | |
4573 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4610 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4574 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4611 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4575 | s_return (SCHEME_V->value); |
4612 | s_return (SCHEME_V->value); |
4576 | |
4613 | |
4577 | #if USE_PORTS |
4614 | #if USE_PORTS |
|
|
4615 | |
|
|
4616 | case OP_EOF_OBJECT: /* eof-object */ |
|
|
4617 | s_return (S_EOF); |
4578 | |
4618 | |
4579 | case OP_WRITE: /* write */ |
4619 | case OP_WRITE: /* write */ |
4580 | case OP_DISPLAY: /* display */ |
4620 | case OP_DISPLAY: /* display */ |
4581 | case OP_WRITE_CHAR: /* write-char */ |
4621 | case OP_WRITE_CHAR: /* write-char */ |
4582 | if (is_pair (cdr (SCHEME_V->args))) |
4622 | if (is_pair (cdr (SCHEME_V->args))) |
… | |
… | |
4724 | SCHEME_V->gc_verbose = (a != S_F); |
4764 | SCHEME_V->gc_verbose = (a != S_F); |
4725 | s_retbool (was); |
4765 | s_retbool (was); |
4726 | } |
4766 | } |
4727 | |
4767 | |
4728 | case OP_NEWSEGMENT: /* new-segment */ |
4768 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
4769 | #if 0 |
4729 | if (!is_pair (args) || !is_number (a)) |
4770 | if (!is_pair (args) || !is_number (a)) |
4730 | Error_0 ("new-segment: argument must be a number"); |
4771 | Error_0 ("new-segment: argument must be a number"); |
4731 | |
4772 | #endif |
4732 | alloc_cellseg (SCHEME_A_ ivalue (a)); |
4773 | s_retbool (alloc_cellseg (SCHEME_A)); |
4733 | |
|
|
4734 | s_return (S_T); |
|
|
4735 | |
4774 | |
4736 | case OP_OBLIST: /* oblist */ |
4775 | case OP_OBLIST: /* oblist */ |
4737 | s_return (oblist_all_symbols (SCHEME_A)); |
4776 | s_return (oblist_all_symbols (SCHEME_A)); |
4738 | |
4777 | |
4739 | #if USE_PORTS |
4778 | #if USE_PORTS |
… | |
… | |
4809 | s_return (p == NIL ? S_F : p); |
4848 | s_return (p == NIL ? S_F : p); |
4810 | } |
4849 | } |
4811 | |
4850 | |
4812 | case OP_GET_OUTSTRING: /* get-output-string */ |
4851 | case OP_GET_OUTSTRING: /* get-output-string */ |
4813 | { |
4852 | { |
4814 | port *p; |
4853 | port *p = port (a); |
4815 | |
4854 | |
4816 | if ((p = a->object.port)->kind & port_string) |
4855 | if (p->kind & port_string) |
4817 | { |
4856 | { |
4818 | off_t size; |
4857 | off_t size; |
4819 | char *str; |
4858 | char *str; |
4820 | |
4859 | |
4821 | size = p->rep.string.curr - p->rep.string.start + 1; |
4860 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4856 | } |
4895 | } |
4857 | |
4896 | |
4858 | if (USE_ERROR_CHECKING) abort (); |
4897 | if (USE_ERROR_CHECKING) abort (); |
4859 | } |
4898 | } |
4860 | |
4899 | |
4861 | static int |
4900 | /* reading */ |
|
|
4901 | ecb_cold static int |
4862 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4902 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4863 | { |
4903 | { |
4864 | pointer args = SCHEME_V->args; |
4904 | pointer args = SCHEME_V->args; |
4865 | pointer x; |
4905 | pointer x; |
4866 | |
4906 | |
… | |
… | |
4926 | int res; |
4966 | int res; |
4927 | |
4967 | |
4928 | if (is_pair (args)) |
4968 | if (is_pair (args)) |
4929 | p = car (args); |
4969 | p = car (args); |
4930 | |
4970 | |
4931 | res = p->object.port->kind & port_string; |
4971 | res = port (p)->kind & port_string; |
4932 | |
4972 | |
4933 | s_retbool (res); |
4973 | s_retbool (res); |
4934 | } |
4974 | } |
4935 | |
4975 | |
4936 | case OP_SET_INPORT: /* set-input-port */ |
4976 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
4995 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5035 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
4996 | SCHEME_V->tok = token (SCHEME_A); |
5036 | SCHEME_V->tok = token (SCHEME_A); |
4997 | s_goto (OP_RDSEXPR); |
5037 | s_goto (OP_RDSEXPR); |
4998 | |
5038 | |
4999 | case TOK_ATOM: |
5039 | case TOK_ATOM: |
5000 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); |
5040 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); |
|
|
5041 | |
|
|
5042 | case TOK_DOTATOM: |
|
|
5043 | SCHEME_V->strbuff[0] = '.'; |
|
|
5044 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
|
|
5045 | |
|
|
5046 | case TOK_STRATOM: |
|
|
5047 | x = readstrexp (SCHEME_A_ '|'); |
|
|
5048 | //TODO: haven't checked whether the garbage collector could interfere |
|
|
5049 | s_return (mk_atom (SCHEME_A_ strvalue (x))); |
5001 | |
5050 | |
5002 | case TOK_DQUOTE: |
5051 | case TOK_DQUOTE: |
5003 | x = readstrexp (SCHEME_A); |
5052 | x = readstrexp (SCHEME_A_ '"'); |
5004 | |
5053 | |
5005 | if (x == S_F) |
5054 | if (x == S_F) |
5006 | Error_0 ("Error reading string"); |
5055 | Error_0 ("Error reading string"); |
5007 | |
5056 | |
5008 | setimmutable (x); |
5057 | setimmutable (x); |
… | |
… | |
5020 | s_goto (OP_EVAL); |
5069 | s_goto (OP_EVAL); |
5021 | } |
5070 | } |
5022 | } |
5071 | } |
5023 | |
5072 | |
5024 | case TOK_SHARP_CONST: |
5073 | case TOK_SHARP_CONST: |
5025 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) |
5074 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) |
5026 | Error_0 ("undefined sharp expression"); |
5075 | Error_0 ("undefined sharp expression"); |
5027 | else |
5076 | else |
5028 | s_return (x); |
5077 | s_return (x); |
5029 | |
5078 | |
5030 | default: |
5079 | default: |
… | |
… | |
5182 | putstr (SCHEME_A_ ")"); |
5231 | putstr (SCHEME_A_ ")"); |
5183 | s_return (S_T); |
5232 | s_return (S_T); |
5184 | } |
5233 | } |
5185 | else |
5234 | else |
5186 | { |
5235 | { |
5187 | pointer elem = vector_elem (vec, i); |
5236 | pointer elem = vector_get (vec, i); |
5188 | |
5237 | |
5189 | ivalue_unchecked (cdr (args)) = i + 1; |
5238 | ivalue_unchecked (cdr (args)) = i + 1; |
5190 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5239 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5191 | SCHEME_V->args = elem; |
5240 | SCHEME_V->args = elem; |
5192 | |
5241 | |
… | |
… | |
5199 | } |
5248 | } |
5200 | |
5249 | |
5201 | if (USE_ERROR_CHECKING) abort (); |
5250 | if (USE_ERROR_CHECKING) abort (); |
5202 | } |
5251 | } |
5203 | |
5252 | |
5204 | static int |
5253 | /* list ops */ |
|
|
5254 | ecb_hot static int |
5205 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5255 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5206 | { |
5256 | { |
5207 | pointer args = SCHEME_V->args; |
5257 | pointer args = SCHEME_V->args; |
5208 | pointer a = car (args); |
5258 | pointer a = car (args); |
5209 | pointer x, y; |
5259 | pointer x, y; |
… | |
… | |
5252 | |
5302 | |
5253 | case OP_CLOSUREP: /* closure? */ |
5303 | case OP_CLOSUREP: /* closure? */ |
5254 | /* |
5304 | /* |
5255 | * Note, macro object is also a closure. |
5305 | * Note, macro object is also a closure. |
5256 | * Therefore, (closure? <#MACRO>) ==> #t |
5306 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5307 | * (schmorp) well, obviously not, fix? TODO |
5257 | */ |
5308 | */ |
5258 | s_retbool (is_closure (a)); |
5309 | s_retbool (is_closure (a)); |
5259 | |
5310 | |
5260 | case OP_MACROP: /* macro? */ |
5311 | case OP_MACROP: /* macro? */ |
5261 | s_retbool (is_macro (a)); |
5312 | s_retbool (is_macro (a)); |
… | |
… | |
5266 | |
5317 | |
5267 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5318 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5268 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5319 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5269 | |
5320 | |
5270 | typedef int (*test_predicate)(pointer); |
5321 | typedef int (*test_predicate)(pointer); |
5271 | static int |
5322 | |
|
|
5323 | ecb_hot static int |
5272 | tst_any (pointer p) |
5324 | tst_any (pointer p) |
5273 | { |
5325 | { |
5274 | return 1; |
5326 | return 1; |
5275 | } |
5327 | } |
5276 | |
5328 | |
5277 | static int |
5329 | ecb_hot static int |
5278 | tst_inonneg (pointer p) |
5330 | tst_inonneg (pointer p) |
5279 | { |
5331 | { |
5280 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5332 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
5281 | } |
5333 | } |
5282 | |
5334 | |
5283 | static int |
5335 | ecb_hot static int |
5284 | tst_is_list (SCHEME_P_ pointer p) |
5336 | tst_is_list (SCHEME_P_ pointer p) |
5285 | { |
5337 | { |
5286 | return p == NIL || is_pair (p); |
5338 | return p == NIL || is_pair (p); |
5287 | } |
5339 | } |
5288 | |
5340 | |
… | |
… | |
5331 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5383 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
5332 | #include "opdefines.h" |
5384 | #include "opdefines.h" |
5333 | #undef OP_DEF |
5385 | #undef OP_DEF |
5334 | ; |
5386 | ; |
5335 | |
5387 | |
5336 | static const char * |
5388 | ecb_cold static const char * |
5337 | opname (int idx) |
5389 | opname (int idx) |
5338 | { |
5390 | { |
5339 | const char *name = opnames; |
5391 | const char *name = opnames; |
5340 | |
5392 | |
5341 | /* should do this at compile time, but would require external program, right? */ |
5393 | /* should do this at compile time, but would require external program, right? */ |
… | |
… | |
5343 | name += strlen (name) + 1; |
5395 | name += strlen (name) + 1; |
5344 | |
5396 | |
5345 | return *name ? name : "ILLEGAL"; |
5397 | return *name ? name : "ILLEGAL"; |
5346 | } |
5398 | } |
5347 | |
5399 | |
5348 | static const char * |
5400 | ecb_cold static const char * |
5349 | procname (pointer x) |
5401 | procname (pointer x) |
5350 | { |
5402 | { |
5351 | return opname (procnum (x)); |
5403 | return opname (procnum (x)); |
5352 | } |
5404 | } |
5353 | |
5405 | |
… | |
… | |
5373 | #undef OP_DEF |
5425 | #undef OP_DEF |
5374 | {0} |
5426 | {0} |
5375 | }; |
5427 | }; |
5376 | |
5428 | |
5377 | /* kernel of this interpreter */ |
5429 | /* kernel of this interpreter */ |
5378 | static void ecb_hot |
5430 | ecb_hot static void |
5379 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5431 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5380 | { |
5432 | { |
5381 | SCHEME_V->op = op; |
5433 | SCHEME_V->op = op; |
5382 | |
5434 | |
5383 | for (;;) |
5435 | for (;;) |
… | |
… | |
5430 | { |
5482 | { |
5431 | if (!tests[j - 1].fct (arg)) |
5483 | if (!tests[j - 1].fct (arg)) |
5432 | break; |
5484 | break; |
5433 | } |
5485 | } |
5434 | |
5486 | |
5435 | if (t[1]) /* last test is replicated as necessary */ |
5487 | if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */ |
5436 | t++; |
5488 | t++; |
5437 | |
5489 | |
5438 | arglist = cdr (arglist); |
5490 | arglist = cdr (arglist); |
5439 | i++; |
5491 | i++; |
5440 | } |
5492 | } |
… | |
… | |
5466 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5518 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5467 | return; |
5519 | return; |
5468 | |
5520 | |
5469 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5521 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5470 | { |
5522 | { |
5471 | xwrstr ("No memory!\n"); |
5523 | putstr (SCHEME_A_ "No memory!\n"); |
5472 | return; |
5524 | return; |
5473 | } |
5525 | } |
5474 | } |
5526 | } |
5475 | } |
5527 | } |
5476 | |
5528 | |
5477 | /* ========== Initialization of internal keywords ========== */ |
5529 | /* ========== Initialization of internal keywords ========== */ |
5478 | |
5530 | |
5479 | static void |
5531 | ecb_cold static void |
5480 | assign_syntax (SCHEME_P_ const char *name) |
5532 | assign_syntax (SCHEME_P_ const char *name) |
5481 | { |
5533 | { |
5482 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5534 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5483 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5535 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5484 | } |
5536 | } |
5485 | |
5537 | |
5486 | static void |
5538 | ecb_cold static void |
5487 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5539 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
5488 | { |
5540 | { |
5489 | pointer x = mk_symbol (SCHEME_A_ name); |
5541 | pointer x = mk_symbol (SCHEME_A_ name); |
5490 | pointer y = mk_proc (SCHEME_A_ op); |
5542 | pointer y = mk_proc (SCHEME_A_ op); |
5491 | new_slot_in_env (SCHEME_A_ x, y); |
5543 | new_slot_in_env (SCHEME_A_ x, y); |
… | |
… | |
5499 | ivalue_unchecked (y) = op; |
5551 | ivalue_unchecked (y) = op; |
5500 | return y; |
5552 | return y; |
5501 | } |
5553 | } |
5502 | |
5554 | |
5503 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5555 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5504 | static int |
5556 | ecb_hot static int |
5505 | syntaxnum (pointer p) |
5557 | syntaxnum (pointer p) |
5506 | { |
5558 | { |
5507 | const char *s = strvalue (car (p)); |
5559 | const char *s = strvalue (p); |
5508 | |
5560 | |
5509 | switch (strlength (car (p))) |
5561 | switch (strlength (p)) |
5510 | { |
5562 | { |
5511 | case 2: |
5563 | case 2: |
5512 | if (s[0] == 'i') |
5564 | if (s[0] == 'i') |
5513 | return OP_IF0; /* if */ |
5565 | return OP_IF0; /* if */ |
5514 | else |
5566 | else |
… | |
… | |
5589 | ecb_cold int |
5641 | ecb_cold int |
5590 | scheme_init (SCHEME_P) |
5642 | scheme_init (SCHEME_P) |
5591 | { |
5643 | { |
5592 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5644 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5593 | pointer x; |
5645 | pointer x; |
|
|
5646 | |
|
|
5647 | /* this memset is not strictly correct, as we assume (intcache) |
|
|
5648 | * that memset 0 will also set pointers to 0, but memset does |
|
|
5649 | * of course not guarantee that. screw such systems. |
|
|
5650 | */ |
|
|
5651 | memset (SCHEME_V, 0, sizeof (*SCHEME_V)); |
5594 | |
5652 | |
5595 | num_set_fixnum (num_zero, 1); |
5653 | num_set_fixnum (num_zero, 1); |
5596 | num_set_ivalue (num_zero, 0); |
5654 | num_set_ivalue (num_zero, 0); |
5597 | num_set_fixnum (num_one, 1); |
5655 | num_set_fixnum (num_one, 1); |
5598 | num_set_ivalue (num_one, 1); |
5656 | num_set_ivalue (num_one, 1); |
… | |
… | |
5610 | SCHEME_V->save_inport = NIL; |
5668 | SCHEME_V->save_inport = NIL; |
5611 | SCHEME_V->loadport = NIL; |
5669 | SCHEME_V->loadport = NIL; |
5612 | SCHEME_V->nesting = 0; |
5670 | SCHEME_V->nesting = 0; |
5613 | SCHEME_V->interactive_repl = 0; |
5671 | SCHEME_V->interactive_repl = 0; |
5614 | |
5672 | |
5615 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5673 | if (!alloc_cellseg (SCHEME_A)) |
5616 | { |
5674 | { |
5617 | #if USE_ERROR_CHECKING |
5675 | #if USE_ERROR_CHECKING |
5618 | SCHEME_V->no_memory = 1; |
5676 | SCHEME_V->no_memory = 1; |
5619 | return 0; |
5677 | return 0; |
5620 | #endif |
5678 | #endif |
… | |
… | |
5688 | |
5746 | |
5689 | return !SCHEME_V->no_memory; |
5747 | return !SCHEME_V->no_memory; |
5690 | } |
5748 | } |
5691 | |
5749 | |
5692 | #if USE_PORTS |
5750 | #if USE_PORTS |
5693 | void |
5751 | ecb_cold void |
5694 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5752 | scheme_set_input_port_file (SCHEME_P_ int fin) |
5695 | { |
5753 | { |
5696 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5754 | SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); |
5697 | } |
5755 | } |
5698 | |
5756 | |
5699 | void |
5757 | ecb_cold void |
5700 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5758 | scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) |
5701 | { |
5759 | { |
5702 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5760 | SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); |
5703 | } |
5761 | } |
5704 | |
5762 | |
5705 | void |
5763 | ecb_cold void |
5706 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5764 | scheme_set_output_port_file (SCHEME_P_ int fout) |
5707 | { |
5765 | { |
5708 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5766 | SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); |
5709 | } |
5767 | } |
5710 | |
5768 | |
5711 | void |
5769 | ecb_cold void |
5712 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5770 | scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) |
5713 | { |
5771 | { |
5714 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5772 | SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); |
5715 | } |
5773 | } |
5716 | #endif |
5774 | #endif |
5717 | |
5775 | |
5718 | void |
5776 | ecb_cold void |
5719 | scheme_set_external_data (SCHEME_P_ void *p) |
5777 | scheme_set_external_data (SCHEME_P_ void *p) |
5720 | { |
5778 | { |
5721 | SCHEME_V->ext_data = p; |
5779 | SCHEME_V->ext_data = p; |
5722 | } |
5780 | } |
5723 | |
5781 | |
… | |
… | |
5771 | } |
5829 | } |
5772 | } |
5830 | } |
5773 | #endif |
5831 | #endif |
5774 | } |
5832 | } |
5775 | |
5833 | |
5776 | void |
5834 | ecb_cold void |
5777 | scheme_load_file (SCHEME_P_ int fin) |
5835 | scheme_load_file (SCHEME_P_ int fin) |
5778 | { |
5836 | { |
5779 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5837 | scheme_load_named_file (SCHEME_A_ fin, 0); |
5780 | } |
5838 | } |
5781 | |
5839 | |
5782 | void |
5840 | ecb_cold void |
5783 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5841 | scheme_load_named_file (SCHEME_P_ int fin, const char *filename) |
5784 | { |
5842 | { |
5785 | dump_stack_reset (SCHEME_A); |
5843 | dump_stack_reset (SCHEME_A); |
5786 | SCHEME_V->envir = SCHEME_V->global_env; |
5844 | SCHEME_V->envir = SCHEME_V->global_env; |
5787 | SCHEME_V->file_i = 0; |
5845 | SCHEME_V->file_i = 0; |
… | |
… | |
5814 | |
5872 | |
5815 | if (SCHEME_V->retcode == 0) |
5873 | if (SCHEME_V->retcode == 0) |
5816 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5874 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5817 | } |
5875 | } |
5818 | |
5876 | |
5819 | void |
5877 | ecb_cold void |
5820 | scheme_load_string (SCHEME_P_ const char *cmd) |
5878 | scheme_load_string (SCHEME_P_ const char *cmd) |
5821 | { |
5879 | { |
5822 | dump_stack_reset (SCHEME_A); |
5880 | dump_stack_reset (SCHEME_A); |
5823 | SCHEME_V->envir = SCHEME_V->global_env; |
5881 | SCHEME_V->envir = SCHEME_V->global_env; |
5824 | SCHEME_V->file_i = 0; |
5882 | SCHEME_V->file_i = 0; |
… | |
… | |
5838 | |
5896 | |
5839 | if (SCHEME_V->retcode == 0) |
5897 | if (SCHEME_V->retcode == 0) |
5840 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5898 | SCHEME_V->retcode = SCHEME_V->nesting != 0; |
5841 | } |
5899 | } |
5842 | |
5900 | |
5843 | void |
5901 | ecb_cold void |
5844 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
5902 | scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) |
5845 | { |
5903 | { |
5846 | pointer x; |
5904 | pointer x; |
5847 | |
5905 | |
5848 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
5906 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
… | |
… | |
5853 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
5911 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
5854 | } |
5912 | } |
5855 | |
5913 | |
5856 | #if !STANDALONE |
5914 | #if !STANDALONE |
5857 | |
5915 | |
5858 | void |
5916 | ecb_cold void |
5859 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
5917 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
5860 | { |
5918 | { |
5861 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
5919 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
5862 | } |
5920 | } |
5863 | |
5921 | |
5864 | void |
5922 | ecb_cold void |
5865 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
5923 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
5866 | { |
5924 | { |
5867 | int i; |
5925 | int i; |
5868 | |
5926 | |
5869 | for (i = 0; i < count; i++) |
5927 | for (i = 0; i < count; i++) |
5870 | scheme_register_foreign_func (SCHEME_A_ list + i); |
5928 | scheme_register_foreign_func (SCHEME_A_ list + i); |
5871 | } |
5929 | } |
5872 | |
5930 | |
5873 | pointer |
5931 | ecb_cold pointer |
5874 | scheme_apply0 (SCHEME_P_ const char *procname) |
5932 | scheme_apply0 (SCHEME_P_ const char *procname) |
5875 | { |
5933 | { |
5876 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
5934 | return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); |
5877 | } |
5935 | } |
5878 | |
5936 | |
5879 | void |
5937 | ecb_cold void |
5880 | save_from_C_call (SCHEME_P) |
5938 | save_from_C_call (SCHEME_P) |
5881 | { |
5939 | { |
5882 | pointer saved_data = cons (car (S_SINK), |
5940 | pointer saved_data = cons (car (S_SINK), |
5883 | cons (SCHEME_V->envir, |
5941 | cons (SCHEME_V->envir, |
5884 | SCHEME_V->dump)); |
5942 | SCHEME_V->dump)); |
… | |
… | |
5888 | /* Truncate the dump stack so TS will return here when done, not |
5946 | /* Truncate the dump stack so TS will return here when done, not |
5889 | directly resume pre-C-call operations. */ |
5947 | directly resume pre-C-call operations. */ |
5890 | dump_stack_reset (SCHEME_A); |
5948 | dump_stack_reset (SCHEME_A); |
5891 | } |
5949 | } |
5892 | |
5950 | |
5893 | void |
5951 | ecb_cold void |
5894 | restore_from_C_call (SCHEME_P) |
5952 | restore_from_C_call (SCHEME_P) |
5895 | { |
5953 | { |
5896 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
5954 | set_car (S_SINK, caar (SCHEME_V->c_nest)); |
5897 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
5955 | SCHEME_V->envir = cadar (SCHEME_V->c_nest); |
5898 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
5956 | SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); |
5899 | /* Pop */ |
5957 | /* Pop */ |
5900 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
5958 | SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); |
5901 | } |
5959 | } |
5902 | |
5960 | |
5903 | /* "func" and "args" are assumed to be already eval'ed. */ |
5961 | /* "func" and "args" are assumed to be already eval'ed. */ |
5904 | pointer |
5962 | ecb_cold pointer |
5905 | scheme_call (SCHEME_P_ pointer func, pointer args) |
5963 | scheme_call (SCHEME_P_ pointer func, pointer args) |
5906 | { |
5964 | { |
5907 | int old_repl = SCHEME_V->interactive_repl; |
5965 | int old_repl = SCHEME_V->interactive_repl; |
5908 | |
5966 | |
5909 | SCHEME_V->interactive_repl = 0; |
5967 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5916 | SCHEME_V->interactive_repl = old_repl; |
5974 | SCHEME_V->interactive_repl = old_repl; |
5917 | restore_from_C_call (SCHEME_A); |
5975 | restore_from_C_call (SCHEME_A); |
5918 | return SCHEME_V->value; |
5976 | return SCHEME_V->value; |
5919 | } |
5977 | } |
5920 | |
5978 | |
5921 | pointer |
5979 | ecb_cold pointer |
5922 | scheme_eval (SCHEME_P_ pointer obj) |
5980 | scheme_eval (SCHEME_P_ pointer obj) |
5923 | { |
5981 | { |
5924 | int old_repl = SCHEME_V->interactive_repl; |
5982 | int old_repl = SCHEME_V->interactive_repl; |
5925 | |
5983 | |
5926 | SCHEME_V->interactive_repl = 0; |
5984 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
5938 | |
5996 | |
5939 | /* ========== Main ========== */ |
5997 | /* ========== Main ========== */ |
5940 | |
5998 | |
5941 | #if STANDALONE |
5999 | #if STANDALONE |
5942 | |
6000 | |
5943 | int |
6001 | ecb_cold int |
5944 | main (int argc, char **argv) |
6002 | main (int argc, char **argv) |
5945 | { |
6003 | { |
5946 | # if USE_MULTIPLICITY |
6004 | # if USE_MULTIPLICITY |
5947 | scheme ssc; |
6005 | scheme ssc; |
5948 | scheme *const SCHEME_V = &ssc; |
6006 | scheme *const SCHEME_V = &ssc; |
… | |
… | |
5950 | # endif |
6008 | # endif |
5951 | int fin; |
6009 | int fin; |
5952 | char *file_name = InitFile; |
6010 | char *file_name = InitFile; |
5953 | int retcode; |
6011 | int retcode; |
5954 | int isfile = 1; |
6012 | int isfile = 1; |
|
|
6013 | system ("ps v $PPID");//D |
5955 | |
6014 | |
5956 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6015 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5957 | { |
6016 | { |
5958 | xwrstr ("Usage: tinyscheme -?\n"); |
6017 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
5959 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6018 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
5960 | xwrstr ("followed by\n"); |
6019 | putstr (SCHEME_A_ "followed by\n"); |
5961 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6020 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
5962 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6021 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
5963 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6022 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
5964 | xwrstr ("Use - as filename for stdin.\n"); |
6023 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
5965 | return 1; |
6024 | return 1; |
5966 | } |
6025 | } |
5967 | |
6026 | |
5968 | if (!scheme_init (SCHEME_A)) |
6027 | if (!scheme_init (SCHEME_A)) |
5969 | { |
6028 | { |
5970 | xwrstr ("Could not initialize!\n"); |
6029 | putstr (SCHEME_A_ "Could not initialize!\n"); |
5971 | return 2; |
6030 | return 2; |
5972 | } |
6031 | } |
5973 | |
6032 | |
5974 | # if USE_PORTS |
6033 | # if USE_PORTS |
5975 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6034 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
6020 | fin = open (file_name, O_RDONLY); |
6079 | fin = open (file_name, O_RDONLY); |
6021 | #endif |
6080 | #endif |
6022 | |
6081 | |
6023 | if (isfile && fin < 0) |
6082 | if (isfile && fin < 0) |
6024 | { |
6083 | { |
6025 | xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); |
6084 | putstr (SCHEME_A_ "Could not open file "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); |
6026 | } |
6085 | } |
6027 | else |
6086 | else |
6028 | { |
6087 | { |
6029 | if (isfile) |
6088 | if (isfile) |
6030 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6089 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
… | |
… | |
6034 | #if USE_PORTS |
6093 | #if USE_PORTS |
6035 | if (!isfile || fin != STDIN_FILENO) |
6094 | if (!isfile || fin != STDIN_FILENO) |
6036 | { |
6095 | { |
6037 | if (SCHEME_V->retcode != 0) |
6096 | if (SCHEME_V->retcode != 0) |
6038 | { |
6097 | { |
6039 | xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); |
6098 | putstr (SCHEME_A_ "Errors encountered reading "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); |
6040 | } |
6099 | } |
6041 | |
6100 | |
6042 | if (isfile) |
6101 | if (isfile) |
6043 | close (fin); |
6102 | close (fin); |
6044 | } |
6103 | } |