… | |
… | |
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 | |
… | |
… | |
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 | |
… | |
… | |
103 | { |
123 | { |
104 | xbase (s, n, 10); |
124 | xbase (s, n, 10); |
105 | } |
125 | } |
106 | |
126 | |
107 | static void |
127 | 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 | static char |
123 | xtoupper (char c) |
137 | xtoupper (char c) |
124 | { |
138 | { |
… | |
… | |
174 | # define prompt "ts> " |
188 | # define prompt "ts> " |
175 | #endif |
189 | #endif |
176 | |
190 | |
177 | #ifndef InitFile |
191 | #ifndef InitFile |
178 | # define InitFile "init.scm" |
192 | # define InitFile "init.scm" |
179 | #endif |
|
|
180 | |
|
|
181 | #ifndef FIRST_CELLSEGS |
|
|
182 | # define FIRST_CELLSEGS 3 |
|
|
183 | #endif |
193 | #endif |
184 | |
194 | |
185 | enum scheme_types |
195 | enum scheme_types |
186 | { |
196 | { |
187 | T_INTEGER, |
197 | T_INTEGER, |
… | |
… | |
248 | static int is_zero_rvalue (RVALUE x); |
258 | static int is_zero_rvalue (RVALUE x); |
249 | |
259 | |
250 | static num num_zero; |
260 | static num num_zero; |
251 | static num num_one; |
261 | static num num_one; |
252 | |
262 | |
|
|
263 | /* convert "pointer" to cell* / cell* to pointer */ |
|
|
264 | #define CELL(p) ((struct cell *)(p) + 0) |
|
|
265 | #define POINTER(c) ((void *)((c) - 0)) |
|
|
266 | |
253 | /* macros for cell operations */ |
267 | /* macros for cell operations */ |
254 | #define typeflag(p) ((p)->flag + 0) |
268 | #define typeflag(p) (CELL(p)->flag + 0) |
255 | #define set_typeflag(p,v) ((p)->flag = (v)) |
269 | #define set_typeflag(p,v) (CELL(p)->flag = (v)) |
256 | #define type(p) (typeflag (p) & T_MASKTYPE) |
270 | #define type(p) (typeflag (p) & T_MASKTYPE) |
257 | |
271 | |
258 | INTERFACE int |
272 | INTERFACE int |
259 | is_string (pointer p) |
273 | is_string (pointer p) |
260 | { |
274 | { |
261 | return type (p) == T_STRING; |
275 | return type (p) == T_STRING; |
262 | } |
276 | } |
263 | |
277 | |
264 | #define strvalue(p) ((p)->object.string.svalue) |
278 | #define strvalue(p) (CELL(p)->object.string.svalue) |
265 | #define strlength(p) ((p)->object.string.length) |
279 | #define strlength(p) (CELL(p)->object.string.length) |
266 | |
280 | |
267 | INTERFACE int |
281 | INTERFACE int |
268 | is_vector (pointer p) |
282 | is_vector (pointer p) |
269 | { |
283 | { |
270 | return type (p) == T_VECTOR; |
284 | return type (p) == T_VECTOR; |
271 | } |
285 | } |
272 | |
286 | |
273 | #define vecvalue(p) ((p)->object.vector.vvalue) |
287 | #define vecvalue(p) (CELL(p)->object.vector.vvalue) |
274 | #define veclength(p) ((p)->object.vector.length) |
288 | #define veclength(p) (CELL(p)->object.vector.length) |
275 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
289 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
276 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
290 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
277 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
291 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
278 | |
292 | |
279 | INTERFACE int |
293 | INTERFACE int |
… | |
… | |
305 | string_value (pointer p) |
319 | string_value (pointer p) |
306 | { |
320 | { |
307 | return strvalue (p); |
321 | return strvalue (p); |
308 | } |
322 | } |
309 | |
323 | |
310 | #define ivalue_unchecked(p) (p)->object.ivalue |
324 | #define ivalue_unchecked(p) CELL(p)->object.ivalue |
311 | #define set_ivalue(p,v) (p)->object.ivalue = (v) |
325 | #define set_ivalue(p,v) CELL(p)->object.ivalue = (v) |
312 | |
326 | |
313 | #if USE_REAL |
327 | #if USE_REAL |
314 | #define rvalue_unchecked(p) (p)->object.rvalue |
328 | #define rvalue_unchecked(p) CELL(p)->object.rvalue |
315 | #define set_rvalue(p,v) (p)->object.rvalue = (v) |
329 | #define set_rvalue(p,v) CELL(p)->object.rvalue = (v) |
316 | #else |
330 | #else |
317 | #define rvalue_unchecked(p) (p)->object.ivalue |
331 | #define rvalue_unchecked(p) CELL(p)->object.ivalue |
318 | #define set_rvalue(p,v) (p)->object.ivalue = (v) |
332 | #define set_rvalue(p,v) CELL(p)->object.ivalue = (v) |
319 | #endif |
333 | #endif |
320 | |
334 | |
321 | INTERFACE long |
335 | INTERFACE long |
322 | charvalue (pointer p) |
336 | charvalue (pointer p) |
323 | { |
337 | { |
324 | return ivalue_unchecked (p); |
338 | return ivalue_unchecked (p); |
325 | } |
339 | } |
326 | |
340 | |
|
|
341 | #define port(p) CELL(p)->object.port |
|
|
342 | #define set_port(p,v) port(p) = (v) |
327 | INTERFACE int |
343 | INTERFACE int |
328 | is_port (pointer p) |
344 | is_port (pointer p) |
329 | { |
345 | { |
330 | return type (p) == T_PORT; |
346 | return type (p) == T_PORT; |
331 | } |
347 | } |
332 | |
348 | |
333 | INTERFACE int |
349 | INTERFACE int |
334 | is_inport (pointer p) |
350 | is_inport (pointer p) |
335 | { |
351 | { |
336 | return is_port (p) && p->object.port->kind & port_input; |
352 | return is_port (p) && port (p)->kind & port_input; |
337 | } |
353 | } |
338 | |
354 | |
339 | INTERFACE int |
355 | INTERFACE int |
340 | is_outport (pointer p) |
356 | is_outport (pointer p) |
341 | { |
357 | { |
342 | return is_port (p) && p->object.port->kind & port_output; |
358 | return is_port (p) && port (p)->kind & port_output; |
343 | } |
359 | } |
344 | |
360 | |
345 | INTERFACE int |
361 | INTERFACE int |
346 | is_pair (pointer p) |
362 | is_pair (pointer p) |
347 | { |
363 | { |
348 | return type (p) == T_PAIR; |
364 | return type (p) == T_PAIR; |
349 | } |
365 | } |
350 | |
366 | |
351 | #define car(p) ((p)->object.cons.car + 0) |
367 | #define car(p) (POINTER (CELL(p)->object.cons.car)) |
352 | #define cdr(p) ((p)->object.cons.cdr + 0) |
368 | #define cdr(p) (POINTER (CELL(p)->object.cons.cdr)) |
353 | |
369 | |
354 | static pointer caar (pointer p) { return car (car (p)); } |
370 | static pointer caar (pointer p) { return car (car (p)); } |
355 | static pointer cadr (pointer p) { return car (cdr (p)); } |
371 | static pointer cadr (pointer p) { return car (cdr (p)); } |
356 | static pointer cdar (pointer p) { return cdr (car (p)); } |
372 | static pointer cdar (pointer p) { return cdr (car (p)); } |
357 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
373 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
… | |
… | |
361 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
377 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
362 | |
378 | |
363 | INTERFACE void |
379 | INTERFACE void |
364 | set_car (pointer p, pointer q) |
380 | set_car (pointer p, pointer q) |
365 | { |
381 | { |
366 | p->object.cons.car = q; |
382 | CELL(p)->object.cons.car = CELL (q); |
367 | } |
383 | } |
368 | |
384 | |
369 | INTERFACE void |
385 | INTERFACE void |
370 | set_cdr (pointer p, pointer q) |
386 | set_cdr (pointer p, pointer q) |
371 | { |
387 | { |
372 | p->object.cons.cdr = q; |
388 | CELL(p)->object.cons.cdr = CELL (q); |
373 | } |
389 | } |
374 | |
390 | |
375 | INTERFACE pointer |
391 | INTERFACE pointer |
376 | pair_car (pointer p) |
392 | pair_car (pointer p) |
377 | { |
393 | { |
… | |
… | |
395 | { |
411 | { |
396 | return strvalue (p); |
412 | return strvalue (p); |
397 | } |
413 | } |
398 | |
414 | |
399 | #if USE_PLIST |
415 | #if USE_PLIST |
|
|
416 | #error plists are broken because symbols are no longer pairs |
|
|
417 | #define symprop(p) cdr(p) |
400 | SCHEME_EXPORT int |
418 | SCHEME_EXPORT int |
401 | hasprop (pointer p) |
419 | hasprop (pointer p) |
402 | { |
420 | { |
403 | return typeflag (p) & T_SYMBOL; |
421 | return typeflag (p) & T_SYMBOL; |
404 | } |
422 | } |
405 | |
|
|
406 | # define symprop(p) cdr(p) |
|
|
407 | #endif |
423 | #endif |
408 | |
424 | |
409 | INTERFACE int |
425 | INTERFACE int |
410 | is_syntax (pointer p) |
426 | is_syntax (pointer p) |
411 | { |
427 | { |
… | |
… | |
425 | } |
441 | } |
426 | |
442 | |
427 | INTERFACE char * |
443 | INTERFACE char * |
428 | syntaxname (pointer p) |
444 | syntaxname (pointer p) |
429 | { |
445 | { |
430 | return strvalue (car (p)); |
446 | return strvalue (p); |
431 | } |
447 | } |
432 | |
448 | |
433 | #define procnum(p) ivalue_unchecked (p) |
449 | #define procnum(p) ivalue_unchecked (p) |
434 | static const char *procname (pointer x); |
450 | static const char *procname (pointer x); |
435 | |
451 | |
… | |
… | |
651 | #endif |
667 | #endif |
652 | |
668 | |
653 | static int file_push (SCHEME_P_ const char *fname); |
669 | static int file_push (SCHEME_P_ const char *fname); |
654 | static void file_pop (SCHEME_P); |
670 | static void file_pop (SCHEME_P); |
655 | static int file_interactive (SCHEME_P); |
671 | static int file_interactive (SCHEME_P); |
656 | ecb_inline int is_one_of (char *s, int c); |
672 | ecb_inline int is_one_of (const char *s, int c); |
657 | static int alloc_cellseg (SCHEME_P_ int n); |
673 | static int alloc_cellseg (SCHEME_P); |
658 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
674 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
659 | static void finalize_cell (SCHEME_P_ pointer a); |
675 | static void finalize_cell (SCHEME_P_ pointer a); |
660 | static int count_consecutive_cells (pointer x, int needed); |
676 | static int count_consecutive_cells (pointer x, int needed); |
661 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
677 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
662 | static pointer mk_number (SCHEME_P_ const num n); |
678 | static pointer mk_number (SCHEME_P_ const num n); |
… | |
… | |
678 | static void mark (pointer a); |
694 | static void mark (pointer a); |
679 | static void gc (SCHEME_P_ pointer a, pointer b); |
695 | static void gc (SCHEME_P_ pointer a, pointer b); |
680 | static int basic_inchar (port *pt); |
696 | static int basic_inchar (port *pt); |
681 | static int inchar (SCHEME_P); |
697 | static int inchar (SCHEME_P); |
682 | static void backchar (SCHEME_P_ int c); |
698 | static void backchar (SCHEME_P_ int c); |
683 | static char *readstr_upto (SCHEME_P_ char *delim); |
699 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
684 | static pointer readstrexp (SCHEME_P); |
700 | static pointer readstrexp (SCHEME_P_ char delim); |
685 | ecb_inline int skipspace (SCHEME_P); |
701 | ecb_inline int skipspace (SCHEME_P); |
686 | static int token (SCHEME_P); |
702 | static int token (SCHEME_P); |
687 | static void printslashstring (SCHEME_P_ char *s, int len); |
703 | static void printslashstring (SCHEME_P_ char *s, int len); |
688 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
704 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
689 | static void printatom (SCHEME_P_ pointer l, int f); |
705 | static void printatom (SCHEME_P_ pointer l, int f); |
… | |
… | |
896 | #endif |
912 | #endif |
897 | } |
913 | } |
898 | |
914 | |
899 | /* allocate new cell segment */ |
915 | /* allocate new cell segment */ |
900 | static int |
916 | static int |
901 | alloc_cellseg (SCHEME_P_ int n) |
917 | alloc_cellseg (SCHEME_P) |
902 | { |
918 | { |
903 | pointer newp; |
919 | struct cell *newp; |
904 | pointer last; |
920 | struct cell *last; |
905 | pointer p; |
921 | struct cell *p; |
906 | char *cp; |
922 | char *cp; |
907 | long i; |
923 | long i; |
908 | int k; |
924 | int k; |
909 | |
925 | |
910 | static int segsize = CELL_SEGSIZE >> 1; |
926 | static int segsize = CELL_SEGSIZE >> 1; |
911 | segsize <<= 1; |
927 | segsize <<= 1; |
912 | |
928 | |
913 | for (k = 0; k < n; k++) |
|
|
914 | { |
|
|
915 | if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1) |
|
|
916 | return k; |
|
|
917 | |
|
|
918 | cp = malloc (segsize * sizeof (struct cell)); |
929 | cp = malloc (segsize * sizeof (struct cell)); |
919 | |
930 | |
920 | if (!cp && USE_ERROR_CHECKING) |
931 | if (!cp && USE_ERROR_CHECKING) |
921 | return k; |
932 | return k; |
922 | |
933 | |
923 | i = ++SCHEME_V->last_cell_seg; |
934 | i = ++SCHEME_V->last_cell_seg; |
924 | SCHEME_V->alloc_seg[i] = cp; |
935 | SCHEME_V->alloc_seg[i] = cp; |
925 | |
936 | |
926 | newp = (pointer)cp; |
937 | newp = (struct cell *)cp; |
927 | SCHEME_V->cell_seg[i] = newp; |
938 | SCHEME_V->cell_seg[i] = newp; |
928 | SCHEME_V->cell_segsize[i] = segsize; |
939 | SCHEME_V->cell_segsize[i] = segsize; |
929 | SCHEME_V->fcells += segsize; |
940 | SCHEME_V->fcells += segsize; |
930 | last = newp + segsize - 1; |
941 | last = newp + segsize - 1; |
931 | |
942 | |
932 | for (p = newp; p <= last; p++) |
943 | for (p = newp; p <= last; p++) |
933 | { |
944 | { |
|
|
945 | pointer cp = POINTER (p); |
934 | set_typeflag (p, T_PAIR); |
946 | set_typeflag (cp, T_PAIR); |
935 | set_car (p, NIL); |
947 | set_car (cp, NIL); |
936 | set_cdr (p, p + 1); |
948 | set_cdr (cp, POINTER (p + 1)); |
937 | } |
949 | } |
938 | |
950 | |
939 | set_cdr (last, SCHEME_V->free_cell); |
951 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
940 | SCHEME_V->free_cell = newp; |
952 | SCHEME_V->free_cell = POINTER (newp); |
941 | } |
|
|
942 | |
953 | |
943 | return n; |
954 | return 1; |
944 | } |
955 | } |
945 | |
956 | |
946 | /* get new cell. parameter a, b is marked by gc. */ |
957 | /* get new cell. parameter a, b is marked by gc. */ |
947 | ecb_inline pointer |
958 | ecb_inline pointer |
948 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
959 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
… | |
… | |
952 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
963 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
953 | return S_SINK; |
964 | return S_SINK; |
954 | |
965 | |
955 | if (SCHEME_V->free_cell == NIL) |
966 | if (SCHEME_V->free_cell == NIL) |
956 | { |
967 | { |
957 | const int min_to_be_recovered = SCHEME_V->last_cell_seg < 128 ? 128 * 8 : SCHEME_V->last_cell_seg * 8; |
968 | const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1; |
958 | |
969 | |
959 | gc (SCHEME_A_ a, b); |
970 | gc (SCHEME_A_ a, b); |
960 | |
971 | |
961 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
972 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
962 | { |
973 | { |
963 | /* if only a few recovered, get more to avoid fruitless gc's */ |
974 | /* if only a few recovered, get more to avoid fruitless gc's */ |
964 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
975 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
965 | { |
976 | { |
966 | #if USE_ERROR_CHECKING |
977 | #if USE_ERROR_CHECKING |
967 | SCHEME_V->no_memory = 1; |
978 | SCHEME_V->no_memory = 1; |
968 | return S_SINK; |
979 | return S_SINK; |
969 | #endif |
980 | #endif |
… | |
… | |
981 | } |
992 | } |
982 | } |
993 | } |
983 | |
994 | |
984 | /* To retain recent allocs before interpreter knows about them - |
995 | /* To retain recent allocs before interpreter knows about them - |
985 | Tehom */ |
996 | Tehom */ |
986 | |
|
|
987 | static void |
997 | static void |
988 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
998 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
989 | { |
999 | { |
990 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
1000 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
991 | |
1001 | |
… | |
… | |
1013 | } |
1023 | } |
1014 | |
1024 | |
1015 | static pointer |
1025 | static pointer |
1016 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1026 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1017 | { |
1027 | { |
1018 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
1028 | pointer v = get_cell_x (SCHEME_A_ NIL, NIL); |
1019 | pointer *e = malloc (len * sizeof (pointer)); |
1029 | pointer *e = malloc (len * sizeof (pointer)); |
1020 | |
1030 | |
1021 | if (!e && USE_ERROR_CHECKING) |
1031 | if (!e && USE_ERROR_CHECKING) |
1022 | return S_SINK; |
1032 | return S_SINK; |
1023 | |
1033 | |
1024 | /* Record it as a vector so that gc understands it. */ |
1034 | /* Record it as a vector so that gc understands it. */ |
1025 | set_typeflag (v, T_VECTOR | T_ATOM); |
1035 | set_typeflag (v, T_VECTOR | T_ATOM); |
1026 | |
1036 | |
1027 | v->object.vector.vvalue = e; |
1037 | CELL(v)->object.vector.vvalue = e; |
1028 | v->object.vector.length = len; |
1038 | CELL(v)->object.vector.length = len; |
1029 | fill_vector (v, 0, init); |
1039 | fill_vector (v, 0, init); |
1030 | push_recent_alloc (SCHEME_A_ v, NIL); |
1040 | push_recent_alloc (SCHEME_A_ v, NIL); |
1031 | |
1041 | |
1032 | return v; |
1042 | return v; |
1033 | } |
1043 | } |
… | |
… | |
1042 | static void |
1052 | static void |
1043 | check_cell_alloced (pointer p, int expect_alloced) |
1053 | check_cell_alloced (pointer p, int expect_alloced) |
1044 | { |
1054 | { |
1045 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1055 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1046 | if (typeflag (p) & !expect_alloced) |
1056 | if (typeflag (p) & !expect_alloced) |
1047 | xwrstr ("Cell is already allocated!\n"); |
1057 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1048 | |
1058 | |
1049 | if (!(typeflag (p)) & expect_alloced) |
1059 | if (!(typeflag (p)) & expect_alloced) |
1050 | xwrstr ("Cell is not allocated!\n"); |
1060 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1051 | } |
1061 | } |
1052 | |
1062 | |
1053 | static void |
1063 | static void |
1054 | check_range_alloced (pointer p, int n, int expect_alloced) |
1064 | check_range_alloced (pointer p, int n, int expect_alloced) |
1055 | { |
1065 | { |
… | |
… | |
1077 | set_cdr (x, b); |
1087 | set_cdr (x, b); |
1078 | |
1088 | |
1079 | return x; |
1089 | return x; |
1080 | } |
1090 | } |
1081 | |
1091 | |
|
|
1092 | static pointer |
|
|
1093 | generate_symbol (SCHEME_P_ const char *name) |
|
|
1094 | { |
|
|
1095 | pointer x = mk_string (SCHEME_A_ name); |
|
|
1096 | setimmutable (x); |
|
|
1097 | set_typeflag (x, T_SYMBOL | T_ATOM); |
|
|
1098 | return x; |
|
|
1099 | } |
|
|
1100 | |
1082 | /* ========== oblist implementation ========== */ |
1101 | /* ========== oblist implementation ========== */ |
1083 | |
1102 | |
1084 | #ifndef USE_OBJECT_LIST |
1103 | #ifndef USE_OBJECT_LIST |
1085 | |
1104 | |
|
|
1105 | static int |
1086 | static int hash_fn (const char *key, int table_size); |
1106 | hash_fn (const char *key, int table_size) |
|
|
1107 | { |
|
|
1108 | const unsigned char *p = key; |
|
|
1109 | uint32_t hash = 2166136261; |
|
|
1110 | |
|
|
1111 | while (*p) |
|
|
1112 | hash = (hash ^ *p++) * 16777619; |
|
|
1113 | |
|
|
1114 | return hash % table_size; |
|
|
1115 | } |
1087 | |
1116 | |
1088 | static pointer |
1117 | static pointer |
1089 | oblist_initial_value (SCHEME_P) |
1118 | oblist_initial_value (SCHEME_P) |
1090 | { |
1119 | { |
1091 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1120 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
… | |
… | |
1093 | |
1122 | |
1094 | /* returns the new symbol */ |
1123 | /* returns the new symbol */ |
1095 | static pointer |
1124 | static pointer |
1096 | oblist_add_by_name (SCHEME_P_ const char *name) |
1125 | oblist_add_by_name (SCHEME_P_ const char *name) |
1097 | { |
1126 | { |
1098 | int location; |
|
|
1099 | |
|
|
1100 | pointer x = mk_string (SCHEME_A_ name); |
1127 | pointer x = generate_symbol (SCHEME_A_ name); |
1101 | set_typeflag (x, T_SYMBOL); |
|
|
1102 | setimmutable (car (x)); |
|
|
1103 | |
|
|
1104 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1128 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1105 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1129 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1106 | return x; |
1130 | return x; |
1107 | } |
1131 | } |
1108 | |
1132 | |
1109 | ecb_inline pointer |
1133 | ecb_inline pointer |
… | |
… | |
1169 | |
1193 | |
1170 | /* returns the new symbol */ |
1194 | /* returns the new symbol */ |
1171 | static pointer |
1195 | static pointer |
1172 | oblist_add_by_name (SCHEME_P_ const char *name) |
1196 | oblist_add_by_name (SCHEME_P_ const char *name) |
1173 | { |
1197 | { |
1174 | pointer x; |
1198 | pointer x = generate_symbol (SCHEME_A_ name); |
1175 | |
|
|
1176 | x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1177 | set_typeflag (x, T_SYMBOL); |
|
|
1178 | setimmutable (car (x)); |
|
|
1179 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1199 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1180 | return x; |
1200 | return x; |
1181 | } |
1201 | } |
1182 | |
1202 | |
1183 | static pointer |
1203 | static pointer |
… | |
… | |
1193 | mk_port (SCHEME_P_ port *p) |
1213 | mk_port (SCHEME_P_ port *p) |
1194 | { |
1214 | { |
1195 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1215 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1196 | |
1216 | |
1197 | set_typeflag (x, T_PORT | T_ATOM); |
1217 | set_typeflag (x, T_PORT | T_ATOM); |
1198 | x->object.port = p; |
1218 | set_port (x, p); |
1199 | |
1219 | |
1200 | return x; |
1220 | return x; |
1201 | } |
1221 | } |
1202 | #endif |
1222 | #endif |
1203 | |
1223 | |
1204 | pointer |
1224 | pointer |
1205 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1225 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1206 | { |
1226 | { |
1207 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1227 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1208 | |
1228 | |
1209 | set_typeflag (x, (T_FOREIGN | T_ATOM)); |
1229 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1210 | x->object.ff = f; |
1230 | CELL(x)->object.ff = f; |
1211 | |
1231 | |
1212 | return x; |
1232 | return x; |
1213 | } |
1233 | } |
1214 | |
1234 | |
1215 | INTERFACE pointer |
1235 | INTERFACE pointer |
1216 | mk_character (SCHEME_P_ int c) |
1236 | mk_character (SCHEME_P_ int c) |
1217 | { |
1237 | { |
1218 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1238 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1219 | |
1239 | |
1220 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1240 | set_typeflag (x, T_CHARACTER | T_ATOM); |
1221 | set_ivalue (x, c & 0xff); |
1241 | set_ivalue (x, c & 0xff); |
1222 | |
1242 | |
1223 | return x; |
1243 | return x; |
1224 | } |
1244 | } |
1225 | |
1245 | |
1226 | /* get number atom (integer) */ |
1246 | /* get number atom (integer) */ |
1227 | INTERFACE pointer |
1247 | INTERFACE pointer |
1228 | mk_integer (SCHEME_P_ long n) |
1248 | mk_integer (SCHEME_P_ long n) |
1229 | { |
1249 | { |
|
|
1250 | pointer p = 0; |
|
|
1251 | pointer *pp = &p; |
|
|
1252 | |
|
|
1253 | #if USE_INTCACHE |
|
|
1254 | if (n >= INTCACHE_MIN && n <= INTCACHE_MAX) |
|
|
1255 | pp = &SCHEME_V->intcache[n - INTCACHE_MIN]; |
|
|
1256 | #endif |
|
|
1257 | |
|
|
1258 | if (!*pp) |
|
|
1259 | { |
1230 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1260 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1231 | |
1261 | |
1232 | set_typeflag (x, (T_INTEGER | T_ATOM)); |
1262 | set_typeflag (x, T_INTEGER | T_ATOM); |
|
|
1263 | setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */ |
1233 | set_ivalue (x, n); |
1264 | set_ivalue (x, n); |
1234 | |
1265 | |
|
|
1266 | *pp = x; |
|
|
1267 | } |
|
|
1268 | |
1235 | return x; |
1269 | return *pp; |
1236 | } |
1270 | } |
1237 | |
1271 | |
1238 | INTERFACE pointer |
1272 | INTERFACE pointer |
1239 | mk_real (SCHEME_P_ RVALUE n) |
1273 | mk_real (SCHEME_P_ RVALUE n) |
1240 | { |
1274 | { |
1241 | #if USE_REAL |
1275 | #if USE_REAL |
1242 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1276 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1243 | |
1277 | |
1244 | set_typeflag (x, (T_REAL | T_ATOM)); |
1278 | set_typeflag (x, T_REAL | T_ATOM); |
1245 | set_rvalue (x, n); |
1279 | set_rvalue (x, n); |
1246 | |
1280 | |
1247 | return x; |
1281 | return x; |
1248 | #else |
1282 | #else |
1249 | return mk_integer (SCHEME_A_ n); |
1283 | return mk_integer (SCHEME_A_ n); |
… | |
… | |
1273 | SCHEME_V->no_memory = 1; |
1307 | SCHEME_V->no_memory = 1; |
1274 | return SCHEME_V->strbuff; |
1308 | return SCHEME_V->strbuff; |
1275 | } |
1309 | } |
1276 | |
1310 | |
1277 | if (str) |
1311 | if (str) |
1278 | { |
1312 | memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */ |
1279 | int l = strlen (str); |
|
|
1280 | |
|
|
1281 | if (l > len_str) |
|
|
1282 | l = len_str; |
|
|
1283 | |
|
|
1284 | memcpy (q, str, l); |
|
|
1285 | q[l] = 0; |
|
|
1286 | } |
|
|
1287 | else |
1313 | else |
1288 | { |
|
|
1289 | memset (q, fill, len_str); |
1314 | memset (q, fill, len_str); |
|
|
1315 | |
1290 | q[len_str] = 0; |
1316 | q[len_str] = 0; |
1291 | } |
|
|
1292 | |
1317 | |
1293 | return q; |
1318 | return q; |
1294 | } |
1319 | } |
1295 | |
1320 | |
1296 | INTERFACE pointer |
1321 | INTERFACE pointer |
… | |
… | |
1310 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1335 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1311 | |
1336 | |
1312 | set_typeflag (x, T_STRING | T_ATOM); |
1337 | set_typeflag (x, T_STRING | T_ATOM); |
1313 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1338 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1314 | strlength (x) = len; |
1339 | strlength (x) = len; |
|
|
1340 | |
1315 | return x; |
1341 | return x; |
1316 | } |
1342 | } |
1317 | |
1343 | |
1318 | INTERFACE pointer |
1344 | INTERFACE pointer |
1319 | mk_string (SCHEME_P_ const char *str) |
1345 | mk_string (SCHEME_P_ const char *str) |
… | |
… | |
1332 | { |
1358 | { |
1333 | int i; |
1359 | int i; |
1334 | |
1360 | |
1335 | for (i = start; i < veclength (vec); i++) |
1361 | for (i = start; i < veclength (vec); i++) |
1336 | vecvalue (vec)[i] = obj; |
1362 | vecvalue (vec)[i] = obj; |
|
|
1363 | } |
|
|
1364 | |
|
|
1365 | INTERFACE void |
|
|
1366 | vector_resize (pointer vec, uint32_t newsize, pointer fill) |
|
|
1367 | { |
|
|
1368 | uint32_t oldsize = veclength (vec); |
|
|
1369 | vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer)); |
|
|
1370 | veclength (vec) = newsize; |
|
|
1371 | fill_vector (vec, oldsize, fill); |
1337 | } |
1372 | } |
1338 | |
1373 | |
1339 | INTERFACE pointer |
1374 | INTERFACE pointer |
1340 | vector_get (pointer vec, uint32_t ielem) |
1375 | vector_get (pointer vec, uint32_t ielem) |
1341 | { |
1376 | { |
… | |
… | |
1363 | |
1398 | |
1364 | INTERFACE pointer |
1399 | INTERFACE pointer |
1365 | gensym (SCHEME_P) |
1400 | gensym (SCHEME_P) |
1366 | { |
1401 | { |
1367 | pointer x; |
1402 | pointer x; |
1368 | |
|
|
1369 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
|
|
1370 | { |
|
|
1371 | char name[40] = "gensym-"; |
1403 | char name[40] = "gensym-"; |
1372 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1404 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1373 | |
1405 | |
1374 | /* first check oblist */ |
1406 | return generate_symbol (SCHEME_A_ name); |
1375 | x = oblist_find_by_name (SCHEME_A_ name); |
1407 | } |
1376 | |
1408 | |
1377 | if (x == NIL) |
1409 | static int |
1378 | { |
1410 | is_gensym (SCHEME_P_ pointer x) |
1379 | x = oblist_add_by_name (SCHEME_A_ name); |
1411 | { |
1380 | return x; |
1412 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1381 | } |
|
|
1382 | } |
|
|
1383 | |
|
|
1384 | return NIL; |
|
|
1385 | } |
1413 | } |
1386 | |
1414 | |
1387 | /* make symbol or number atom from string */ |
1415 | /* make symbol or number atom from string */ |
1388 | static pointer |
1416 | static pointer |
1389 | mk_atom (SCHEME_P_ char *q) |
1417 | mk_atom (SCHEME_P_ char *q) |
… | |
… | |
1598 | |
1626 | |
1599 | /* garbage collection. parameter a, b is marked. */ |
1627 | /* garbage collection. parameter a, b is marked. */ |
1600 | static void |
1628 | static void |
1601 | gc (SCHEME_P_ pointer a, pointer b) |
1629 | gc (SCHEME_P_ pointer a, pointer b) |
1602 | { |
1630 | { |
1603 | pointer p; |
|
|
1604 | int i; |
1631 | int i; |
1605 | |
1632 | |
1606 | if (SCHEME_V->gc_verbose) |
1633 | if (SCHEME_V->gc_verbose) |
1607 | putstr (SCHEME_A_ "gc..."); |
1634 | putstr (SCHEME_A_ "gc..."); |
1608 | |
1635 | |
… | |
… | |
1624 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1651 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1625 | mark (car (S_SINK)); |
1652 | mark (car (S_SINK)); |
1626 | /* Mark any older stuff above nested C calls */ |
1653 | /* Mark any older stuff above nested C calls */ |
1627 | mark (SCHEME_V->c_nest); |
1654 | mark (SCHEME_V->c_nest); |
1628 | |
1655 | |
|
|
1656 | #if USE_INTCACHE |
|
|
1657 | /* mark intcache */ |
|
|
1658 | for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i) |
|
|
1659 | if (SCHEME_V->intcache[i - INTCACHE_MIN]) |
|
|
1660 | mark (SCHEME_V->intcache[i - INTCACHE_MIN]); |
|
|
1661 | #endif |
|
|
1662 | |
1629 | /* mark variables a, b */ |
1663 | /* mark variables a, b */ |
1630 | mark (a); |
1664 | mark (a); |
1631 | mark (b); |
1665 | mark (b); |
1632 | |
1666 | |
1633 | /* garbage collect */ |
1667 | /* garbage collect */ |
1634 | clrmark (NIL); |
1668 | clrmark (NIL); |
1635 | SCHEME_V->fcells = 0; |
1669 | SCHEME_V->fcells = 0; |
1636 | SCHEME_V->free_cell = NIL; |
1670 | SCHEME_V->free_cell = NIL; |
1637 | |
1671 | |
1638 | /* free-list is kept sorted by address so as to maintain consecutive |
1672 | if (SCHEME_V->gc_verbose) |
1639 | ranges, if possible, for use with vectors. Here we scan the cells |
1673 | putstr (SCHEME_A_ "freeing..."); |
1640 | (which are also kept sorted by address) downwards to build the |
1674 | |
1641 | free-list in sorted order. |
1675 | uint32_t total = 0; |
1642 | */ |
1676 | |
|
|
1677 | /* Here we scan the cells to build the free-list. */ |
1643 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1678 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1644 | { |
1679 | { |
1645 | p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
1680 | struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1681 | struct cell *p; |
|
|
1682 | total += SCHEME_V->cell_segsize [i]; |
1646 | |
1683 | |
1647 | while (--p >= SCHEME_V->cell_seg[i]) |
1684 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
1648 | { |
1685 | { |
|
|
1686 | pointer c = POINTER (p); |
|
|
1687 | |
1649 | if (is_mark (p)) |
1688 | if (is_mark (c)) |
1650 | clrmark (p); |
1689 | clrmark (c); |
1651 | else |
1690 | else |
1652 | { |
1691 | { |
1653 | /* reclaim cell */ |
1692 | /* reclaim cell */ |
1654 | if (typeflag (p) != T_PAIR) |
1693 | if (typeflag (c) != T_PAIR) |
1655 | { |
1694 | { |
1656 | finalize_cell (SCHEME_A_ p); |
1695 | finalize_cell (SCHEME_A_ c); |
1657 | set_typeflag (p, T_PAIR); |
1696 | set_typeflag (c, T_PAIR); |
1658 | set_car (p, NIL); |
1697 | set_car (c, NIL); |
1659 | } |
1698 | } |
1660 | |
1699 | |
1661 | ++SCHEME_V->fcells; |
1700 | ++SCHEME_V->fcells; |
1662 | set_cdr (p, SCHEME_V->free_cell); |
1701 | set_cdr (c, SCHEME_V->free_cell); |
1663 | SCHEME_V->free_cell = p; |
1702 | SCHEME_V->free_cell = c; |
1664 | } |
1703 | } |
1665 | } |
1704 | } |
1666 | } |
1705 | } |
1667 | |
1706 | |
1668 | if (SCHEME_V->gc_verbose) |
1707 | if (SCHEME_V->gc_verbose) |
1669 | { |
1708 | { |
1670 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); |
1709 | 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"); |
1671 | } |
1710 | } |
1672 | } |
1711 | } |
1673 | |
1712 | |
1674 | static void |
1713 | static void |
1675 | finalize_cell (SCHEME_P_ pointer a) |
1714 | finalize_cell (SCHEME_P_ pointer a) |
1676 | { |
1715 | { |
1677 | /* TODO, fast bitmap check? */ |
1716 | /* TODO, fast bitmap check? */ |
1678 | if (is_string (a)) |
1717 | if (is_string (a) || is_symbol (a)) |
1679 | free (strvalue (a)); |
1718 | free (strvalue (a)); |
1680 | else if (is_vector (a)) |
1719 | else if (is_vector (a)) |
1681 | free (vecvalue (a)); |
1720 | free (vecvalue (a)); |
1682 | #if USE_PORTS |
1721 | #if USE_PORTS |
1683 | else if (is_port (a)) |
1722 | else if (is_port (a)) |
1684 | { |
1723 | { |
1685 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
1724 | if (port(a)->kind & port_file && port (a)->rep.stdio.closeit) |
1686 | port_close (SCHEME_A_ a, port_input | port_output); |
1725 | port_close (SCHEME_A_ a, port_input | port_output); |
1687 | |
1726 | |
1688 | free (a->object.port); |
1727 | free (port (a)); |
1689 | } |
1728 | } |
1690 | #endif |
1729 | #endif |
1691 | } |
1730 | } |
1692 | |
1731 | |
1693 | /* ========== Routines for Reading ========== */ |
1732 | /* ========== Routines for Reading ========== */ |
… | |
… | |
1709 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1748 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1710 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1749 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1711 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; |
1750 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; |
1712 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1751 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1713 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1752 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1714 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1753 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1715 | |
1754 | |
1716 | #if SHOW_ERROR_LINE |
1755 | #if SHOW_ERROR_LINE |
1717 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1756 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1718 | |
1757 | |
1719 | if (fname) |
1758 | if (fname) |
… | |
… | |
1736 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1775 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1737 | #if USE_PORTS |
1776 | #if USE_PORTS |
1738 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1777 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1739 | #endif |
1778 | #endif |
1740 | SCHEME_V->file_i--; |
1779 | SCHEME_V->file_i--; |
1741 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1780 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1742 | } |
1781 | } |
1743 | } |
1782 | } |
1744 | |
1783 | |
1745 | static int |
1784 | static int |
1746 | file_interactive (SCHEME_P) |
1785 | file_interactive (SCHEME_P) |
1747 | { |
1786 | { |
1748 | #if USE_PORTS |
1787 | #if USE_PORTS |
1749 | return SCHEME_V->file_i == 0 |
1788 | return SCHEME_V->file_i == 0 |
1750 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1789 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1751 | && (SCHEME_V->inport->object.port->kind & port_file); |
1790 | && (port (SCHEME_V->inport)->kind & port_file); |
1752 | #else |
1791 | #else |
1753 | return 0; |
1792 | return 0; |
1754 | #endif |
1793 | #endif |
1755 | } |
1794 | } |
1756 | |
1795 | |
… | |
… | |
1890 | } |
1929 | } |
1891 | |
1930 | |
1892 | static void |
1931 | static void |
1893 | port_close (SCHEME_P_ pointer p, int flag) |
1932 | port_close (SCHEME_P_ pointer p, int flag) |
1894 | { |
1933 | { |
1895 | port *pt = p->object.port; |
1934 | port *pt = port (p); |
1896 | |
1935 | |
1897 | pt->kind &= ~flag; |
1936 | pt->kind &= ~flag; |
1898 | |
1937 | |
1899 | if ((pt->kind & (port_input | port_output)) == 0) |
1938 | if ((pt->kind & (port_input | port_output)) == 0) |
1900 | { |
1939 | { |
… | |
… | |
1921 | /* get new character from input file */ |
1960 | /* get new character from input file */ |
1922 | static int |
1961 | static int |
1923 | inchar (SCHEME_P) |
1962 | inchar (SCHEME_P) |
1924 | { |
1963 | { |
1925 | int c; |
1964 | int c; |
1926 | port *pt; |
1965 | port *pt = port (SCHEME_V->inport); |
1927 | |
|
|
1928 | pt = SCHEME_V->inport->object.port; |
|
|
1929 | |
1966 | |
1930 | if (pt->kind & port_saw_EOF) |
1967 | if (pt->kind & port_saw_EOF) |
1931 | return EOF; |
1968 | return EOF; |
1932 | |
1969 | |
1933 | c = basic_inchar (pt); |
1970 | c = basic_inchar (pt); |
… | |
… | |
2000 | port *pt; |
2037 | port *pt; |
2001 | |
2038 | |
2002 | if (c == EOF) |
2039 | if (c == EOF) |
2003 | return; |
2040 | return; |
2004 | |
2041 | |
2005 | pt = SCHEME_V->inport->object.port; |
2042 | pt = port (SCHEME_V->inport); |
2006 | pt->unget = c; |
2043 | pt->unget = c; |
2007 | #else |
2044 | #else |
2008 | if (c == EOF) |
2045 | if (c == EOF) |
2009 | return; |
2046 | return; |
2010 | |
2047 | |
… | |
… | |
2038 | |
2075 | |
2039 | INTERFACE void |
2076 | INTERFACE void |
2040 | putstr (SCHEME_P_ const char *s) |
2077 | putstr (SCHEME_P_ const char *s) |
2041 | { |
2078 | { |
2042 | #if USE_PORTS |
2079 | #if USE_PORTS |
2043 | port *pt = SCHEME_V->outport->object.port; |
2080 | port *pt = port (SCHEME_V->outport); |
2044 | |
2081 | |
2045 | if (pt->kind & port_file) |
2082 | if (pt->kind & port_file) |
2046 | write (pt->rep.stdio.file, s, strlen (s)); |
2083 | write (pt->rep.stdio.file, s, strlen (s)); |
2047 | else |
2084 | else |
2048 | for (; *s; s++) |
2085 | for (; *s; s++) |
… | |
… | |
2050 | *pt->rep.string.curr++ = *s; |
2087 | *pt->rep.string.curr++ = *s; |
2051 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
2088 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
2052 | *pt->rep.string.curr++ = *s; |
2089 | *pt->rep.string.curr++ = *s; |
2053 | |
2090 | |
2054 | #else |
2091 | #else |
2055 | xwrstr (s); |
2092 | write (pt->rep.stdio.file, s, strlen (s)); |
2056 | #endif |
2093 | #endif |
2057 | } |
2094 | } |
2058 | |
2095 | |
2059 | static void |
2096 | static void |
2060 | putchars (SCHEME_P_ const char *s, int len) |
2097 | putchars (SCHEME_P_ const char *s, int len) |
2061 | { |
2098 | { |
2062 | #if USE_PORTS |
2099 | #if USE_PORTS |
2063 | port *pt = SCHEME_V->outport->object.port; |
2100 | port *pt = port (SCHEME_V->outport); |
2064 | |
2101 | |
2065 | if (pt->kind & port_file) |
2102 | if (pt->kind & port_file) |
2066 | write (pt->rep.stdio.file, s, len); |
2103 | write (pt->rep.stdio.file, s, len); |
2067 | else |
2104 | else |
2068 | { |
2105 | { |
… | |
… | |
2082 | |
2119 | |
2083 | INTERFACE void |
2120 | INTERFACE void |
2084 | putcharacter (SCHEME_P_ int c) |
2121 | putcharacter (SCHEME_P_ int c) |
2085 | { |
2122 | { |
2086 | #if USE_PORTS |
2123 | #if USE_PORTS |
2087 | port *pt = SCHEME_V->outport->object.port; |
2124 | port *pt = port (SCHEME_V->outport); |
2088 | |
2125 | |
2089 | if (pt->kind & port_file) |
2126 | if (pt->kind & port_file) |
2090 | { |
2127 | { |
2091 | char cc = c; |
2128 | char cc = c; |
2092 | write (pt->rep.stdio.file, &cc, 1); |
2129 | write (pt->rep.stdio.file, &cc, 1); |
… | |
… | |
2105 | #endif |
2142 | #endif |
2106 | } |
2143 | } |
2107 | |
2144 | |
2108 | /* read characters up to delimiter, but cater to character constants */ |
2145 | /* read characters up to delimiter, but cater to character constants */ |
2109 | static char * |
2146 | static char * |
2110 | readstr_upto (SCHEME_P_ char *delim) |
2147 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2111 | { |
2148 | { |
2112 | char *p = SCHEME_V->strbuff; |
2149 | char *p = SCHEME_V->strbuff + skip; |
2113 | |
2150 | |
2114 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2151 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2115 | |
2152 | |
2116 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2153 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2117 | *p = 0; |
2154 | *p = 0; |
… | |
… | |
2124 | return SCHEME_V->strbuff; |
2161 | return SCHEME_V->strbuff; |
2125 | } |
2162 | } |
2126 | |
2163 | |
2127 | /* read string expression "xxx...xxx" */ |
2164 | /* read string expression "xxx...xxx" */ |
2128 | static pointer |
2165 | static pointer |
2129 | readstrexp (SCHEME_P) |
2166 | readstrexp (SCHEME_P_ char delim) |
2130 | { |
2167 | { |
2131 | char *p = SCHEME_V->strbuff; |
2168 | char *p = SCHEME_V->strbuff; |
2132 | int c; |
2169 | int c; |
2133 | int c1 = 0; |
2170 | int c1 = 0; |
2134 | enum |
|
|
2135 | { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2171 | enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2136 | |
2172 | |
2137 | for (;;) |
2173 | for (;;) |
2138 | { |
2174 | { |
2139 | c = inchar (SCHEME_A); |
2175 | c = inchar (SCHEME_A); |
2140 | |
2176 | |
… | |
… | |
2142 | return S_F; |
2178 | return S_F; |
2143 | |
2179 | |
2144 | switch (state) |
2180 | switch (state) |
2145 | { |
2181 | { |
2146 | case st_ok: |
2182 | case st_ok: |
2147 | switch (c) |
2183 | if (ecb_expect_false (c == delim)) |
2148 | { |
|
|
2149 | case '\\': |
|
|
2150 | state = st_bsl; |
|
|
2151 | break; |
|
|
2152 | |
|
|
2153 | case '"': |
|
|
2154 | *p = 0; |
|
|
2155 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2184 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2156 | |
2185 | |
2157 | default: |
2186 | if (ecb_expect_false (c == '\\')) |
|
|
2187 | state = st_bsl; |
|
|
2188 | else |
2158 | *p++ = c; |
2189 | *p++ = c; |
2159 | break; |
|
|
2160 | } |
|
|
2161 | |
2190 | |
2162 | break; |
2191 | break; |
2163 | |
2192 | |
2164 | case st_bsl: |
2193 | case st_bsl: |
2165 | switch (c) |
2194 | switch (c) |
… | |
… | |
2195 | case 'r': |
2224 | case 'r': |
2196 | *p++ = '\r'; |
2225 | *p++ = '\r'; |
2197 | state = st_ok; |
2226 | state = st_ok; |
2198 | break; |
2227 | break; |
2199 | |
2228 | |
2200 | case '"': |
|
|
2201 | *p++ = '"'; |
|
|
2202 | state = st_ok; |
|
|
2203 | break; |
|
|
2204 | |
|
|
2205 | default: |
2229 | default: |
2206 | *p++ = c; |
2230 | *p++ = c; |
2207 | state = st_ok; |
2231 | state = st_ok; |
2208 | break; |
2232 | break; |
2209 | } |
2233 | } |
2210 | |
2234 | |
2211 | break; |
2235 | break; |
2212 | |
2236 | |
2213 | case st_x1: |
2237 | case st_x1: |
2214 | case st_x2: |
2238 | case st_x2: |
2215 | c = toupper (c); |
2239 | c = tolower (c); |
2216 | |
2240 | |
2217 | if (c >= '0' && c <= 'F') |
2241 | if (c >= '0' && c <= '9') |
2218 | { |
|
|
2219 | if (c <= '9') |
|
|
2220 | c1 = (c1 << 4) + c - '0'; |
2242 | c1 = (c1 << 4) + c - '0'; |
2221 | else |
2243 | else if (c >= 'a' && c <= 'f') |
2222 | c1 = (c1 << 4) + c - 'A' + 10; |
2244 | c1 = (c1 << 4) + c - 'a' + 10; |
2223 | |
|
|
2224 | if (state == st_x1) |
|
|
2225 | state = st_x2; |
|
|
2226 | else |
|
|
2227 | { |
|
|
2228 | *p++ = c1; |
|
|
2229 | state = st_ok; |
|
|
2230 | } |
|
|
2231 | } |
|
|
2232 | else |
2245 | else |
2233 | return S_F; |
2246 | return S_F; |
|
|
2247 | |
|
|
2248 | if (state == st_x1) |
|
|
2249 | state = st_x2; |
|
|
2250 | else |
|
|
2251 | { |
|
|
2252 | *p++ = c1; |
|
|
2253 | state = st_ok; |
|
|
2254 | } |
2234 | |
2255 | |
2235 | break; |
2256 | break; |
2236 | |
2257 | |
2237 | case st_oct1: |
2258 | case st_oct1: |
2238 | case st_oct2: |
2259 | case st_oct2: |
… | |
… | |
2242 | backchar (SCHEME_A_ c); |
2263 | backchar (SCHEME_A_ c); |
2243 | state = st_ok; |
2264 | state = st_ok; |
2244 | } |
2265 | } |
2245 | else |
2266 | else |
2246 | { |
2267 | { |
2247 | if (state == st_oct2 && c1 >= 32) |
2268 | if (state == st_oct2 && c1 >= ' ') |
2248 | return S_F; |
2269 | return S_F; |
2249 | |
2270 | |
2250 | c1 = (c1 << 3) + (c - '0'); |
2271 | c1 = (c1 << 3) + (c - '0'); |
2251 | |
2272 | |
2252 | if (state == st_oct1) |
2273 | if (state == st_oct1) |
… | |
… | |
2257 | state = st_ok; |
2278 | state = st_ok; |
2258 | } |
2279 | } |
2259 | } |
2280 | } |
2260 | |
2281 | |
2261 | break; |
2282 | break; |
2262 | |
|
|
2263 | } |
2283 | } |
2264 | } |
2284 | } |
2265 | } |
2285 | } |
2266 | |
2286 | |
2267 | /* check c is in chars */ |
2287 | /* check c is in chars */ |
2268 | ecb_inline int |
2288 | ecb_inline int |
2269 | is_one_of (char *s, int c) |
2289 | is_one_of (const char *s, int c) |
2270 | { |
2290 | { |
2271 | if (c == EOF) |
|
|
2272 | return 1; |
|
|
2273 | |
|
|
2274 | return !!strchr (s, c); |
2291 | return c == EOF || !!strchr (s, c); |
2275 | } |
2292 | } |
2276 | |
2293 | |
2277 | /* skip white characters */ |
2294 | /* skip white characters */ |
2278 | ecb_inline int |
2295 | ecb_inline int |
2279 | skipspace (SCHEME_P) |
2296 | skipspace (SCHEME_P) |
… | |
… | |
2281 | int c, curr_line = 0; |
2298 | int c, curr_line = 0; |
2282 | |
2299 | |
2283 | do |
2300 | do |
2284 | { |
2301 | { |
2285 | c = inchar (SCHEME_A); |
2302 | c = inchar (SCHEME_A); |
|
|
2303 | |
2286 | #if SHOW_ERROR_LINE |
2304 | #if SHOW_ERROR_LINE |
2287 | if (c == '\n') |
2305 | if (ecb_expect_false (c == '\n')) |
2288 | curr_line++; |
2306 | curr_line++; |
2289 | #endif |
2307 | #endif |
|
|
2308 | |
|
|
2309 | if (ecb_expect_false (c == EOF)) |
|
|
2310 | return c; |
2290 | } |
2311 | } |
2291 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2312 | while (is_one_of (WHITESPACE, c)); |
2292 | |
2313 | |
2293 | /* record it */ |
2314 | /* record it */ |
2294 | #if SHOW_ERROR_LINE |
2315 | #if SHOW_ERROR_LINE |
2295 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2316 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2296 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2317 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2297 | #endif |
2318 | #endif |
2298 | |
2319 | |
2299 | if (c != EOF) |
|
|
2300 | { |
|
|
2301 | backchar (SCHEME_A_ c); |
2320 | backchar (SCHEME_A_ c); |
2302 | return 1; |
2321 | return 1; |
2303 | } |
|
|
2304 | else |
|
|
2305 | return EOF; |
|
|
2306 | } |
2322 | } |
2307 | |
2323 | |
2308 | /* get token */ |
2324 | /* get token */ |
2309 | static int |
2325 | static int |
2310 | token (SCHEME_P) |
2326 | token (SCHEME_P) |
… | |
… | |
2326 | return TOK_RPAREN; |
2342 | return TOK_RPAREN; |
2327 | |
2343 | |
2328 | case '.': |
2344 | case '.': |
2329 | c = inchar (SCHEME_A); |
2345 | c = inchar (SCHEME_A); |
2330 | |
2346 | |
2331 | if (is_one_of (" \n\t", c)) |
2347 | if (is_one_of (WHITESPACE, c)) |
2332 | return TOK_DOT; |
2348 | return TOK_DOT; |
2333 | else |
2349 | else |
2334 | { |
2350 | { |
2335 | //TODO: ungetc twice in a row is not supported in C |
|
|
2336 | backchar (SCHEME_A_ c); |
2351 | backchar (SCHEME_A_ c); |
2337 | backchar (SCHEME_A_ '.'); |
|
|
2338 | return TOK_ATOM; |
2352 | return TOK_DOTATOM; |
2339 | } |
2353 | } |
|
|
2354 | |
|
|
2355 | case '|': |
|
|
2356 | return TOK_STRATOM; |
2340 | |
2357 | |
2341 | case '\'': |
2358 | case '\'': |
2342 | return TOK_QUOTE; |
2359 | return TOK_QUOTE; |
2343 | |
2360 | |
2344 | case ';': |
2361 | case ';': |
… | |
… | |
2476 | } |
2493 | } |
2477 | |
2494 | |
2478 | putcharacter (SCHEME_A_ '"'); |
2495 | putcharacter (SCHEME_A_ '"'); |
2479 | } |
2496 | } |
2480 | |
2497 | |
2481 | |
|
|
2482 | /* print atoms */ |
2498 | /* print atoms */ |
2483 | static void |
2499 | static void |
2484 | printatom (SCHEME_P_ pointer l, int f) |
2500 | printatom (SCHEME_P_ pointer l, int f) |
2485 | { |
2501 | { |
2486 | char *p; |
2502 | char *p; |
2487 | int len; |
2503 | int len; |
2488 | |
2504 | |
2489 | atom2str (SCHEME_A_ l, f, &p, &len); |
2505 | atom2str (SCHEME_A_ l, f, &p, &len); |
2490 | putchars (SCHEME_A_ p, len); |
2506 | putchars (SCHEME_A_ p, len); |
2491 | } |
2507 | } |
2492 | |
|
|
2493 | |
2508 | |
2494 | /* Uses internal buffer unless string pointer is already available */ |
2509 | /* Uses internal buffer unless string pointer is already available */ |
2495 | static void |
2510 | static void |
2496 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2511 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2497 | { |
2512 | { |
… | |
… | |
2658 | #endif |
2673 | #endif |
2659 | } |
2674 | } |
2660 | else if (is_continuation (l)) |
2675 | else if (is_continuation (l)) |
2661 | p = "#<CONTINUATION>"; |
2676 | p = "#<CONTINUATION>"; |
2662 | else |
2677 | else |
|
|
2678 | { |
|
|
2679 | #if USE_PRINTF |
|
|
2680 | p = SCHEME_V->strbuff; |
|
|
2681 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2682 | #else |
2663 | p = "#<ERROR>"; |
2683 | p = "#<ERROR>"; |
|
|
2684 | #endif |
|
|
2685 | } |
2664 | |
2686 | |
2665 | *pp = p; |
2687 | *pp = p; |
2666 | *plen = strlen (p); |
2688 | *plen = strlen (p); |
2667 | } |
2689 | } |
2668 | |
2690 | |
… | |
… | |
2812 | /* () is #t in R5RS */ |
2834 | /* () is #t in R5RS */ |
2813 | #define is_true(p) ((p) != S_F) |
2835 | #define is_true(p) ((p) != S_F) |
2814 | #define is_false(p) ((p) == S_F) |
2836 | #define is_false(p) ((p) == S_F) |
2815 | |
2837 | |
2816 | /* ========== Environment implementation ========== */ |
2838 | /* ========== Environment implementation ========== */ |
2817 | |
|
|
2818 | #if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) |
|
|
2819 | |
|
|
2820 | static int |
|
|
2821 | hash_fn (const char *key, int table_size) |
|
|
2822 | { |
|
|
2823 | const unsigned char *p = key; |
|
|
2824 | uint32_t hash = 2166136261; |
|
|
2825 | |
|
|
2826 | while (*p) |
|
|
2827 | hash = (hash ^ *p++) * 16777619; |
|
|
2828 | |
|
|
2829 | return hash % table_size; |
|
|
2830 | } |
|
|
2831 | #endif |
|
|
2832 | |
2839 | |
2833 | #ifndef USE_ALIST_ENV |
2840 | #ifndef USE_ALIST_ENV |
2834 | |
2841 | |
2835 | /* |
2842 | /* |
2836 | * In this implementation, each frame of the environment may be |
2843 | * In this implementation, each frame of the environment may be |
… | |
… | |
2853 | |
2860 | |
2854 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2861 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2855 | setenvironment (SCHEME_V->envir); |
2862 | setenvironment (SCHEME_V->envir); |
2856 | } |
2863 | } |
2857 | |
2864 | |
|
|
2865 | static uint32_t |
|
|
2866 | sym_hash (pointer sym, uint32_t size) |
|
|
2867 | { |
|
|
2868 | uintptr_t ptr = (uintptr_t)sym; |
|
|
2869 | |
|
|
2870 | #if 0 |
|
|
2871 | /* table size is prime, so why mix */ |
|
|
2872 | ptr += ptr >> 32; |
|
|
2873 | ptr += ptr >> 16; |
|
|
2874 | ptr += ptr >> 8; |
|
|
2875 | #endif |
|
|
2876 | |
|
|
2877 | return ptr % size; |
|
|
2878 | } |
|
|
2879 | |
2858 | ecb_inline void |
2880 | ecb_inline void |
2859 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2881 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2860 | { |
2882 | { |
2861 | pointer slot = immutable_cons (variable, value); |
2883 | pointer slot = immutable_cons (variable, value); |
2862 | |
2884 | |
2863 | if (is_vector (car (env))) |
2885 | if (is_vector (car (env))) |
2864 | { |
2886 | { |
2865 | int location = hash_fn (symname (variable), veclength (car (env))); |
2887 | int location = sym_hash (variable, veclength (car (env))); |
2866 | |
|
|
2867 | vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); |
2888 | vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); |
2868 | } |
2889 | } |
2869 | else |
2890 | else |
2870 | set_car (env, immutable_cons (slot, car (env))); |
2891 | set_car (env, immutable_cons (slot, car (env))); |
2871 | } |
2892 | } |
2872 | |
2893 | |
2873 | static pointer |
2894 | static pointer |
2874 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2895 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2875 | { |
2896 | { |
2876 | pointer x, y; |
2897 | pointer x, y; |
2877 | int location; |
|
|
2878 | |
2898 | |
2879 | for (x = env; x != NIL; x = cdr (x)) |
2899 | for (x = env; x != NIL; x = cdr (x)) |
2880 | { |
2900 | { |
2881 | if (is_vector (car (x))) |
2901 | if (is_vector (car (x))) |
2882 | { |
2902 | { |
2883 | location = hash_fn (symname (hdl), veclength (car (x))); |
2903 | int location = sym_hash (hdl, veclength (car (x))); |
2884 | y = vector_get (car (x), location); |
2904 | y = vector_get (car (x), location); |
2885 | } |
2905 | } |
2886 | else |
2906 | else |
2887 | y = car (x); |
2907 | y = car (x); |
2888 | |
2908 | |
… | |
… | |
2925 | for (y = car (x); y != NIL; y = cdr (y)) |
2945 | for (y = car (x); y != NIL; y = cdr (y)) |
2926 | if (caar (y) == hdl) |
2946 | if (caar (y) == hdl) |
2927 | break; |
2947 | break; |
2928 | |
2948 | |
2929 | if (y != NIL) |
2949 | if (y != NIL) |
|
|
2950 | return car (y); |
2930 | break; |
2951 | break; |
2931 | |
2952 | |
2932 | if (!all) |
2953 | if (!all) |
2933 | return NIL; |
2954 | break; |
2934 | } |
2955 | } |
2935 | |
|
|
2936 | if (x != NIL) |
|
|
2937 | return car (y); |
|
|
2938 | |
2956 | |
2939 | return NIL; |
2957 | return NIL; |
2940 | } |
2958 | } |
2941 | |
2959 | |
2942 | #endif /* USE_ALIST_ENV else */ |
2960 | #endif /* USE_ALIST_ENV else */ |
2943 | |
2961 | |
2944 | ecb_inline void |
2962 | ecb_inline void |
2945 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2963 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2946 | { |
2964 | { |
|
|
2965 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2947 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2966 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2948 | } |
2967 | } |
2949 | |
2968 | |
2950 | ecb_inline void |
2969 | ecb_inline void |
2951 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2970 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
… | |
… | |
3052 | { |
3071 | { |
3053 | int nframes = (uintptr_t)SCHEME_V->dump; |
3072 | int nframes = (uintptr_t)SCHEME_V->dump; |
3054 | struct dump_stack_frame *next_frame; |
3073 | struct dump_stack_frame *next_frame; |
3055 | |
3074 | |
3056 | /* enough room for the next frame? */ |
3075 | /* enough room for the next frame? */ |
3057 | if (nframes >= SCHEME_V->dump_size) |
3076 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3058 | { |
3077 | { |
3059 | SCHEME_V->dump_size += STACK_GROWTH; |
3078 | SCHEME_V->dump_size += STACK_GROWTH; |
3060 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3079 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3061 | } |
3080 | } |
3062 | |
3081 | |
… | |
… | |
3243 | |
3262 | |
3244 | #endif |
3263 | #endif |
3245 | |
3264 | |
3246 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3265 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3247 | |
3266 | |
|
|
3267 | #if EXPERIMENT |
|
|
3268 | static int |
|
|
3269 | debug (SCHEME_P_ int indent, pointer x) |
|
|
3270 | { |
|
|
3271 | int c; |
|
|
3272 | |
|
|
3273 | if (is_syntax (x)) |
|
|
3274 | { |
|
|
3275 | printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x)); |
|
|
3276 | return 8 + 8; |
|
|
3277 | } |
|
|
3278 | |
|
|
3279 | if (x == NIL) |
|
|
3280 | { |
|
|
3281 | printf ("%*sNIL\n", indent, ""); |
|
|
3282 | return 3; |
|
|
3283 | } |
|
|
3284 | |
|
|
3285 | switch (type (x)) |
|
|
3286 | { |
|
|
3287 | case T_INTEGER: |
|
|
3288 | printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x); |
|
|
3289 | return 32+8; |
|
|
3290 | |
|
|
3291 | case T_SYMBOL: |
|
|
3292 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
|
|
3293 | return 24+8; |
|
|
3294 | |
|
|
3295 | case T_CLOSURE: |
|
|
3296 | printf ("%*sS<%s>\n", indent, "", "closure"); |
|
|
3297 | debug (SCHEME_A_ indent + 3, cdr(x)); |
|
|
3298 | return 32 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3299 | |
|
|
3300 | case T_PAIR: |
|
|
3301 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
|
|
3302 | c = debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3303 | c += debug (SCHEME_A_ indent + 3, cdr (x)); |
|
|
3304 | return c + 1; |
|
|
3305 | |
|
|
3306 | case T_PORT: |
|
|
3307 | printf ("%*sS<%s>\n", indent, "", "port"); |
|
|
3308 | return 24+8; |
|
|
3309 | |
|
|
3310 | case T_VECTOR: |
|
|
3311 | printf ("%*sS<%s>\n", indent, "", "vector"); |
|
|
3312 | return 24+8; |
|
|
3313 | |
|
|
3314 | case T_ENVIRONMENT: |
|
|
3315 | printf ("%*sS<%s>\n", indent, "", "environment"); |
|
|
3316 | return 0 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3317 | |
|
|
3318 | default: |
|
|
3319 | printf ("unhandled type %d\n", type (x)); |
|
|
3320 | break; |
|
|
3321 | } |
|
|
3322 | } |
|
|
3323 | #endif |
|
|
3324 | |
3248 | static int |
3325 | static int |
3249 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3326 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3250 | { |
3327 | { |
3251 | pointer args = SCHEME_V->args; |
3328 | pointer args = SCHEME_V->args; |
3252 | pointer x, y; |
3329 | pointer x, y; |
3253 | |
3330 | |
3254 | switch (op) |
3331 | switch (op) |
3255 | { |
3332 | { |
|
|
3333 | #if EXPERIMENT //D |
|
|
3334 | case OP_DEBUG: |
|
|
3335 | printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); |
|
|
3336 | printf ("\n"); |
|
|
3337 | s_return (S_T); |
|
|
3338 | #endif |
3256 | case OP_LOAD: /* load */ |
3339 | case OP_LOAD: /* load */ |
3257 | if (file_interactive (SCHEME_A)) |
3340 | if (file_interactive (SCHEME_A)) |
3258 | { |
3341 | { |
3259 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3342 | putstr (SCHEME_A_ "Loading "); putstr (SCHEME_A_ strvalue (car (args))); putstr (SCHEME_A_ "\n"); |
3260 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3343 | //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3261 | } |
3344 | } |
3262 | |
3345 | |
3263 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3346 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3264 | Error_1 ("unable to open", car (args)); |
3347 | Error_1 ("unable to open", car (args)); |
3265 | else |
3348 | else |
… | |
… | |
3269 | } |
3352 | } |
3270 | |
3353 | |
3271 | case OP_T0LVL: /* top level */ |
3354 | case OP_T0LVL: /* top level */ |
3272 | |
3355 | |
3273 | /* If we reached the end of file, this loop is done. */ |
3356 | /* If we reached the end of file, this loop is done. */ |
3274 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3357 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3275 | { |
3358 | { |
3276 | if (SCHEME_V->file_i == 0) |
3359 | if (SCHEME_V->file_i == 0) |
3277 | { |
3360 | { |
3278 | SCHEME_V->args = NIL; |
3361 | SCHEME_V->args = NIL; |
3279 | s_goto (OP_QUIT); |
3362 | s_goto (OP_QUIT); |
… | |
… | |
3357 | #endif |
3440 | #endif |
3358 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3441 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3359 | { |
3442 | { |
3360 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3443 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3361 | |
3444 | |
3362 | if (x != NIL) |
3445 | if (x == NIL) |
3363 | s_return (slot_value_in_env (x)); |
|
|
3364 | else |
|
|
3365 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3446 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
|
|
3447 | |
|
|
3448 | s_return (slot_value_in_env (x)); |
3366 | } |
3449 | } |
3367 | else if (is_pair (SCHEME_V->code)) |
3450 | else if (is_pair (SCHEME_V->code)) |
3368 | { |
3451 | { |
3369 | x = car (SCHEME_V->code); |
3452 | x = car (SCHEME_V->code); |
3370 | |
3453 | |
… | |
… | |
3383 | } |
3466 | } |
3384 | else |
3467 | else |
3385 | s_return (SCHEME_V->code); |
3468 | s_return (SCHEME_V->code); |
3386 | |
3469 | |
3387 | case OP_E0ARGS: /* eval arguments */ |
3470 | case OP_E0ARGS: /* eval arguments */ |
3388 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3471 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3389 | { |
3472 | { |
3390 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3473 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3391 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3474 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3392 | SCHEME_V->code = SCHEME_V->value; |
3475 | SCHEME_V->code = SCHEME_V->value; |
3393 | s_goto (OP_APPLY); |
3476 | s_goto (OP_APPLY); |
… | |
… | |
3447 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3530 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3448 | else if (is_foreign (SCHEME_V->code)) |
3531 | else if (is_foreign (SCHEME_V->code)) |
3449 | { |
3532 | { |
3450 | /* Keep nested calls from GC'ing the arglist */ |
3533 | /* Keep nested calls from GC'ing the arglist */ |
3451 | push_recent_alloc (SCHEME_A_ args, NIL); |
3534 | push_recent_alloc (SCHEME_A_ args, NIL); |
3452 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3535 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3453 | |
3536 | |
3454 | s_return (x); |
3537 | s_return (x); |
3455 | } |
3538 | } |
3456 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3539 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3457 | { |
3540 | { |
… | |
… | |
3494 | |
3577 | |
3495 | case OP_DOMACRO: /* do macro */ |
3578 | case OP_DOMACRO: /* do macro */ |
3496 | SCHEME_V->code = SCHEME_V->value; |
3579 | SCHEME_V->code = SCHEME_V->value; |
3497 | s_goto (OP_EVAL); |
3580 | s_goto (OP_EVAL); |
3498 | |
3581 | |
3499 | #if 1 |
|
|
3500 | |
|
|
3501 | case OP_LAMBDA: /* lambda */ |
3582 | case OP_LAMBDA: /* lambda */ |
3502 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3583 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3503 | set SCHEME_V->value fall thru */ |
3584 | set SCHEME_V->value fall thru */ |
3504 | { |
3585 | { |
3505 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3586 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
… | |
… | |
3511 | SCHEME_V->code = slot_value_in_env (f); |
3592 | SCHEME_V->code = slot_value_in_env (f); |
3512 | s_goto (OP_APPLY); |
3593 | s_goto (OP_APPLY); |
3513 | } |
3594 | } |
3514 | |
3595 | |
3515 | SCHEME_V->value = SCHEME_V->code; |
3596 | SCHEME_V->value = SCHEME_V->code; |
3516 | /* Fallthru */ |
|
|
3517 | } |
3597 | } |
|
|
3598 | /* Fallthru */ |
3518 | |
3599 | |
3519 | case OP_LAMBDA1: |
3600 | case OP_LAMBDA1: |
3520 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3601 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3521 | |
|
|
3522 | #else |
|
|
3523 | |
|
|
3524 | case OP_LAMBDA: /* lambda */ |
|
|
3525 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
|
|
3526 | |
|
|
3527 | #endif |
|
|
3528 | |
3602 | |
3529 | case OP_MKCLOSURE: /* make-closure */ |
3603 | case OP_MKCLOSURE: /* make-closure */ |
3530 | x = car (args); |
3604 | x = car (args); |
3531 | |
3605 | |
3532 | if (car (x) == SCHEME_V->LAMBDA) |
3606 | if (car (x) == SCHEME_V->LAMBDA) |
… | |
… | |
4128 | else |
4202 | else |
4129 | Error_0 ("modulo: division by zero"); |
4203 | Error_0 ("modulo: division by zero"); |
4130 | |
4204 | |
4131 | s_return (mk_number (SCHEME_A_ v)); |
4205 | s_return (mk_number (SCHEME_A_ v)); |
4132 | |
4206 | |
4133 | case OP_CAR: /* car */ |
4207 | /* the compiler will optimize this mess... */ |
4134 | s_return (caar (args)); |
4208 | case OP_CAR: op_car: s_return (car (x)); |
4135 | |
4209 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4136 | case OP_CDR: /* cdr */ |
4210 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4137 | s_return (cdar (args)); |
4211 | case OP_CADR: op_cadr: x = cdr (x); goto op_car; |
|
|
4212 | case OP_CDAR: op_cdar: x = car (x); goto op_cdr; |
|
|
4213 | case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr; |
|
|
4214 | case OP_CAAAR: op_caaar: x = car (x); goto op_caar; |
|
|
4215 | case OP_CAADR: op_caadr: x = cdr (x); goto op_caar; |
|
|
4216 | case OP_CADAR: op_cadar: x = car (x); goto op_cadr; |
|
|
4217 | case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr; |
|
|
4218 | case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar; |
|
|
4219 | case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar; |
|
|
4220 | case OP_CDDAR: op_cddar: x = car (x); goto op_cddr; |
|
|
4221 | case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr; |
|
|
4222 | case OP_CAAAAR: x = car (x); goto op_caaar; |
|
|
4223 | case OP_CAAADR: x = cdr (x); goto op_caaar; |
|
|
4224 | case OP_CAADAR: x = car (x); goto op_caadr; |
|
|
4225 | case OP_CAADDR: x = cdr (x); goto op_caadr; |
|
|
4226 | case OP_CADAAR: x = car (x); goto op_cadar; |
|
|
4227 | case OP_CADADR: x = cdr (x); goto op_cadar; |
|
|
4228 | case OP_CADDAR: x = car (x); goto op_caddr; |
|
|
4229 | case OP_CADDDR: x = cdr (x); goto op_caddr; |
|
|
4230 | case OP_CDAAAR: x = car (x); goto op_cdaar; |
|
|
4231 | case OP_CDAADR: x = cdr (x); goto op_cdaar; |
|
|
4232 | case OP_CDADAR: x = car (x); goto op_cdadr; |
|
|
4233 | case OP_CDADDR: x = cdr (x); goto op_cdadr; |
|
|
4234 | case OP_CDDAAR: x = car (x); goto op_cddar; |
|
|
4235 | case OP_CDDADR: x = cdr (x); goto op_cddar; |
|
|
4236 | case OP_CDDDAR: x = car (x); goto op_cdddr; |
|
|
4237 | case OP_CDDDDR: x = cdr (x); goto op_cdddr; |
4138 | |
4238 | |
4139 | case OP_CONS: /* cons */ |
4239 | case OP_CONS: /* cons */ |
4140 | set_cdr (args, cadr (args)); |
4240 | set_cdr (args, cadr (args)); |
4141 | s_return (args); |
4241 | s_return (args); |
4142 | |
4242 | |
… | |
… | |
4391 | } |
4491 | } |
4392 | |
4492 | |
4393 | case OP_VECLEN: /* vector-length */ |
4493 | case OP_VECLEN: /* vector-length */ |
4394 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4494 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4395 | |
4495 | |
|
|
4496 | case OP_VECRESIZE: |
|
|
4497 | vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args)); |
|
|
4498 | s_return (x); |
|
|
4499 | |
4396 | case OP_VECREF: /* vector-ref */ |
4500 | case OP_VECREF: /* vector-ref */ |
4397 | { |
4501 | { |
4398 | int index = ivalue_unchecked (cadr (args)); |
4502 | int index = ivalue_unchecked (cadr (args)); |
4399 | |
4503 | |
4400 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4504 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
… | |
… | |
4460 | pointer d = cdr (args); |
4564 | pointer d = cdr (args); |
4461 | int r; |
4565 | int r; |
4462 | |
4566 | |
4463 | switch (op) |
4567 | switch (op) |
4464 | { |
4568 | { |
4465 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4569 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4466 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; |
4570 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break; |
4467 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4571 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4468 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4572 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4469 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
4573 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
|
|
4574 | case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break; |
4470 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4575 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4471 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4576 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4472 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4577 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4473 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4578 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4474 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4579 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4475 | |
4580 | |
4476 | #if USE_CHAR_CLASSIFIERS |
4581 | #if USE_CHAR_CLASSIFIERS |
4477 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4582 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4478 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4583 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4479 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4584 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
… | |
… | |
4529 | } |
4634 | } |
4530 | else |
4635 | else |
4531 | s_return (SCHEME_V->code); |
4636 | s_return (SCHEME_V->code); |
4532 | |
4637 | |
4533 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4638 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4534 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4639 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4535 | s_return (SCHEME_V->value); |
4640 | s_return (SCHEME_V->value); |
4536 | |
4641 | |
4537 | #if USE_PORTS |
4642 | #if USE_PORTS |
4538 | |
4643 | |
4539 | case OP_WRITE: /* write */ |
4644 | case OP_WRITE: /* write */ |
… | |
… | |
4684 | SCHEME_V->gc_verbose = (a != S_F); |
4789 | SCHEME_V->gc_verbose = (a != S_F); |
4685 | s_retbool (was); |
4790 | s_retbool (was); |
4686 | } |
4791 | } |
4687 | |
4792 | |
4688 | case OP_NEWSEGMENT: /* new-segment */ |
4793 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
4794 | #if 0 |
4689 | if (!is_pair (args) || !is_number (a)) |
4795 | if (!is_pair (args) || !is_number (a)) |
4690 | Error_0 ("new-segment: argument must be a number"); |
4796 | Error_0 ("new-segment: argument must be a number"); |
4691 | |
4797 | #endif |
4692 | alloc_cellseg (SCHEME_A_ ivalue (a)); |
4798 | s_retbool (alloc_cellseg (SCHEME_A)); |
4693 | |
|
|
4694 | s_return (S_T); |
|
|
4695 | |
4799 | |
4696 | case OP_OBLIST: /* oblist */ |
4800 | case OP_OBLIST: /* oblist */ |
4697 | s_return (oblist_all_symbols (SCHEME_A)); |
4801 | s_return (oblist_all_symbols (SCHEME_A)); |
4698 | |
4802 | |
4699 | #if USE_PORTS |
4803 | #if USE_PORTS |
… | |
… | |
4769 | s_return (p == NIL ? S_F : p); |
4873 | s_return (p == NIL ? S_F : p); |
4770 | } |
4874 | } |
4771 | |
4875 | |
4772 | case OP_GET_OUTSTRING: /* get-output-string */ |
4876 | case OP_GET_OUTSTRING: /* get-output-string */ |
4773 | { |
4877 | { |
4774 | port *p; |
4878 | port *p = port (a); |
4775 | |
4879 | |
4776 | if ((p = a->object.port)->kind & port_string) |
4880 | if (p->kind & port_string) |
4777 | { |
4881 | { |
4778 | off_t size; |
4882 | off_t size; |
4779 | char *str; |
4883 | char *str; |
4780 | |
4884 | |
4781 | size = p->rep.string.curr - p->rep.string.start + 1; |
4885 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4886 | int res; |
4990 | int res; |
4887 | |
4991 | |
4888 | if (is_pair (args)) |
4992 | if (is_pair (args)) |
4889 | p = car (args); |
4993 | p = car (args); |
4890 | |
4994 | |
4891 | res = p->object.port->kind & port_string; |
4995 | res = port (p)->kind & port_string; |
4892 | |
4996 | |
4893 | s_retbool (res); |
4997 | s_retbool (res); |
4894 | } |
4998 | } |
4895 | |
4999 | |
4896 | case OP_SET_INPORT: /* set-input-port */ |
5000 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
4955 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5059 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
4956 | SCHEME_V->tok = token (SCHEME_A); |
5060 | SCHEME_V->tok = token (SCHEME_A); |
4957 | s_goto (OP_RDSEXPR); |
5061 | s_goto (OP_RDSEXPR); |
4958 | |
5062 | |
4959 | case TOK_ATOM: |
5063 | case TOK_ATOM: |
4960 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); |
5064 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); |
|
|
5065 | |
|
|
5066 | case TOK_DOTATOM: |
|
|
5067 | SCHEME_V->strbuff[0] = '.'; |
|
|
5068 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
|
|
5069 | |
|
|
5070 | case TOK_STRATOM: |
|
|
5071 | x = readstrexp (SCHEME_A_ '|'); |
|
|
5072 | //TODO: haven't checked whether the garbage collector could interfere |
|
|
5073 | s_return (mk_atom (SCHEME_A_ strvalue (x))); |
4961 | |
5074 | |
4962 | case TOK_DQUOTE: |
5075 | case TOK_DQUOTE: |
4963 | x = readstrexp (SCHEME_A); |
5076 | x = readstrexp (SCHEME_A_ '"'); |
4964 | |
5077 | |
4965 | if (x == S_F) |
5078 | if (x == S_F) |
4966 | Error_0 ("Error reading string"); |
5079 | Error_0 ("Error reading string"); |
4967 | |
5080 | |
4968 | setimmutable (x); |
5081 | setimmutable (x); |
… | |
… | |
4980 | s_goto (OP_EVAL); |
5093 | s_goto (OP_EVAL); |
4981 | } |
5094 | } |
4982 | } |
5095 | } |
4983 | |
5096 | |
4984 | case TOK_SHARP_CONST: |
5097 | case TOK_SHARP_CONST: |
4985 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) |
5098 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) |
4986 | Error_0 ("undefined sharp expression"); |
5099 | Error_0 ("undefined sharp expression"); |
4987 | else |
5100 | else |
4988 | s_return (x); |
5101 | s_return (x); |
4989 | |
5102 | |
4990 | default: |
5103 | default: |
… | |
… | |
5212 | |
5325 | |
5213 | case OP_CLOSUREP: /* closure? */ |
5326 | case OP_CLOSUREP: /* closure? */ |
5214 | /* |
5327 | /* |
5215 | * Note, macro object is also a closure. |
5328 | * Note, macro object is also a closure. |
5216 | * Therefore, (closure? <#MACRO>) ==> #t |
5329 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5330 | * (schmorp) well, obviously not, fix? TODO |
5217 | */ |
5331 | */ |
5218 | s_retbool (is_closure (a)); |
5332 | s_retbool (is_closure (a)); |
5219 | |
5333 | |
5220 | case OP_MACROP: /* macro? */ |
5334 | case OP_MACROP: /* macro? */ |
5221 | s_retbool (is_macro (a)); |
5335 | s_retbool (is_macro (a)); |
… | |
… | |
5426 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5540 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5427 | return; |
5541 | return; |
5428 | |
5542 | |
5429 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5543 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5430 | { |
5544 | { |
5431 | xwrstr ("No memory!\n"); |
5545 | putstr (SCHEME_A_ "No memory!\n"); |
5432 | return; |
5546 | return; |
5433 | } |
5547 | } |
5434 | } |
5548 | } |
5435 | } |
5549 | } |
5436 | |
5550 | |
… | |
… | |
5462 | |
5576 | |
5463 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5577 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5464 | static int |
5578 | static int |
5465 | syntaxnum (pointer p) |
5579 | syntaxnum (pointer p) |
5466 | { |
5580 | { |
5467 | const char *s = strvalue (car (p)); |
5581 | const char *s = strvalue (p); |
5468 | |
5582 | |
5469 | switch (strlength (car (p))) |
5583 | switch (strlength (p)) |
5470 | { |
5584 | { |
5471 | case 2: |
5585 | case 2: |
5472 | if (s[0] == 'i') |
5586 | if (s[0] == 'i') |
5473 | return OP_IF0; /* if */ |
5587 | return OP_IF0; /* if */ |
5474 | else |
5588 | else |
… | |
… | |
5549 | ecb_cold int |
5663 | ecb_cold int |
5550 | scheme_init (SCHEME_P) |
5664 | scheme_init (SCHEME_P) |
5551 | { |
5665 | { |
5552 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5666 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5553 | pointer x; |
5667 | pointer x; |
|
|
5668 | |
|
|
5669 | /* this memset is not strictly correct, as we assume (intcache) |
|
|
5670 | * that memset 0 will also set pointers to 0, but memset does |
|
|
5671 | * of course not guarantee that. screw such systems. |
|
|
5672 | */ |
|
|
5673 | memset (SCHEME_V, 0, sizeof (*SCHEME_V)); |
5554 | |
5674 | |
5555 | num_set_fixnum (num_zero, 1); |
5675 | num_set_fixnum (num_zero, 1); |
5556 | num_set_ivalue (num_zero, 0); |
5676 | num_set_ivalue (num_zero, 0); |
5557 | num_set_fixnum (num_one, 1); |
5677 | num_set_fixnum (num_one, 1); |
5558 | num_set_ivalue (num_one, 1); |
5678 | num_set_ivalue (num_one, 1); |
… | |
… | |
5570 | SCHEME_V->save_inport = NIL; |
5690 | SCHEME_V->save_inport = NIL; |
5571 | SCHEME_V->loadport = NIL; |
5691 | SCHEME_V->loadport = NIL; |
5572 | SCHEME_V->nesting = 0; |
5692 | SCHEME_V->nesting = 0; |
5573 | SCHEME_V->interactive_repl = 0; |
5693 | SCHEME_V->interactive_repl = 0; |
5574 | |
5694 | |
5575 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5695 | if (!alloc_cellseg (SCHEME_A)) |
5576 | { |
5696 | { |
5577 | #if USE_ERROR_CHECKING |
5697 | #if USE_ERROR_CHECKING |
5578 | SCHEME_V->no_memory = 1; |
5698 | SCHEME_V->no_memory = 1; |
5579 | return 0; |
5699 | return 0; |
5580 | #endif |
5700 | #endif |
… | |
… | |
5910 | # endif |
6030 | # endif |
5911 | int fin; |
6031 | int fin; |
5912 | char *file_name = InitFile; |
6032 | char *file_name = InitFile; |
5913 | int retcode; |
6033 | int retcode; |
5914 | int isfile = 1; |
6034 | int isfile = 1; |
|
|
6035 | system ("ps v $PPID");//D |
5915 | |
6036 | |
5916 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6037 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5917 | { |
6038 | { |
5918 | xwrstr ("Usage: tinyscheme -?\n"); |
6039 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
5919 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6040 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
5920 | xwrstr ("followed by\n"); |
6041 | putstr (SCHEME_A_ "followed by\n"); |
5921 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6042 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
5922 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6043 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
5923 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6044 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
5924 | xwrstr ("Use - as filename for stdin.\n"); |
6045 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
5925 | return 1; |
6046 | return 1; |
5926 | } |
6047 | } |
5927 | |
6048 | |
5928 | if (!scheme_init (SCHEME_A)) |
6049 | if (!scheme_init (SCHEME_A)) |
5929 | { |
6050 | { |
5930 | xwrstr ("Could not initialize!\n"); |
6051 | putstr (SCHEME_A_ "Could not initialize!\n"); |
5931 | return 2; |
6052 | return 2; |
5932 | } |
6053 | } |
5933 | |
6054 | |
5934 | # if USE_PORTS |
6055 | # if USE_PORTS |
5935 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6056 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
5980 | fin = open (file_name, O_RDONLY); |
6101 | fin = open (file_name, O_RDONLY); |
5981 | #endif |
6102 | #endif |
5982 | |
6103 | |
5983 | if (isfile && fin < 0) |
6104 | if (isfile && fin < 0) |
5984 | { |
6105 | { |
5985 | xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); |
6106 | putstr (SCHEME_A_ "Could not open file "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); |
5986 | } |
6107 | } |
5987 | else |
6108 | else |
5988 | { |
6109 | { |
5989 | if (isfile) |
6110 | if (isfile) |
5990 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6111 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
… | |
… | |
5994 | #if USE_PORTS |
6115 | #if USE_PORTS |
5995 | if (!isfile || fin != STDIN_FILENO) |
6116 | if (!isfile || fin != STDIN_FILENO) |
5996 | { |
6117 | { |
5997 | if (SCHEME_V->retcode != 0) |
6118 | if (SCHEME_V->retcode != 0) |
5998 | { |
6119 | { |
5999 | xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); |
6120 | putstr (SCHEME_A_ "Errors encountered reading "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); |
6000 | } |
6121 | } |
6001 | |
6122 | |
6002 | if (isfile) |
6123 | if (isfile) |
6003 | close (fin); |
6124 | close (fin); |
6004 | } |
6125 | } |