… | |
… | |
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, |
… | |
… | |
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 | |
… | |
… | |
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 |
… | |
… | |
903 | #endif |
884 | #endif |
904 | } |
885 | } |
905 | |
886 | |
906 | /* allocate new cell segment */ |
887 | /* allocate new cell segment */ |
907 | static int |
888 | 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 | |
|
|
1024 | static void |
969 | 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 | |
… | |
… | |
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 | { |
… | |
… | |
1114 | set_cdr (x, b); |
1059 | set_cdr (x, b); |
1115 | |
1060 | |
1116 | return x; |
1061 | return x; |
1117 | } |
1062 | } |
1118 | |
1063 | |
|
|
1064 | 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; |
|
|
1082 | |
|
|
1083 | while (*p) |
|
|
1084 | hash = (hash ^ *p++) * 16777619; |
|
|
1085 | |
|
|
1086 | return hash % table_size; |
|
|
1087 | } |
1124 | |
1088 | |
1125 | static pointer |
1089 | 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 */ |
… | |
… | |
1130 | |
1094 | |
1131 | /* returns the new symbol */ |
1095 | /* returns the new symbol */ |
1132 | static pointer |
1096 | 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_inline pointer |
1147 | oblist_find_by_name (SCHEME_P_ const char *name) |
1106 | oblist_find_by_name (SCHEME_P_ const char *name) |
… | |
… | |
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) |
… | |
… | |
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 | |
… | |
… | |
1206 | |
1165 | |
1207 | /* returns the new symbol */ |
1166 | /* returns the new symbol */ |
1208 | static pointer |
1167 | 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 | 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 | 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 */ |
… | |
… | |
1400 | |
1370 | |
1401 | INTERFACE pointer |
1371 | 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 | static pointer |
1426 | mk_atom (SCHEME_P_ char *q) |
1389 | mk_atom (SCHEME_P_ char *q) |
… | |
… | |
1578 | |
1541 | |
1579 | if (ecb_expect_false (is_vector (p))) |
1542 | if (ecb_expect_false (is_vector (p))) |
1580 | { |
1543 | { |
1581 | int i; |
1544 | int i; |
1582 | |
1545 | |
1583 | for (i = 0; i < p->object.vector.length; i++) |
1546 | for (i = 0; i < veclength (p); i++) |
1584 | mark (vecvalue (p)[i]); |
1547 | mark (vecvalue (p)[i]); |
1585 | } |
1548 | } |
1586 | |
1549 | |
1587 | if (is_atom (p)) |
1550 | if (is_atom (p)) |
1588 | goto E6; |
1551 | goto E6; |
… | |
… | |
1635 | |
1598 | |
1636 | /* garbage collection. parameter a, b is marked. */ |
1599 | /* garbage collection. parameter a, b is marked. */ |
1637 | static void |
1600 | static void |
1638 | gc (SCHEME_P_ pointer a, pointer b) |
1601 | gc (SCHEME_P_ pointer a, pointer b) |
1639 | { |
1602 | { |
1640 | pointer p; |
|
|
1641 | int i; |
1603 | int i; |
1642 | |
1604 | |
1643 | if (SCHEME_V->gc_verbose) |
1605 | if (SCHEME_V->gc_verbose) |
1644 | putstr (SCHEME_A_ "gc..."); |
1606 | putstr (SCHEME_A_ "gc..."); |
1645 | |
1607 | |
… | |
… | |
1661 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1623 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1662 | mark (car (S_SINK)); |
1624 | mark (car (S_SINK)); |
1663 | /* Mark any older stuff above nested C calls */ |
1625 | /* Mark any older stuff above nested C calls */ |
1664 | mark (SCHEME_V->c_nest); |
1626 | mark (SCHEME_V->c_nest); |
1665 | |
1627 | |
|
|
1628 | #if USE_INTCACHE |
|
|
1629 | /* mark intcache */ |
|
|
1630 | for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i) |
|
|
1631 | if (SCHEME_V->intcache[i - INTCACHE_MIN]) |
|
|
1632 | mark (SCHEME_V->intcache[i - INTCACHE_MIN]); |
|
|
1633 | #endif |
|
|
1634 | |
1666 | /* mark variables a, b */ |
1635 | /* mark variables a, b */ |
1667 | mark (a); |
1636 | mark (a); |
1668 | mark (b); |
1637 | mark (b); |
1669 | |
1638 | |
1670 | /* garbage collect */ |
1639 | /* garbage collect */ |
1671 | clrmark (NIL); |
1640 | clrmark (NIL); |
1672 | SCHEME_V->fcells = 0; |
1641 | SCHEME_V->fcells = 0; |
1673 | SCHEME_V->free_cell = NIL; |
1642 | SCHEME_V->free_cell = NIL; |
1674 | |
1643 | |
1675 | /* free-list is kept sorted by address so as to maintain consecutive |
1644 | if (SCHEME_V->gc_verbose) |
1676 | ranges, if possible, for use with vectors. Here we scan the cells |
1645 | putstr (SCHEME_A_ "freeing..."); |
1677 | (which are also kept sorted by address) downwards to build the |
1646 | |
1678 | free-list in sorted order. |
1647 | uint32_t total = 0; |
1679 | */ |
1648 | |
|
|
1649 | /* Here we scan the cells to build the free-list. */ |
1680 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1650 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1681 | { |
1651 | { |
1682 | p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
1652 | struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1653 | struct cell *p; |
|
|
1654 | total += SCHEME_V->cell_segsize [i]; |
1683 | |
1655 | |
1684 | while (--p >= SCHEME_V->cell_seg[i]) |
1656 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
1685 | { |
1657 | { |
|
|
1658 | pointer c = POINTER (p); |
|
|
1659 | |
1686 | if (is_mark (p)) |
1660 | if (is_mark (c)) |
1687 | clrmark (p); |
1661 | clrmark (c); |
1688 | else |
1662 | else |
1689 | { |
1663 | { |
1690 | /* reclaim cell */ |
1664 | /* reclaim cell */ |
1691 | if (typeflag (p) != T_PAIR) |
1665 | if (typeflag (c) != T_PAIR) |
1692 | { |
1666 | { |
1693 | finalize_cell (SCHEME_A_ p); |
1667 | finalize_cell (SCHEME_A_ c); |
1694 | set_typeflag (p, T_PAIR); |
1668 | set_typeflag (c, T_PAIR); |
1695 | set_car (p, NIL); |
1669 | set_car (c, NIL); |
1696 | } |
1670 | } |
1697 | |
1671 | |
1698 | ++SCHEME_V->fcells; |
1672 | ++SCHEME_V->fcells; |
1699 | set_cdr (p, SCHEME_V->free_cell); |
1673 | set_cdr (c, SCHEME_V->free_cell); |
1700 | SCHEME_V->free_cell = p; |
1674 | SCHEME_V->free_cell = c; |
1701 | } |
1675 | } |
1702 | } |
1676 | } |
1703 | } |
1677 | } |
1704 | |
1678 | |
1705 | if (SCHEME_V->gc_verbose) |
1679 | if (SCHEME_V->gc_verbose) |
1706 | { |
1680 | { |
1707 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); |
1681 | 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"); |
1708 | } |
1682 | } |
1709 | } |
1683 | } |
1710 | |
1684 | |
1711 | static void |
1685 | static void |
1712 | finalize_cell (SCHEME_P_ pointer a) |
1686 | finalize_cell (SCHEME_P_ pointer a) |
1713 | { |
1687 | { |
1714 | /* TODO, fast bitmap check? */ |
1688 | /* TODO, fast bitmap check? */ |
1715 | if (is_string (a)) |
1689 | if (is_string (a) || is_symbol (a)) |
1716 | free (strvalue (a)); |
1690 | free (strvalue (a)); |
1717 | else if (is_vector (a)) |
1691 | else if (is_vector (a)) |
1718 | free (vecvalue (a)); |
1692 | free (vecvalue (a)); |
1719 | #if USE_PORTS |
1693 | #if USE_PORTS |
1720 | else if (is_port (a)) |
1694 | else if (is_port (a)) |
1721 | { |
1695 | { |
1722 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
1696 | if (port(a)->kind & port_file && port (a)->rep.stdio.closeit) |
1723 | port_close (SCHEME_A_ a, port_input | port_output); |
1697 | port_close (SCHEME_A_ a, port_input | port_output); |
1724 | |
1698 | |
1725 | free (a->object.port); |
1699 | free (port (a)); |
1726 | } |
1700 | } |
1727 | #endif |
1701 | #endif |
1728 | } |
1702 | } |
1729 | |
1703 | |
1730 | /* ========== Routines for Reading ========== */ |
1704 | /* ========== Routines for Reading ========== */ |
… | |
… | |
1746 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1720 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1747 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1721 | 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; |
1722 | 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; |
1723 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1750 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1724 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1751 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1725 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1752 | |
1726 | |
1753 | #if SHOW_ERROR_LINE |
1727 | #if SHOW_ERROR_LINE |
1754 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1728 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1755 | |
1729 | |
1756 | if (fname) |
1730 | if (fname) |
… | |
… | |
1773 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1747 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1774 | #if USE_PORTS |
1748 | #if USE_PORTS |
1775 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1749 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1776 | #endif |
1750 | #endif |
1777 | SCHEME_V->file_i--; |
1751 | SCHEME_V->file_i--; |
1778 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1752 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1779 | } |
1753 | } |
1780 | } |
1754 | } |
1781 | |
1755 | |
1782 | static int |
1756 | static int |
1783 | file_interactive (SCHEME_P) |
1757 | file_interactive (SCHEME_P) |
1784 | { |
1758 | { |
1785 | #if USE_PORTS |
1759 | #if USE_PORTS |
1786 | return SCHEME_V->file_i == 0 |
1760 | return SCHEME_V->file_i == 0 |
1787 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1761 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1788 | && (SCHEME_V->inport->object.port->kind & port_file); |
1762 | && (port (SCHEME_V->inport)->kind & port_file); |
1789 | #else |
1763 | #else |
1790 | return 0; |
1764 | return 0; |
1791 | #endif |
1765 | #endif |
1792 | } |
1766 | } |
1793 | |
1767 | |
… | |
… | |
1927 | } |
1901 | } |
1928 | |
1902 | |
1929 | static void |
1903 | static void |
1930 | port_close (SCHEME_P_ pointer p, int flag) |
1904 | port_close (SCHEME_P_ pointer p, int flag) |
1931 | { |
1905 | { |
1932 | port *pt = p->object.port; |
1906 | port *pt = port (p); |
1933 | |
1907 | |
1934 | pt->kind &= ~flag; |
1908 | pt->kind &= ~flag; |
1935 | |
1909 | |
1936 | if ((pt->kind & (port_input | port_output)) == 0) |
1910 | if ((pt->kind & (port_input | port_output)) == 0) |
1937 | { |
1911 | { |
… | |
… | |
1958 | /* get new character from input file */ |
1932 | /* get new character from input file */ |
1959 | static int |
1933 | static int |
1960 | inchar (SCHEME_P) |
1934 | inchar (SCHEME_P) |
1961 | { |
1935 | { |
1962 | int c; |
1936 | int c; |
1963 | port *pt; |
1937 | port *pt = port (SCHEME_V->inport); |
1964 | |
|
|
1965 | pt = SCHEME_V->inport->object.port; |
|
|
1966 | |
1938 | |
1967 | if (pt->kind & port_saw_EOF) |
1939 | if (pt->kind & port_saw_EOF) |
1968 | return EOF; |
1940 | return EOF; |
1969 | |
1941 | |
1970 | c = basic_inchar (pt); |
1942 | c = basic_inchar (pt); |
… | |
… | |
2037 | port *pt; |
2009 | port *pt; |
2038 | |
2010 | |
2039 | if (c == EOF) |
2011 | if (c == EOF) |
2040 | return; |
2012 | return; |
2041 | |
2013 | |
2042 | pt = SCHEME_V->inport->object.port; |
2014 | pt = port (SCHEME_V->inport); |
2043 | pt->unget = c; |
2015 | pt->unget = c; |
2044 | #else |
2016 | #else |
2045 | if (c == EOF) |
2017 | if (c == EOF) |
2046 | return; |
2018 | return; |
2047 | |
2019 | |
… | |
… | |
2075 | |
2047 | |
2076 | INTERFACE void |
2048 | INTERFACE void |
2077 | putstr (SCHEME_P_ const char *s) |
2049 | putstr (SCHEME_P_ const char *s) |
2078 | { |
2050 | { |
2079 | #if USE_PORTS |
2051 | #if USE_PORTS |
2080 | port *pt = SCHEME_V->outport->object.port; |
2052 | port *pt = port (SCHEME_V->outport); |
2081 | |
2053 | |
2082 | if (pt->kind & port_file) |
2054 | if (pt->kind & port_file) |
2083 | write (pt->rep.stdio.file, s, strlen (s)); |
2055 | write (pt->rep.stdio.file, s, strlen (s)); |
2084 | else |
2056 | else |
2085 | for (; *s; s++) |
2057 | for (; *s; s++) |
… | |
… | |
2087 | *pt->rep.string.curr++ = *s; |
2059 | *pt->rep.string.curr++ = *s; |
2088 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
2060 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
2089 | *pt->rep.string.curr++ = *s; |
2061 | *pt->rep.string.curr++ = *s; |
2090 | |
2062 | |
2091 | #else |
2063 | #else |
2092 | xwrstr (s); |
2064 | write (pt->rep.stdio.file, s, strlen (s)); |
2093 | #endif |
2065 | #endif |
2094 | } |
2066 | } |
2095 | |
2067 | |
2096 | static void |
2068 | static void |
2097 | putchars (SCHEME_P_ const char *s, int len) |
2069 | putchars (SCHEME_P_ const char *s, int len) |
2098 | { |
2070 | { |
2099 | #if USE_PORTS |
2071 | #if USE_PORTS |
2100 | port *pt = SCHEME_V->outport->object.port; |
2072 | port *pt = port (SCHEME_V->outport); |
2101 | |
2073 | |
2102 | if (pt->kind & port_file) |
2074 | if (pt->kind & port_file) |
2103 | write (pt->rep.stdio.file, s, len); |
2075 | write (pt->rep.stdio.file, s, len); |
2104 | else |
2076 | else |
2105 | { |
2077 | { |
… | |
… | |
2119 | |
2091 | |
2120 | INTERFACE void |
2092 | INTERFACE void |
2121 | putcharacter (SCHEME_P_ int c) |
2093 | putcharacter (SCHEME_P_ int c) |
2122 | { |
2094 | { |
2123 | #if USE_PORTS |
2095 | #if USE_PORTS |
2124 | port *pt = SCHEME_V->outport->object.port; |
2096 | port *pt = port (SCHEME_V->outport); |
2125 | |
2097 | |
2126 | if (pt->kind & port_file) |
2098 | if (pt->kind & port_file) |
2127 | { |
2099 | { |
2128 | char cc = c; |
2100 | char cc = c; |
2129 | write (pt->rep.stdio.file, &cc, 1); |
2101 | write (pt->rep.stdio.file, &cc, 1); |
… | |
… | |
2142 | #endif |
2114 | #endif |
2143 | } |
2115 | } |
2144 | |
2116 | |
2145 | /* read characters up to delimiter, but cater to character constants */ |
2117 | /* read characters up to delimiter, but cater to character constants */ |
2146 | static char * |
2118 | static char * |
2147 | readstr_upto (SCHEME_P_ char *delim) |
2119 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2148 | { |
2120 | { |
2149 | char *p = SCHEME_V->strbuff; |
2121 | char *p = SCHEME_V->strbuff + skip; |
2150 | |
2122 | |
2151 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2123 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2152 | |
2124 | |
2153 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2125 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2154 | *p = 0; |
2126 | *p = 0; |
… | |
… | |
2161 | return SCHEME_V->strbuff; |
2133 | return SCHEME_V->strbuff; |
2162 | } |
2134 | } |
2163 | |
2135 | |
2164 | /* read string expression "xxx...xxx" */ |
2136 | /* read string expression "xxx...xxx" */ |
2165 | static pointer |
2137 | static pointer |
2166 | readstrexp (SCHEME_P) |
2138 | readstrexp (SCHEME_P_ char delim) |
2167 | { |
2139 | { |
2168 | char *p = SCHEME_V->strbuff; |
2140 | char *p = SCHEME_V->strbuff; |
2169 | int c; |
2141 | int c; |
2170 | int c1 = 0; |
2142 | int c1 = 0; |
2171 | enum |
|
|
2172 | { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2143 | enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2173 | |
2144 | |
2174 | for (;;) |
2145 | for (;;) |
2175 | { |
2146 | { |
2176 | c = inchar (SCHEME_A); |
2147 | c = inchar (SCHEME_A); |
2177 | |
2148 | |
… | |
… | |
2179 | return S_F; |
2150 | return S_F; |
2180 | |
2151 | |
2181 | switch (state) |
2152 | switch (state) |
2182 | { |
2153 | { |
2183 | case st_ok: |
2154 | case st_ok: |
2184 | switch (c) |
2155 | 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); |
2156 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2193 | |
2157 | |
2194 | default: |
2158 | if (ecb_expect_false (c == '\\')) |
|
|
2159 | state = st_bsl; |
|
|
2160 | else |
2195 | *p++ = c; |
2161 | *p++ = c; |
2196 | break; |
|
|
2197 | } |
|
|
2198 | |
2162 | |
2199 | break; |
2163 | break; |
2200 | |
2164 | |
2201 | case st_bsl: |
2165 | case st_bsl: |
2202 | switch (c) |
2166 | switch (c) |
… | |
… | |
2232 | case 'r': |
2196 | case 'r': |
2233 | *p++ = '\r'; |
2197 | *p++ = '\r'; |
2234 | state = st_ok; |
2198 | state = st_ok; |
2235 | break; |
2199 | break; |
2236 | |
2200 | |
2237 | case '"': |
|
|
2238 | *p++ = '"'; |
|
|
2239 | state = st_ok; |
|
|
2240 | break; |
|
|
2241 | |
|
|
2242 | default: |
2201 | default: |
2243 | *p++ = c; |
2202 | *p++ = c; |
2244 | state = st_ok; |
2203 | state = st_ok; |
2245 | break; |
2204 | break; |
2246 | } |
2205 | } |
2247 | |
2206 | |
2248 | break; |
2207 | break; |
2249 | |
2208 | |
2250 | case st_x1: |
2209 | case st_x1: |
2251 | case st_x2: |
2210 | case st_x2: |
2252 | c = toupper (c); |
2211 | c = tolower (c); |
2253 | |
2212 | |
2254 | if (c >= '0' && c <= 'F') |
2213 | if (c >= '0' && c <= '9') |
2255 | { |
|
|
2256 | if (c <= '9') |
|
|
2257 | c1 = (c1 << 4) + c - '0'; |
2214 | c1 = (c1 << 4) + c - '0'; |
2258 | else |
2215 | else if (c >= 'a' && c <= 'f') |
2259 | c1 = (c1 << 4) + c - 'A' + 10; |
2216 | 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 |
2217 | else |
2270 | return S_F; |
2218 | return S_F; |
|
|
2219 | |
|
|
2220 | if (state == st_x1) |
|
|
2221 | state = st_x2; |
|
|
2222 | else |
|
|
2223 | { |
|
|
2224 | *p++ = c1; |
|
|
2225 | state = st_ok; |
|
|
2226 | } |
2271 | |
2227 | |
2272 | break; |
2228 | break; |
2273 | |
2229 | |
2274 | case st_oct1: |
2230 | case st_oct1: |
2275 | case st_oct2: |
2231 | case st_oct2: |
… | |
… | |
2279 | backchar (SCHEME_A_ c); |
2235 | backchar (SCHEME_A_ c); |
2280 | state = st_ok; |
2236 | state = st_ok; |
2281 | } |
2237 | } |
2282 | else |
2238 | else |
2283 | { |
2239 | { |
2284 | if (state == st_oct2 && c1 >= 32) |
2240 | if (state == st_oct2 && c1 >= ' ') |
2285 | return S_F; |
2241 | return S_F; |
2286 | |
2242 | |
2287 | c1 = (c1 << 3) + (c - '0'); |
2243 | c1 = (c1 << 3) + (c - '0'); |
2288 | |
2244 | |
2289 | if (state == st_oct1) |
2245 | if (state == st_oct1) |
… | |
… | |
2294 | state = st_ok; |
2250 | state = st_ok; |
2295 | } |
2251 | } |
2296 | } |
2252 | } |
2297 | |
2253 | |
2298 | break; |
2254 | break; |
2299 | |
|
|
2300 | } |
2255 | } |
2301 | } |
2256 | } |
2302 | } |
2257 | } |
2303 | |
2258 | |
2304 | /* check c is in chars */ |
2259 | /* check c is in chars */ |
2305 | ecb_inline int |
2260 | ecb_inline int |
2306 | is_one_of (char *s, int c) |
2261 | is_one_of (const char *s, int c) |
2307 | { |
2262 | { |
2308 | if (c == EOF) |
|
|
2309 | return 1; |
|
|
2310 | |
|
|
2311 | return !!strchr (s, c); |
2263 | return c == EOF || !!strchr (s, c); |
2312 | } |
2264 | } |
2313 | |
2265 | |
2314 | /* skip white characters */ |
2266 | /* skip white characters */ |
2315 | ecb_inline int |
2267 | ecb_inline int |
2316 | skipspace (SCHEME_P) |
2268 | skipspace (SCHEME_P) |
… | |
… | |
2318 | int c, curr_line = 0; |
2270 | int c, curr_line = 0; |
2319 | |
2271 | |
2320 | do |
2272 | do |
2321 | { |
2273 | { |
2322 | c = inchar (SCHEME_A); |
2274 | c = inchar (SCHEME_A); |
|
|
2275 | |
2323 | #if SHOW_ERROR_LINE |
2276 | #if SHOW_ERROR_LINE |
2324 | if (c == '\n') |
2277 | if (ecb_expect_false (c == '\n')) |
2325 | curr_line++; |
2278 | curr_line++; |
2326 | #endif |
2279 | #endif |
|
|
2280 | |
|
|
2281 | if (ecb_expect_false (c == EOF)) |
|
|
2282 | return c; |
2327 | } |
2283 | } |
2328 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2284 | while (is_one_of (WHITESPACE, c)); |
2329 | |
2285 | |
2330 | /* record it */ |
2286 | /* record it */ |
2331 | #if SHOW_ERROR_LINE |
2287 | #if SHOW_ERROR_LINE |
2332 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2288 | 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; |
2289 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2334 | #endif |
2290 | #endif |
2335 | |
2291 | |
2336 | if (c != EOF) |
|
|
2337 | { |
|
|
2338 | backchar (SCHEME_A_ c); |
2292 | backchar (SCHEME_A_ c); |
2339 | return 1; |
2293 | return 1; |
2340 | } |
|
|
2341 | else |
|
|
2342 | return EOF; |
|
|
2343 | } |
2294 | } |
2344 | |
2295 | |
2345 | /* get token */ |
2296 | /* get token */ |
2346 | static int |
2297 | static int |
2347 | token (SCHEME_P) |
2298 | token (SCHEME_P) |
… | |
… | |
2363 | return TOK_RPAREN; |
2314 | return TOK_RPAREN; |
2364 | |
2315 | |
2365 | case '.': |
2316 | case '.': |
2366 | c = inchar (SCHEME_A); |
2317 | c = inchar (SCHEME_A); |
2367 | |
2318 | |
2368 | if (is_one_of (" \n\t", c)) |
2319 | if (is_one_of (WHITESPACE, c)) |
2369 | return TOK_DOT; |
2320 | return TOK_DOT; |
2370 | else |
2321 | else |
2371 | { |
2322 | { |
2372 | //TODO: ungetc twice in a row is not supported in C |
|
|
2373 | backchar (SCHEME_A_ c); |
2323 | backchar (SCHEME_A_ c); |
2374 | backchar (SCHEME_A_ '.'); |
|
|
2375 | return TOK_ATOM; |
2324 | return TOK_DOTATOM; |
2376 | } |
2325 | } |
|
|
2326 | |
|
|
2327 | case '|': |
|
|
2328 | return TOK_STRATOM; |
2377 | |
2329 | |
2378 | case '\'': |
2330 | case '\'': |
2379 | return TOK_QUOTE; |
2331 | return TOK_QUOTE; |
2380 | |
2332 | |
2381 | case ';': |
2333 | case ';': |
… | |
… | |
2513 | } |
2465 | } |
2514 | |
2466 | |
2515 | putcharacter (SCHEME_A_ '"'); |
2467 | putcharacter (SCHEME_A_ '"'); |
2516 | } |
2468 | } |
2517 | |
2469 | |
2518 | |
|
|
2519 | /* print atoms */ |
2470 | /* print atoms */ |
2520 | static void |
2471 | static void |
2521 | printatom (SCHEME_P_ pointer l, int f) |
2472 | printatom (SCHEME_P_ pointer l, int f) |
2522 | { |
2473 | { |
2523 | char *p; |
2474 | char *p; |
2524 | int len; |
2475 | int len; |
2525 | |
2476 | |
2526 | atom2str (SCHEME_A_ l, f, &p, &len); |
2477 | atom2str (SCHEME_A_ l, f, &p, &len); |
2527 | putchars (SCHEME_A_ p, len); |
2478 | putchars (SCHEME_A_ p, len); |
2528 | } |
2479 | } |
2529 | |
|
|
2530 | |
2480 | |
2531 | /* Uses internal buffer unless string pointer is already available */ |
2481 | /* Uses internal buffer unless string pointer is already available */ |
2532 | static void |
2482 | static void |
2533 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2483 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2534 | { |
2484 | { |
… | |
… | |
2695 | #endif |
2645 | #endif |
2696 | } |
2646 | } |
2697 | else if (is_continuation (l)) |
2647 | else if (is_continuation (l)) |
2698 | p = "#<CONTINUATION>"; |
2648 | p = "#<CONTINUATION>"; |
2699 | else |
2649 | else |
|
|
2650 | { |
|
|
2651 | #if USE_PRINTF |
|
|
2652 | p = SCHEME_V->strbuff; |
|
|
2653 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2654 | #else |
2700 | p = "#<ERROR>"; |
2655 | p = "#<ERROR>"; |
|
|
2656 | #endif |
|
|
2657 | } |
2701 | |
2658 | |
2702 | *pp = p; |
2659 | *pp = p; |
2703 | *plen = strlen (p); |
2660 | *plen = strlen (p); |
2704 | } |
2661 | } |
2705 | |
2662 | |
… | |
… | |
2849 | /* () is #t in R5RS */ |
2806 | /* () is #t in R5RS */ |
2850 | #define is_true(p) ((p) != S_F) |
2807 | #define is_true(p) ((p) != S_F) |
2851 | #define is_false(p) ((p) == S_F) |
2808 | #define is_false(p) ((p) == S_F) |
2852 | |
2809 | |
2853 | /* ========== Environment implementation ========== */ |
2810 | /* ========== 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 | |
2811 | |
2870 | #ifndef USE_ALIST_ENV |
2812 | #ifndef USE_ALIST_ENV |
2871 | |
2813 | |
2872 | /* |
2814 | /* |
2873 | * In this implementation, each frame of the environment may be |
2815 | * In this implementation, each frame of the environment may be |
… | |
… | |
2890 | |
2832 | |
2891 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2833 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2892 | setenvironment (SCHEME_V->envir); |
2834 | setenvironment (SCHEME_V->envir); |
2893 | } |
2835 | } |
2894 | |
2836 | |
|
|
2837 | static uint32_t |
|
|
2838 | sym_hash (pointer sym, uint32_t size) |
|
|
2839 | { |
|
|
2840 | uintptr_t ptr = (uintptr_t)sym; |
|
|
2841 | |
|
|
2842 | #if 0 |
|
|
2843 | /* table size is prime, so why mix */ |
|
|
2844 | ptr += ptr >> 32; |
|
|
2845 | ptr += ptr >> 16; |
|
|
2846 | ptr += ptr >> 8; |
|
|
2847 | #endif |
|
|
2848 | |
|
|
2849 | return ptr % size; |
|
|
2850 | } |
|
|
2851 | |
2895 | ecb_inline void |
2852 | ecb_inline void |
2896 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2853 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2897 | { |
2854 | { |
2898 | pointer slot = immutable_cons (variable, value); |
2855 | pointer slot = immutable_cons (variable, value); |
2899 | |
2856 | |
2900 | if (is_vector (car (env))) |
2857 | if (is_vector (car (env))) |
2901 | { |
2858 | { |
2902 | int location = hash_fn (symname (variable), veclength (car (env))); |
2859 | int location = sym_hash (variable, veclength (car (env))); |
2903 | |
|
|
2904 | set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); |
2860 | vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); |
2905 | } |
2861 | } |
2906 | else |
2862 | else |
2907 | set_car (env, immutable_cons (slot, car (env))); |
2863 | set_car (env, immutable_cons (slot, car (env))); |
2908 | } |
2864 | } |
2909 | |
2865 | |
2910 | static pointer |
2866 | static pointer |
2911 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2867 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2912 | { |
2868 | { |
2913 | pointer x, y; |
2869 | pointer x, y; |
2914 | int location; |
|
|
2915 | |
2870 | |
2916 | for (x = env; x != NIL; x = cdr (x)) |
2871 | for (x = env; x != NIL; x = cdr (x)) |
2917 | { |
2872 | { |
2918 | if (is_vector (car (x))) |
2873 | if (is_vector (car (x))) |
2919 | { |
2874 | { |
2920 | location = hash_fn (symname (hdl), veclength (car (x))); |
2875 | int location = sym_hash (hdl, veclength (car (x))); |
2921 | y = vector_elem (car (x), location); |
2876 | y = vector_get (car (x), location); |
2922 | } |
2877 | } |
2923 | else |
2878 | else |
2924 | y = car (x); |
2879 | y = car (x); |
2925 | |
2880 | |
2926 | for (; y != NIL; y = cdr (y)) |
2881 | for (; y != NIL; y = cdr (y)) |
2927 | if (caar (y) == hdl) |
2882 | if (caar (y) == hdl) |
2928 | break; |
2883 | break; |
2929 | |
2884 | |
2930 | if (y != NIL) |
2885 | if (y != NIL) |
|
|
2886 | return car (y); |
|
|
2887 | |
|
|
2888 | if (!all) |
2931 | break; |
2889 | break; |
2932 | |
|
|
2933 | if (!all) |
|
|
2934 | return NIL; |
|
|
2935 | } |
2890 | } |
2936 | |
|
|
2937 | if (x != NIL) |
|
|
2938 | return car (y); |
|
|
2939 | |
2891 | |
2940 | return NIL; |
2892 | return NIL; |
2941 | } |
2893 | } |
2942 | |
2894 | |
2943 | #else /* USE_ALIST_ENV */ |
2895 | #else /* USE_ALIST_ENV */ |
… | |
… | |
2965 | for (y = car (x); y != NIL; y = cdr (y)) |
2917 | for (y = car (x); y != NIL; y = cdr (y)) |
2966 | if (caar (y) == hdl) |
2918 | if (caar (y) == hdl) |
2967 | break; |
2919 | break; |
2968 | |
2920 | |
2969 | if (y != NIL) |
2921 | if (y != NIL) |
|
|
2922 | return car (y); |
2970 | break; |
2923 | break; |
2971 | |
2924 | |
2972 | if (!all) |
2925 | if (!all) |
2973 | return NIL; |
2926 | break; |
2974 | } |
2927 | } |
2975 | |
|
|
2976 | if (x != NIL) |
|
|
2977 | return car (y); |
|
|
2978 | |
2928 | |
2979 | return NIL; |
2929 | return NIL; |
2980 | } |
2930 | } |
2981 | |
2931 | |
2982 | #endif /* USE_ALIST_ENV else */ |
2932 | #endif /* USE_ALIST_ENV else */ |
2983 | |
2933 | |
2984 | ecb_inline void |
2934 | ecb_inline void |
2985 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2935 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2986 | { |
2936 | { |
|
|
2937 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2987 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2938 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2988 | } |
2939 | } |
2989 | |
2940 | |
2990 | ecb_inline void |
2941 | ecb_inline void |
2991 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2942 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
… | |
… | |
3092 | { |
3043 | { |
3093 | int nframes = (uintptr_t)SCHEME_V->dump; |
3044 | int nframes = (uintptr_t)SCHEME_V->dump; |
3094 | struct dump_stack_frame *next_frame; |
3045 | struct dump_stack_frame *next_frame; |
3095 | |
3046 | |
3096 | /* enough room for the next frame? */ |
3047 | /* enough room for the next frame? */ |
3097 | if (nframes >= SCHEME_V->dump_size) |
3048 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3098 | { |
3049 | { |
3099 | SCHEME_V->dump_size += STACK_GROWTH; |
3050 | 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); |
3051 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3101 | } |
3052 | } |
3102 | |
3053 | |
… | |
… | |
3283 | |
3234 | |
3284 | #endif |
3235 | #endif |
3285 | |
3236 | |
3286 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3237 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3287 | |
3238 | |
|
|
3239 | #if EXPERIMENT |
|
|
3240 | static int |
|
|
3241 | debug (SCHEME_P_ int indent, pointer x) |
|
|
3242 | { |
|
|
3243 | int c; |
|
|
3244 | |
|
|
3245 | if (is_syntax (x)) |
|
|
3246 | { |
|
|
3247 | printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x)); |
|
|
3248 | return 8 + 8; |
|
|
3249 | } |
|
|
3250 | |
|
|
3251 | if (x == NIL) |
|
|
3252 | { |
|
|
3253 | printf ("%*sNIL\n", indent, ""); |
|
|
3254 | return 3; |
|
|
3255 | } |
|
|
3256 | |
|
|
3257 | switch (type (x)) |
|
|
3258 | { |
|
|
3259 | case T_INTEGER: |
|
|
3260 | printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x); |
|
|
3261 | return 32+8; |
|
|
3262 | |
|
|
3263 | case T_SYMBOL: |
|
|
3264 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
|
|
3265 | return 24+8; |
|
|
3266 | |
|
|
3267 | case T_CLOSURE: |
|
|
3268 | printf ("%*sS<%s>\n", indent, "", "closure"); |
|
|
3269 | debug (SCHEME_A_ indent + 3, cdr(x)); |
|
|
3270 | return 32 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3271 | |
|
|
3272 | case T_PAIR: |
|
|
3273 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
|
|
3274 | c = debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3275 | c += debug (SCHEME_A_ indent + 3, cdr (x)); |
|
|
3276 | return c + 1; |
|
|
3277 | |
|
|
3278 | case T_PORT: |
|
|
3279 | printf ("%*sS<%s>\n", indent, "", "port"); |
|
|
3280 | return 24+8; |
|
|
3281 | |
|
|
3282 | case T_VECTOR: |
|
|
3283 | printf ("%*sS<%s>\n", indent, "", "vector"); |
|
|
3284 | return 24+8; |
|
|
3285 | |
|
|
3286 | case T_ENVIRONMENT: |
|
|
3287 | printf ("%*sS<%s>\n", indent, "", "environment"); |
|
|
3288 | return 0 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3289 | |
|
|
3290 | default: |
|
|
3291 | printf ("unhandled type %d\n", type (x)); |
|
|
3292 | break; |
|
|
3293 | } |
|
|
3294 | } |
|
|
3295 | #endif |
|
|
3296 | |
3288 | static int |
3297 | static int |
3289 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3298 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3290 | { |
3299 | { |
3291 | pointer args = SCHEME_V->args; |
3300 | pointer args = SCHEME_V->args; |
3292 | pointer x, y; |
3301 | pointer x, y; |
3293 | |
3302 | |
3294 | switch (op) |
3303 | switch (op) |
3295 | { |
3304 | { |
|
|
3305 | #if EXPERIMENT //D |
|
|
3306 | case OP_DEBUG: |
|
|
3307 | printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); |
|
|
3308 | printf ("\n"); |
|
|
3309 | s_return (S_T); |
|
|
3310 | #endif |
3296 | case OP_LOAD: /* load */ |
3311 | case OP_LOAD: /* load */ |
3297 | if (file_interactive (SCHEME_A)) |
3312 | if (file_interactive (SCHEME_A)) |
3298 | { |
3313 | { |
3299 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3314 | 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))); |
3315 | //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3301 | } |
3316 | } |
3302 | |
3317 | |
3303 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3318 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3304 | Error_1 ("unable to open", car (args)); |
3319 | Error_1 ("unable to open", car (args)); |
3305 | else |
3320 | else |
… | |
… | |
3309 | } |
3324 | } |
3310 | |
3325 | |
3311 | case OP_T0LVL: /* top level */ |
3326 | case OP_T0LVL: /* top level */ |
3312 | |
3327 | |
3313 | /* If we reached the end of file, this loop is done. */ |
3328 | /* If we reached the end of file, this loop is done. */ |
3314 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3329 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3315 | { |
3330 | { |
3316 | if (SCHEME_V->file_i == 0) |
3331 | if (SCHEME_V->file_i == 0) |
3317 | { |
3332 | { |
3318 | SCHEME_V->args = NIL; |
3333 | SCHEME_V->args = NIL; |
3319 | s_goto (OP_QUIT); |
3334 | s_goto (OP_QUIT); |
… | |
… | |
3397 | #endif |
3412 | #endif |
3398 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3413 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3399 | { |
3414 | { |
3400 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3415 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3401 | |
3416 | |
3402 | if (x != NIL) |
3417 | if (x == NIL) |
3403 | s_return (slot_value_in_env (x)); |
|
|
3404 | else |
|
|
3405 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3418 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
|
|
3419 | |
|
|
3420 | s_return (slot_value_in_env (x)); |
3406 | } |
3421 | } |
3407 | else if (is_pair (SCHEME_V->code)) |
3422 | else if (is_pair (SCHEME_V->code)) |
3408 | { |
3423 | { |
3409 | x = car (SCHEME_V->code); |
3424 | x = car (SCHEME_V->code); |
3410 | |
3425 | |
… | |
… | |
3423 | } |
3438 | } |
3424 | else |
3439 | else |
3425 | s_return (SCHEME_V->code); |
3440 | s_return (SCHEME_V->code); |
3426 | |
3441 | |
3427 | case OP_E0ARGS: /* eval arguments */ |
3442 | case OP_E0ARGS: /* eval arguments */ |
3428 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3443 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3429 | { |
3444 | { |
3430 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3445 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3431 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3446 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3432 | SCHEME_V->code = SCHEME_V->value; |
3447 | SCHEME_V->code = SCHEME_V->value; |
3433 | s_goto (OP_APPLY); |
3448 | s_goto (OP_APPLY); |
… | |
… | |
3487 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3502 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3488 | else if (is_foreign (SCHEME_V->code)) |
3503 | else if (is_foreign (SCHEME_V->code)) |
3489 | { |
3504 | { |
3490 | /* Keep nested calls from GC'ing the arglist */ |
3505 | /* Keep nested calls from GC'ing the arglist */ |
3491 | push_recent_alloc (SCHEME_A_ args, NIL); |
3506 | push_recent_alloc (SCHEME_A_ args, NIL); |
3492 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3507 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3493 | |
3508 | |
3494 | s_return (x); |
3509 | s_return (x); |
3495 | } |
3510 | } |
3496 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3511 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3497 | { |
3512 | { |
… | |
… | |
3534 | |
3549 | |
3535 | case OP_DOMACRO: /* do macro */ |
3550 | case OP_DOMACRO: /* do macro */ |
3536 | SCHEME_V->code = SCHEME_V->value; |
3551 | SCHEME_V->code = SCHEME_V->value; |
3537 | s_goto (OP_EVAL); |
3552 | s_goto (OP_EVAL); |
3538 | |
3553 | |
3539 | #if 1 |
|
|
3540 | |
|
|
3541 | case OP_LAMBDA: /* lambda */ |
3554 | case OP_LAMBDA: /* lambda */ |
3542 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3555 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3543 | set SCHEME_V->value fall thru */ |
3556 | set SCHEME_V->value fall thru */ |
3544 | { |
3557 | { |
3545 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3558 | 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); |
3564 | SCHEME_V->code = slot_value_in_env (f); |
3552 | s_goto (OP_APPLY); |
3565 | s_goto (OP_APPLY); |
3553 | } |
3566 | } |
3554 | |
3567 | |
3555 | SCHEME_V->value = SCHEME_V->code; |
3568 | SCHEME_V->value = SCHEME_V->code; |
3556 | /* Fallthru */ |
|
|
3557 | } |
3569 | } |
|
|
3570 | /* Fallthru */ |
3558 | |
3571 | |
3559 | case OP_LAMBDA1: |
3572 | case OP_LAMBDA1: |
3560 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3573 | 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 | |
3574 | |
3569 | case OP_MKCLOSURE: /* make-closure */ |
3575 | case OP_MKCLOSURE: /* make-closure */ |
3570 | x = car (args); |
3576 | x = car (args); |
3571 | |
3577 | |
3572 | if (car (x) == SCHEME_V->LAMBDA) |
3578 | if (car (x) == SCHEME_V->LAMBDA) |
… | |
… | |
3993 | |
3999 | |
3994 | switch (op) |
4000 | switch (op) |
3995 | { |
4001 | { |
3996 | #if USE_MATH |
4002 | #if USE_MATH |
3997 | case OP_INEX2EX: /* inexact->exact */ |
4003 | case OP_INEX2EX: /* inexact->exact */ |
3998 | { |
|
|
3999 | if (is_integer (x)) |
4004 | if (!is_integer (x)) |
4000 | s_return (x); |
4005 | { |
4001 | |
|
|
4002 | RVALUE r = rvalue_unchecked (x); |
4006 | RVALUE r = rvalue_unchecked (x); |
4003 | |
4007 | |
4004 | if (r == (RVALUE)(IVALUE)r) |
4008 | if (r == (RVALUE)(IVALUE)r) |
4005 | s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); |
4009 | x = mk_integer (SCHEME_A_ rvalue_unchecked (x)); |
4006 | else |
4010 | else |
4007 | Error_1 ("inexact->exact: not integral:", x); |
4011 | Error_1 ("inexact->exact: not integral:", x); |
4008 | } |
4012 | } |
|
|
4013 | |
|
|
4014 | s_return (x); |
4009 | |
4015 | |
4010 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4016 | 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)))); |
4017 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)) |
|
|
4018 | / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args)))))); |
4012 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4019 | 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)))); |
4020 | 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)))); |
4021 | 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)))); |
4022 | 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)))); |
4023 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4017 | |
4024 | |
4018 | case OP_ATAN: |
4025 | case OP_ATAN: |
|
|
4026 | s_return (mk_real (SCHEME_A_ |
4019 | if (cdr (args) == NIL) |
4027 | cdr (args) == NIL |
4020 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4028 | ? atan (rvalue (x)) |
4021 | else |
4029 | : 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 | |
4030 | |
4027 | case OP_SQRT: |
4031 | case OP_SQRT: |
4028 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
4032 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
4029 | |
4033 | |
4030 | case OP_EXPT: |
4034 | case OP_EXPT: |
… | |
… | |
4057 | s_return (mk_real (SCHEME_A_ result)); |
4061 | s_return (mk_real (SCHEME_A_ result)); |
4058 | else |
4062 | else |
4059 | s_return (mk_integer (SCHEME_A_ result)); |
4063 | s_return (mk_integer (SCHEME_A_ result)); |
4060 | } |
4064 | } |
4061 | |
4065 | |
4062 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
4066 | 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)))); |
4067 | 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)))); |
4068 | case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x)))); |
|
|
4069 | case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x)))); |
4076 | #endif |
4070 | #endif |
4077 | |
4071 | |
4078 | case OP_ADD: /* + */ |
4072 | case OP_ADD: /* + */ |
4079 | v = num_zero; |
4073 | v = num_zero; |
4080 | |
4074 | |
… | |
… | |
4168 | else |
4162 | else |
4169 | Error_0 ("modulo: division by zero"); |
4163 | Error_0 ("modulo: division by zero"); |
4170 | |
4164 | |
4171 | s_return (mk_number (SCHEME_A_ v)); |
4165 | s_return (mk_number (SCHEME_A_ v)); |
4172 | |
4166 | |
4173 | case OP_CAR: /* car */ |
4167 | /* the compiler will optimize this mess... */ |
4174 | s_return (caar (args)); |
4168 | case OP_CAR: op_car: s_return (car (x)); |
4175 | |
4169 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4176 | case OP_CDR: /* cdr */ |
4170 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4177 | s_return (cdar (args)); |
4171 | case OP_CADR: op_cadr: x = cdr (x); goto op_car; |
|
|
4172 | case OP_CDAR: op_cdar: x = car (x); goto op_cdr; |
|
|
4173 | case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr; |
|
|
4174 | case OP_CAAAR: op_caaar: x = car (x); goto op_caar; |
|
|
4175 | case OP_CAADR: op_caadr: x = cdr (x); goto op_caar; |
|
|
4176 | case OP_CADAR: op_cadar: x = car (x); goto op_cadr; |
|
|
4177 | case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr; |
|
|
4178 | case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar; |
|
|
4179 | case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar; |
|
|
4180 | case OP_CDDAR: op_cddar: x = car (x); goto op_cddr; |
|
|
4181 | case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr; |
|
|
4182 | case OP_CAAAAR: x = car (x); goto op_caaar; |
|
|
4183 | case OP_CAAADR: x = cdr (x); goto op_caaar; |
|
|
4184 | case OP_CAADAR: x = car (x); goto op_caadr; |
|
|
4185 | case OP_CAADDR: x = cdr (x); goto op_caadr; |
|
|
4186 | case OP_CADAAR: x = car (x); goto op_cadar; |
|
|
4187 | case OP_CADADR: x = cdr (x); goto op_cadar; |
|
|
4188 | case OP_CADDAR: x = car (x); goto op_caddr; |
|
|
4189 | case OP_CADDDR: x = cdr (x); goto op_caddr; |
|
|
4190 | case OP_CDAAAR: x = car (x); goto op_cdaar; |
|
|
4191 | case OP_CDAADR: x = cdr (x); goto op_cdaar; |
|
|
4192 | case OP_CDADAR: x = car (x); goto op_cdadr; |
|
|
4193 | case OP_CDADDR: x = cdr (x); goto op_cdadr; |
|
|
4194 | case OP_CDDAAR: x = car (x); goto op_cddar; |
|
|
4195 | case OP_CDDADR: x = cdr (x); goto op_cddar; |
|
|
4196 | case OP_CDDDAR: x = car (x); goto op_cdddr; |
|
|
4197 | case OP_CDDDDR: x = cdr (x); goto op_cdddr; |
4178 | |
4198 | |
4179 | case OP_CONS: /* cons */ |
4199 | case OP_CONS: /* cons */ |
4180 | set_cdr (args, cadr (args)); |
4200 | set_cdr (args, cadr (args)); |
4181 | s_return (args); |
4201 | s_return (args); |
4182 | |
4202 | |
… | |
… | |
4401 | if (SCHEME_V->no_memory) |
4421 | if (SCHEME_V->no_memory) |
4402 | s_return (S_SINK); |
4422 | s_return (S_SINK); |
4403 | #endif |
4423 | #endif |
4404 | |
4424 | |
4405 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4425 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4406 | set_vector_elem (vec, i, car (x)); |
4426 | vector_set (vec, i, car (x)); |
4407 | |
4427 | |
4408 | s_return (vec); |
4428 | s_return (vec); |
4409 | } |
4429 | } |
4410 | |
4430 | |
4411 | case OP_MKVECTOR: /* make-vector */ |
4431 | case OP_MKVECTOR: /* make-vector */ |
… | |
… | |
4423 | if (SCHEME_V->no_memory) |
4443 | if (SCHEME_V->no_memory) |
4424 | s_return (S_SINK); |
4444 | s_return (S_SINK); |
4425 | #endif |
4445 | #endif |
4426 | |
4446 | |
4427 | if (fill != NIL) |
4447 | if (fill != NIL) |
4428 | fill_vector (vec, fill); |
4448 | fill_vector (vec, 0, fill); |
4429 | |
4449 | |
4430 | s_return (vec); |
4450 | s_return (vec); |
4431 | } |
4451 | } |
4432 | |
4452 | |
4433 | case OP_VECLEN: /* vector-length */ |
4453 | case OP_VECLEN: /* vector-length */ |
4434 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4454 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
|
|
4455 | |
|
|
4456 | case OP_VECRESIZE: |
|
|
4457 | vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args)); |
|
|
4458 | s_return (x); |
4435 | |
4459 | |
4436 | case OP_VECREF: /* vector-ref */ |
4460 | case OP_VECREF: /* vector-ref */ |
4437 | { |
4461 | { |
4438 | int index = ivalue_unchecked (cadr (args)); |
4462 | int index = ivalue_unchecked (cadr (args)); |
4439 | |
4463 | |
4440 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4464 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4441 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4465 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4442 | |
4466 | |
4443 | s_return (vector_elem (x, index)); |
4467 | s_return (vector_get (x, index)); |
4444 | } |
4468 | } |
4445 | |
4469 | |
4446 | case OP_VECSET: /* vector-set! */ |
4470 | case OP_VECSET: /* vector-set! */ |
4447 | { |
4471 | { |
4448 | int index = ivalue_unchecked (cadr (args)); |
4472 | int index = ivalue_unchecked (cadr (args)); |
… | |
… | |
4451 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4475 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4452 | |
4476 | |
4453 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4477 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4454 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4478 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4455 | |
4479 | |
4456 | set_vector_elem (x, index, caddr (args)); |
4480 | vector_set (x, index, caddr (args)); |
4457 | s_return (x); |
4481 | s_return (x); |
4458 | } |
4482 | } |
4459 | } |
4483 | } |
4460 | |
4484 | |
4461 | if (USE_ERROR_CHECKING) abort (); |
4485 | if (USE_ERROR_CHECKING) abort (); |
… | |
… | |
4500 | pointer d = cdr (args); |
4524 | pointer d = cdr (args); |
4501 | int r; |
4525 | int r; |
4502 | |
4526 | |
4503 | switch (op) |
4527 | switch (op) |
4504 | { |
4528 | { |
4505 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4529 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4506 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; |
4530 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break; |
4507 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4531 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4508 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4532 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4509 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
4533 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
|
|
4534 | case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break; |
4510 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4535 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4511 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4536 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4512 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4537 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4513 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4538 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4514 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4539 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4515 | |
4540 | |
4516 | #if USE_CHAR_CLASSIFIERS |
4541 | #if USE_CHAR_CLASSIFIERS |
4517 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4542 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4518 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4543 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4519 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4544 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
… | |
… | |
4569 | } |
4594 | } |
4570 | else |
4595 | else |
4571 | s_return (SCHEME_V->code); |
4596 | s_return (SCHEME_V->code); |
4572 | |
4597 | |
4573 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4598 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4574 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4599 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4575 | s_return (SCHEME_V->value); |
4600 | s_return (SCHEME_V->value); |
4576 | |
4601 | |
4577 | #if USE_PORTS |
4602 | #if USE_PORTS |
4578 | |
4603 | |
4579 | case OP_WRITE: /* write */ |
4604 | case OP_WRITE: /* write */ |
… | |
… | |
4724 | SCHEME_V->gc_verbose = (a != S_F); |
4749 | SCHEME_V->gc_verbose = (a != S_F); |
4725 | s_retbool (was); |
4750 | s_retbool (was); |
4726 | } |
4751 | } |
4727 | |
4752 | |
4728 | case OP_NEWSEGMENT: /* new-segment */ |
4753 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
4754 | #if 0 |
4729 | if (!is_pair (args) || !is_number (a)) |
4755 | if (!is_pair (args) || !is_number (a)) |
4730 | Error_0 ("new-segment: argument must be a number"); |
4756 | Error_0 ("new-segment: argument must be a number"); |
4731 | |
4757 | #endif |
4732 | alloc_cellseg (SCHEME_A_ ivalue (a)); |
4758 | s_retbool (alloc_cellseg (SCHEME_A)); |
4733 | |
|
|
4734 | s_return (S_T); |
|
|
4735 | |
4759 | |
4736 | case OP_OBLIST: /* oblist */ |
4760 | case OP_OBLIST: /* oblist */ |
4737 | s_return (oblist_all_symbols (SCHEME_A)); |
4761 | s_return (oblist_all_symbols (SCHEME_A)); |
4738 | |
4762 | |
4739 | #if USE_PORTS |
4763 | #if USE_PORTS |
… | |
… | |
4809 | s_return (p == NIL ? S_F : p); |
4833 | s_return (p == NIL ? S_F : p); |
4810 | } |
4834 | } |
4811 | |
4835 | |
4812 | case OP_GET_OUTSTRING: /* get-output-string */ |
4836 | case OP_GET_OUTSTRING: /* get-output-string */ |
4813 | { |
4837 | { |
4814 | port *p; |
4838 | port *p = port (a); |
4815 | |
4839 | |
4816 | if ((p = a->object.port)->kind & port_string) |
4840 | if (p->kind & port_string) |
4817 | { |
4841 | { |
4818 | off_t size; |
4842 | off_t size; |
4819 | char *str; |
4843 | char *str; |
4820 | |
4844 | |
4821 | size = p->rep.string.curr - p->rep.string.start + 1; |
4845 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4926 | int res; |
4950 | int res; |
4927 | |
4951 | |
4928 | if (is_pair (args)) |
4952 | if (is_pair (args)) |
4929 | p = car (args); |
4953 | p = car (args); |
4930 | |
4954 | |
4931 | res = p->object.port->kind & port_string; |
4955 | res = port (p)->kind & port_string; |
4932 | |
4956 | |
4933 | s_retbool (res); |
4957 | s_retbool (res); |
4934 | } |
4958 | } |
4935 | |
4959 | |
4936 | case OP_SET_INPORT: /* set-input-port */ |
4960 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
4995 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5019 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
4996 | SCHEME_V->tok = token (SCHEME_A); |
5020 | SCHEME_V->tok = token (SCHEME_A); |
4997 | s_goto (OP_RDSEXPR); |
5021 | s_goto (OP_RDSEXPR); |
4998 | |
5022 | |
4999 | case TOK_ATOM: |
5023 | case TOK_ATOM: |
5000 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); |
5024 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); |
|
|
5025 | |
|
|
5026 | case TOK_DOTATOM: |
|
|
5027 | SCHEME_V->strbuff[0] = '.'; |
|
|
5028 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
|
|
5029 | |
|
|
5030 | case TOK_STRATOM: |
|
|
5031 | x = readstrexp (SCHEME_A_ '|'); |
|
|
5032 | //TODO: haven't checked whether the garbage collector could interfere |
|
|
5033 | s_return (mk_atom (SCHEME_A_ strvalue (x))); |
5001 | |
5034 | |
5002 | case TOK_DQUOTE: |
5035 | case TOK_DQUOTE: |
5003 | x = readstrexp (SCHEME_A); |
5036 | x = readstrexp (SCHEME_A_ '"'); |
5004 | |
5037 | |
5005 | if (x == S_F) |
5038 | if (x == S_F) |
5006 | Error_0 ("Error reading string"); |
5039 | Error_0 ("Error reading string"); |
5007 | |
5040 | |
5008 | setimmutable (x); |
5041 | setimmutable (x); |
… | |
… | |
5020 | s_goto (OP_EVAL); |
5053 | s_goto (OP_EVAL); |
5021 | } |
5054 | } |
5022 | } |
5055 | } |
5023 | |
5056 | |
5024 | case TOK_SHARP_CONST: |
5057 | case TOK_SHARP_CONST: |
5025 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) |
5058 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) |
5026 | Error_0 ("undefined sharp expression"); |
5059 | Error_0 ("undefined sharp expression"); |
5027 | else |
5060 | else |
5028 | s_return (x); |
5061 | s_return (x); |
5029 | |
5062 | |
5030 | default: |
5063 | default: |
… | |
… | |
5182 | putstr (SCHEME_A_ ")"); |
5215 | putstr (SCHEME_A_ ")"); |
5183 | s_return (S_T); |
5216 | s_return (S_T); |
5184 | } |
5217 | } |
5185 | else |
5218 | else |
5186 | { |
5219 | { |
5187 | pointer elem = vector_elem (vec, i); |
5220 | pointer elem = vector_get (vec, i); |
5188 | |
5221 | |
5189 | ivalue_unchecked (cdr (args)) = i + 1; |
5222 | ivalue_unchecked (cdr (args)) = i + 1; |
5190 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5223 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5191 | SCHEME_V->args = elem; |
5224 | SCHEME_V->args = elem; |
5192 | |
5225 | |
… | |
… | |
5252 | |
5285 | |
5253 | case OP_CLOSUREP: /* closure? */ |
5286 | case OP_CLOSUREP: /* closure? */ |
5254 | /* |
5287 | /* |
5255 | * Note, macro object is also a closure. |
5288 | * Note, macro object is also a closure. |
5256 | * Therefore, (closure? <#MACRO>) ==> #t |
5289 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5290 | * (schmorp) well, obviously not, fix? TODO |
5257 | */ |
5291 | */ |
5258 | s_retbool (is_closure (a)); |
5292 | s_retbool (is_closure (a)); |
5259 | |
5293 | |
5260 | case OP_MACROP: /* macro? */ |
5294 | case OP_MACROP: /* macro? */ |
5261 | s_retbool (is_macro (a)); |
5295 | s_retbool (is_macro (a)); |
… | |
… | |
5430 | { |
5464 | { |
5431 | if (!tests[j - 1].fct (arg)) |
5465 | if (!tests[j - 1].fct (arg)) |
5432 | break; |
5466 | break; |
5433 | } |
5467 | } |
5434 | |
5468 | |
5435 | if (t[1]) /* last test is replicated as necessary */ |
5469 | if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */ |
5436 | t++; |
5470 | t++; |
5437 | |
5471 | |
5438 | arglist = cdr (arglist); |
5472 | arglist = cdr (arglist); |
5439 | i++; |
5473 | i++; |
5440 | } |
5474 | } |
… | |
… | |
5466 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5500 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5467 | return; |
5501 | return; |
5468 | |
5502 | |
5469 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5503 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5470 | { |
5504 | { |
5471 | xwrstr ("No memory!\n"); |
5505 | putstr (SCHEME_A_ "No memory!\n"); |
5472 | return; |
5506 | return; |
5473 | } |
5507 | } |
5474 | } |
5508 | } |
5475 | } |
5509 | } |
5476 | |
5510 | |
… | |
… | |
5502 | |
5536 | |
5503 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5537 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5504 | static int |
5538 | static int |
5505 | syntaxnum (pointer p) |
5539 | syntaxnum (pointer p) |
5506 | { |
5540 | { |
5507 | const char *s = strvalue (car (p)); |
5541 | const char *s = strvalue (p); |
5508 | |
5542 | |
5509 | switch (strlength (car (p))) |
5543 | switch (strlength (p)) |
5510 | { |
5544 | { |
5511 | case 2: |
5545 | case 2: |
5512 | if (s[0] == 'i') |
5546 | if (s[0] == 'i') |
5513 | return OP_IF0; /* if */ |
5547 | return OP_IF0; /* if */ |
5514 | else |
5548 | else |
… | |
… | |
5589 | ecb_cold int |
5623 | ecb_cold int |
5590 | scheme_init (SCHEME_P) |
5624 | scheme_init (SCHEME_P) |
5591 | { |
5625 | { |
5592 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5626 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5593 | pointer x; |
5627 | pointer x; |
|
|
5628 | |
|
|
5629 | /* this memset is not strictly correct, as we assume (intcache) |
|
|
5630 | * that memset 0 will also set pointers to 0, but memset does |
|
|
5631 | * of course not guarantee that. screw such systems. |
|
|
5632 | */ |
|
|
5633 | memset (SCHEME_V, 0, sizeof (*SCHEME_V)); |
5594 | |
5634 | |
5595 | num_set_fixnum (num_zero, 1); |
5635 | num_set_fixnum (num_zero, 1); |
5596 | num_set_ivalue (num_zero, 0); |
5636 | num_set_ivalue (num_zero, 0); |
5597 | num_set_fixnum (num_one, 1); |
5637 | num_set_fixnum (num_one, 1); |
5598 | num_set_ivalue (num_one, 1); |
5638 | num_set_ivalue (num_one, 1); |
… | |
… | |
5610 | SCHEME_V->save_inport = NIL; |
5650 | SCHEME_V->save_inport = NIL; |
5611 | SCHEME_V->loadport = NIL; |
5651 | SCHEME_V->loadport = NIL; |
5612 | SCHEME_V->nesting = 0; |
5652 | SCHEME_V->nesting = 0; |
5613 | SCHEME_V->interactive_repl = 0; |
5653 | SCHEME_V->interactive_repl = 0; |
5614 | |
5654 | |
5615 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5655 | if (!alloc_cellseg (SCHEME_A)) |
5616 | { |
5656 | { |
5617 | #if USE_ERROR_CHECKING |
5657 | #if USE_ERROR_CHECKING |
5618 | SCHEME_V->no_memory = 1; |
5658 | SCHEME_V->no_memory = 1; |
5619 | return 0; |
5659 | return 0; |
5620 | #endif |
5660 | #endif |
… | |
… | |
5950 | # endif |
5990 | # endif |
5951 | int fin; |
5991 | int fin; |
5952 | char *file_name = InitFile; |
5992 | char *file_name = InitFile; |
5953 | int retcode; |
5993 | int retcode; |
5954 | int isfile = 1; |
5994 | int isfile = 1; |
|
|
5995 | system ("ps v $PPID");//D |
5955 | |
5996 | |
5956 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5997 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5957 | { |
5998 | { |
5958 | xwrstr ("Usage: tinyscheme -?\n"); |
5999 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
5959 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6000 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
5960 | xwrstr ("followed by\n"); |
6001 | putstr (SCHEME_A_ "followed by\n"); |
5961 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6002 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
5962 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6003 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
5963 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6004 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
5964 | xwrstr ("Use - as filename for stdin.\n"); |
6005 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
5965 | return 1; |
6006 | return 1; |
5966 | } |
6007 | } |
5967 | |
6008 | |
5968 | if (!scheme_init (SCHEME_A)) |
6009 | if (!scheme_init (SCHEME_A)) |
5969 | { |
6010 | { |
5970 | xwrstr ("Could not initialize!\n"); |
6011 | putstr (SCHEME_A_ "Could not initialize!\n"); |
5971 | return 2; |
6012 | return 2; |
5972 | } |
6013 | } |
5973 | |
6014 | |
5974 | # if USE_PORTS |
6015 | # if USE_PORTS |
5975 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6016 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
6020 | fin = open (file_name, O_RDONLY); |
6061 | fin = open (file_name, O_RDONLY); |
6021 | #endif |
6062 | #endif |
6022 | |
6063 | |
6023 | if (isfile && fin < 0) |
6064 | if (isfile && fin < 0) |
6024 | { |
6065 | { |
6025 | xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); |
6066 | putstr (SCHEME_A_ "Could not open file "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); |
6026 | } |
6067 | } |
6027 | else |
6068 | else |
6028 | { |
6069 | { |
6029 | if (isfile) |
6070 | if (isfile) |
6030 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6071 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
… | |
… | |
6034 | #if USE_PORTS |
6075 | #if USE_PORTS |
6035 | if (!isfile || fin != STDIN_FILENO) |
6076 | if (!isfile || fin != STDIN_FILENO) |
6036 | { |
6077 | { |
6037 | if (SCHEME_V->retcode != 0) |
6078 | if (SCHEME_V->retcode != 0) |
6038 | { |
6079 | { |
6039 | xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); |
6080 | putstr (SCHEME_A_ "Errors encountered reading "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); |
6040 | } |
6081 | } |
6041 | |
6082 | |
6042 | if (isfile) |
6083 | if (isfile) |
6043 | close (fin); |
6084 | close (fin); |
6044 | } |
6085 | } |