… | |
… | |
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> |
… | |
… | |
74 | TOK_SHARP_CONST, |
78 | TOK_SHARP_CONST, |
75 | TOK_VEC |
79 | TOK_VEC |
76 | }; |
80 | }; |
77 | |
81 | |
78 | #define BACKQUOTE '`' |
82 | #define BACKQUOTE '`' |
79 | #define DELIMITERS "()\";\f\t\v\n\r " |
83 | #define WHITESPACE " \t\r\n\v\f" |
|
|
84 | #define DELIMITERS "()\";" WHITESPACE |
80 | |
85 | |
81 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
86 | #define NIL POINTER (&SCHEME_V->xNIL) |
82 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
87 | #define S_T POINTER (&SCHEME_V->xT) |
83 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
88 | #define S_F POINTER (&SCHEME_V->xF) |
84 | #define S_SINK (&SCHEME_V->xsink) |
89 | #define S_SINK POINTER (&SCHEME_V->xsink) |
85 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
90 | #define S_EOF POINTER (&SCHEME_V->xEOF_OBJ) |
86 | |
91 | |
87 | #if !USE_MULTIPLICITY |
92 | #if !USE_MULTIPLICITY |
88 | static scheme sc; |
93 | static scheme sc; |
89 | #endif |
94 | #endif |
90 | |
95 | |
… | |
… | |
98 | } |
103 | } |
99 | |
104 | |
100 | char *p = s; |
105 | char *p = s; |
101 | |
106 | |
102 | do { |
107 | do { |
103 | *p++ = '0' + n % base; |
108 | *p++ = "0123456789abcdef"[n % base]; |
104 | n /= base; |
109 | n /= base; |
105 | } while (n); |
110 | } while (n); |
106 | |
111 | |
107 | *p-- = 0; |
112 | *p-- = 0; |
108 | |
113 | |
… | |
… | |
118 | { |
123 | { |
119 | xbase (s, n, 10); |
124 | xbase (s, n, 10); |
120 | } |
125 | } |
121 | |
126 | |
122 | static void |
127 | static void |
123 | xwrstr (const char *s) |
128 | putnum (SCHEME_P_ long n) |
124 | { |
|
|
125 | write (1, s, strlen (s)); |
|
|
126 | } |
|
|
127 | |
|
|
128 | static void |
|
|
129 | xwrnum (long n) |
|
|
130 | { |
129 | { |
131 | char buf[64]; |
130 | char buf[64]; |
132 | |
131 | |
133 | xnum (buf, n); |
132 | xnum (buf, n); |
134 | xwrstr (buf); |
133 | putstr (SCHEME_A_ buf); |
135 | } |
134 | } |
136 | |
135 | |
137 | static char |
136 | static char |
138 | xtoupper (char c) |
137 | xtoupper (char c) |
139 | { |
138 | { |
… | |
… | |
191 | |
190 | |
192 | #ifndef InitFile |
191 | #ifndef InitFile |
193 | # define InitFile "init.scm" |
192 | # define InitFile "init.scm" |
194 | #endif |
193 | #endif |
195 | |
194 | |
196 | #ifndef FIRST_CELLSEGS |
|
|
197 | # define FIRST_CELLSEGS 3 |
|
|
198 | #endif |
|
|
199 | |
|
|
200 | enum scheme_types |
195 | enum scheme_types |
201 | { |
196 | { |
202 | T_INTEGER, |
197 | T_INTEGER, |
|
|
198 | T_CHARACTER, |
203 | T_REAL, |
199 | T_REAL, |
204 | T_STRING, |
200 | T_STRING, |
205 | T_SYMBOL, |
201 | T_SYMBOL, |
206 | T_PROC, |
202 | T_PROC, |
207 | T_PAIR, /* also used for free cells */ |
203 | T_PAIR, /* also used for free cells */ |
208 | T_CLOSURE, |
204 | T_CLOSURE, |
|
|
205 | T_MACRO, |
209 | T_CONTINUATION, |
206 | T_CONTINUATION, |
210 | T_FOREIGN, |
207 | T_FOREIGN, |
211 | T_CHARACTER, |
|
|
212 | T_PORT, |
208 | T_PORT, |
213 | T_VECTOR, |
209 | T_VECTOR, |
214 | T_MACRO, |
|
|
215 | T_PROMISE, |
210 | T_PROMISE, |
216 | T_ENVIRONMENT, |
211 | T_ENVIRONMENT, |
217 | /* one more... */ |
212 | /* one more... */ |
218 | T_NUM_SYSTEM_TYPES |
213 | T_NUM_SYSTEM_TYPES |
219 | }; |
214 | }; |
… | |
… | |
255 | 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); |
256 | static num num_intdiv (num a, num b); |
251 | static num num_intdiv (num a, num b); |
257 | static num num_rem (num a, num b); |
252 | static num num_rem (num a, num b); |
258 | static num num_mod (num a, num b); |
253 | static num num_mod (num a, num b); |
259 | |
254 | |
260 | #if USE_MATH |
|
|
261 | static double round_per_R5RS (double x); |
|
|
262 | #endif |
|
|
263 | static int is_zero_rvalue (RVALUE x); |
255 | static int is_zero_rvalue (RVALUE x); |
264 | |
256 | |
265 | static num num_zero; |
257 | static num num_zero; |
266 | static num num_one; |
258 | static num num_one; |
267 | |
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 | |
268 | /* macros for cell operations */ |
264 | /* macros for cell operations */ |
269 | #define typeflag(p) ((p)->flag + 0) |
265 | #define typeflag(p) (CELL(p)->flag + 0) |
270 | #define set_typeflag(p,v) ((p)->flag = (v)) |
266 | #define set_typeflag(p,v) (CELL(p)->flag = (v)) |
271 | #define type(p) (typeflag (p) & T_MASKTYPE) |
267 | #define type(p) (typeflag (p) & T_MASKTYPE) |
272 | |
268 | |
273 | INTERFACE int |
269 | INTERFACE int |
274 | is_string (pointer p) |
270 | is_string (pointer p) |
275 | { |
271 | { |
276 | return type (p) == T_STRING; |
272 | return type (p) == T_STRING; |
277 | } |
273 | } |
278 | |
274 | |
279 | #define strvalue(p) ((p)->object.string.svalue) |
275 | #define strvalue(p) (CELL(p)->object.string.svalue) |
280 | #define strlength(p) ((p)->object.string.length) |
276 | #define strlength(p) (CELL(p)->object.string.length) |
281 | |
277 | |
282 | INTERFACE int |
278 | INTERFACE int |
283 | is_vector (pointer p) |
279 | is_vector (pointer p) |
284 | { |
280 | { |
285 | return type (p) == T_VECTOR; |
281 | return type (p) == T_VECTOR; |
286 | } |
282 | } |
287 | |
283 | |
288 | #define vecvalue(p) ((p)->object.vector.vvalue) |
284 | #define vecvalue(p) (CELL(p)->object.vector.vvalue) |
289 | #define veclength(p) ((p)->object.vector.length) |
285 | #define veclength(p) (CELL(p)->object.vector.length) |
290 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
286 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
291 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
287 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
292 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
288 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
293 | |
289 | |
294 | INTERFACE int |
290 | INTERFACE int |
… | |
… | |
320 | string_value (pointer p) |
316 | string_value (pointer p) |
321 | { |
317 | { |
322 | return strvalue (p); |
318 | return strvalue (p); |
323 | } |
319 | } |
324 | |
320 | |
325 | #define ivalue_unchecked(p) (p)->object.ivalue |
321 | #define ivalue_unchecked(p) CELL(p)->object.ivalue |
326 | #define set_ivalue(p,v) (p)->object.ivalue = (v) |
322 | #define set_ivalue(p,v) CELL(p)->object.ivalue = (v) |
327 | |
323 | |
328 | #if USE_REAL |
324 | #if USE_REAL |
329 | #define rvalue_unchecked(p) (p)->object.rvalue |
325 | #define rvalue_unchecked(p) CELL(p)->object.rvalue |
330 | #define set_rvalue(p,v) (p)->object.rvalue = (v) |
326 | #define set_rvalue(p,v) CELL(p)->object.rvalue = (v) |
331 | #else |
327 | #else |
332 | #define rvalue_unchecked(p) (p)->object.ivalue |
328 | #define rvalue_unchecked(p) CELL(p)->object.ivalue |
333 | #define set_rvalue(p,v) (p)->object.ivalue = (v) |
329 | #define set_rvalue(p,v) CELL(p)->object.ivalue = (v) |
334 | #endif |
330 | #endif |
335 | |
331 | |
336 | INTERFACE long |
332 | INTERFACE long |
337 | charvalue (pointer p) |
333 | charvalue (pointer p) |
338 | { |
334 | { |
339 | return ivalue_unchecked (p); |
335 | return ivalue_unchecked (p); |
340 | } |
336 | } |
341 | |
337 | |
|
|
338 | #define port(p) CELL(p)->object.port |
|
|
339 | #define set_port(p,v) port(p) = (v) |
342 | INTERFACE int |
340 | INTERFACE int |
343 | is_port (pointer p) |
341 | is_port (pointer p) |
344 | { |
342 | { |
345 | return type (p) == T_PORT; |
343 | return type (p) == T_PORT; |
346 | } |
344 | } |
347 | |
345 | |
348 | INTERFACE int |
346 | INTERFACE int |
349 | is_inport (pointer p) |
347 | is_inport (pointer p) |
350 | { |
348 | { |
351 | return is_port (p) && p->object.port->kind & port_input; |
349 | return is_port (p) && port (p)->kind & port_input; |
352 | } |
350 | } |
353 | |
351 | |
354 | INTERFACE int |
352 | INTERFACE int |
355 | is_outport (pointer p) |
353 | is_outport (pointer p) |
356 | { |
354 | { |
357 | return is_port (p) && p->object.port->kind & port_output; |
355 | return is_port (p) && port (p)->kind & port_output; |
358 | } |
356 | } |
359 | |
357 | |
360 | INTERFACE int |
358 | INTERFACE int |
361 | is_pair (pointer p) |
359 | is_pair (pointer p) |
362 | { |
360 | { |
363 | return type (p) == T_PAIR; |
361 | return type (p) == T_PAIR; |
364 | } |
362 | } |
365 | |
363 | |
366 | #define car(p) ((p)->object.cons.car + 0) |
364 | #define car(p) (POINTER (CELL(p)->object.cons.car)) |
367 | #define cdr(p) ((p)->object.cons.cdr + 0) |
365 | #define cdr(p) (POINTER (CELL(p)->object.cons.cdr)) |
368 | |
366 | |
369 | static pointer caar (pointer p) { return car (car (p)); } |
367 | static pointer caar (pointer p) { return car (car (p)); } |
370 | static pointer cadr (pointer p) { return car (cdr (p)); } |
368 | static pointer cadr (pointer p) { return car (cdr (p)); } |
371 | static pointer cdar (pointer p) { return cdr (car (p)); } |
369 | static pointer cdar (pointer p) { return cdr (car (p)); } |
372 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
370 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
… | |
… | |
376 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
374 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
377 | |
375 | |
378 | INTERFACE void |
376 | INTERFACE void |
379 | set_car (pointer p, pointer q) |
377 | set_car (pointer p, pointer q) |
380 | { |
378 | { |
381 | p->object.cons.car = q; |
379 | CELL(p)->object.cons.car = CELL (q); |
382 | } |
380 | } |
383 | |
381 | |
384 | INTERFACE void |
382 | INTERFACE void |
385 | set_cdr (pointer p, pointer q) |
383 | set_cdr (pointer p, pointer q) |
386 | { |
384 | { |
387 | p->object.cons.cdr = q; |
385 | CELL(p)->object.cons.cdr = CELL (q); |
388 | } |
386 | } |
389 | |
387 | |
390 | INTERFACE pointer |
388 | INTERFACE pointer |
391 | pair_car (pointer p) |
389 | pair_car (pointer p) |
392 | { |
390 | { |
… | |
… | |
406 | } |
404 | } |
407 | |
405 | |
408 | INTERFACE char * |
406 | INTERFACE char * |
409 | symname (pointer p) |
407 | symname (pointer p) |
410 | { |
408 | { |
411 | return strvalue (car (p)); |
409 | return strvalue (p); |
412 | } |
410 | } |
413 | |
411 | |
414 | #if USE_PLIST |
412 | #if USE_PLIST |
|
|
413 | #error plists are broken because symbols are no longer pairs |
|
|
414 | #define symprop(p) cdr(p) |
415 | SCHEME_EXPORT int |
415 | SCHEME_EXPORT int |
416 | hasprop (pointer p) |
416 | hasprop (pointer p) |
417 | { |
417 | { |
418 | return typeflag (p) & T_SYMBOL; |
418 | return typeflag (p) & T_SYMBOL; |
419 | } |
419 | } |
420 | |
|
|
421 | # define symprop(p) cdr(p) |
|
|
422 | #endif |
420 | #endif |
423 | |
421 | |
424 | INTERFACE int |
422 | INTERFACE int |
425 | is_syntax (pointer p) |
423 | is_syntax (pointer p) |
426 | { |
424 | { |
… | |
… | |
440 | } |
438 | } |
441 | |
439 | |
442 | INTERFACE char * |
440 | INTERFACE char * |
443 | syntaxname (pointer p) |
441 | syntaxname (pointer p) |
444 | { |
442 | { |
445 | return strvalue (car (p)); |
443 | return strvalue (p); |
446 | } |
444 | } |
447 | |
445 | |
448 | #define procnum(p) ivalue_unchecked (p) |
446 | #define procnum(p) ivalue_unchecked (p) |
449 | static const char *procname (pointer x); |
447 | static const char *procname (pointer x); |
450 | |
448 | |
… | |
… | |
667 | |
665 | |
668 | static int file_push (SCHEME_P_ const char *fname); |
666 | static int file_push (SCHEME_P_ const char *fname); |
669 | static void file_pop (SCHEME_P); |
667 | static void file_pop (SCHEME_P); |
670 | static int file_interactive (SCHEME_P); |
668 | static int file_interactive (SCHEME_P); |
671 | ecb_inline int is_one_of (const char *s, int c); |
669 | ecb_inline int is_one_of (const char *s, int c); |
672 | static int alloc_cellseg (SCHEME_P_ int n); |
670 | static int alloc_cellseg (SCHEME_P); |
673 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
671 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
674 | static void finalize_cell (SCHEME_P_ pointer a); |
672 | static void finalize_cell (SCHEME_P_ pointer a); |
675 | static int count_consecutive_cells (pointer x, int needed); |
673 | static int count_consecutive_cells (pointer x, int needed); |
676 | 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); |
677 | static pointer mk_number (SCHEME_P_ const num n); |
675 | static pointer mk_number (SCHEME_P_ const num n); |
… | |
… | |
871 | } |
869 | } |
872 | |
870 | |
873 | return ret; |
871 | return ret; |
874 | } |
872 | } |
875 | |
873 | |
876 | #if USE_MATH |
|
|
877 | |
|
|
878 | /* Round to nearest. Round to even if midway */ |
|
|
879 | static double |
|
|
880 | round_per_R5RS (double x) |
|
|
881 | { |
|
|
882 | double fl = floor (x); |
|
|
883 | double ce = ceil (x); |
|
|
884 | double dfl = x - fl; |
|
|
885 | double dce = ce - x; |
|
|
886 | |
|
|
887 | if (dfl > dce) |
|
|
888 | return ce; |
|
|
889 | else if (dfl < dce) |
|
|
890 | return fl; |
|
|
891 | else |
|
|
892 | { |
|
|
893 | if (fmod (fl, 2) == 0) /* I imagine this holds */ |
|
|
894 | return fl; |
|
|
895 | else |
|
|
896 | return ce; |
|
|
897 | } |
|
|
898 | } |
|
|
899 | #endif |
|
|
900 | |
|
|
901 | static int |
874 | static int |
902 | is_zero_rvalue (RVALUE x) |
875 | is_zero_rvalue (RVALUE x) |
903 | { |
876 | { |
904 | return x == 0; |
877 | return x == 0; |
905 | #if 0 |
878 | #if 0 |
… | |
… | |
911 | #endif |
884 | #endif |
912 | } |
885 | } |
913 | |
886 | |
914 | /* allocate new cell segment */ |
887 | /* allocate new cell segment */ |
915 | static int |
888 | static int |
916 | alloc_cellseg (SCHEME_P_ int n) |
889 | alloc_cellseg (SCHEME_P) |
917 | { |
890 | { |
918 | pointer newp; |
891 | struct cell *newp; |
919 | pointer last; |
892 | struct cell *last; |
920 | pointer p; |
893 | struct cell *p; |
921 | char *cp; |
894 | char *cp; |
922 | long i; |
895 | long i; |
923 | int k; |
896 | int k; |
924 | |
897 | |
925 | static int segsize = CELL_SEGSIZE >> 1; |
898 | static int segsize = CELL_SEGSIZE >> 1; |
926 | segsize <<= 1; |
899 | segsize <<= 1; |
927 | |
900 | |
928 | for (k = 0; k < n; k++) |
|
|
929 | { |
|
|
930 | if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1) |
|
|
931 | return k; |
|
|
932 | |
|
|
933 | cp = malloc (segsize * sizeof (struct cell)); |
901 | cp = malloc (segsize * sizeof (struct cell)); |
934 | |
902 | |
935 | if (!cp && USE_ERROR_CHECKING) |
903 | if (!cp && USE_ERROR_CHECKING) |
936 | return k; |
904 | return k; |
937 | |
905 | |
938 | i = ++SCHEME_V->last_cell_seg; |
906 | i = ++SCHEME_V->last_cell_seg; |
939 | SCHEME_V->alloc_seg[i] = cp; |
907 | SCHEME_V->alloc_seg[i] = cp; |
940 | |
908 | |
941 | newp = (pointer)cp; |
909 | newp = (struct cell *)cp; |
942 | SCHEME_V->cell_seg[i] = newp; |
910 | SCHEME_V->cell_seg[i] = newp; |
943 | SCHEME_V->cell_segsize[i] = segsize; |
911 | SCHEME_V->cell_segsize[i] = segsize; |
944 | SCHEME_V->fcells += segsize; |
912 | SCHEME_V->fcells += segsize; |
945 | last = newp + segsize - 1; |
913 | last = newp + segsize - 1; |
946 | |
914 | |
947 | for (p = newp; p <= last; p++) |
915 | for (p = newp; p <= last; p++) |
948 | { |
916 | { |
|
|
917 | pointer cp = POINTER (p); |
949 | set_typeflag (p, T_PAIR); |
918 | set_typeflag (cp, T_PAIR); |
950 | set_car (p, NIL); |
919 | set_car (cp, NIL); |
951 | set_cdr (p, p + 1); |
920 | set_cdr (cp, POINTER (p + 1)); |
952 | } |
921 | } |
953 | |
922 | |
954 | set_cdr (last, SCHEME_V->free_cell); |
923 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
955 | SCHEME_V->free_cell = newp; |
924 | SCHEME_V->free_cell = POINTER (newp); |
956 | } |
|
|
957 | |
925 | |
958 | return n; |
926 | return 1; |
959 | } |
927 | } |
960 | |
928 | |
961 | /* get new cell. parameter a, b is marked by gc. */ |
929 | /* get new cell. parameter a, b is marked by gc. */ |
962 | ecb_inline pointer |
930 | ecb_inline pointer |
963 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
931 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
… | |
… | |
967 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
935 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
968 | return S_SINK; |
936 | return S_SINK; |
969 | |
937 | |
970 | if (SCHEME_V->free_cell == NIL) |
938 | if (SCHEME_V->free_cell == NIL) |
971 | { |
939 | { |
972 | 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; |
973 | |
941 | |
974 | gc (SCHEME_A_ a, b); |
942 | gc (SCHEME_A_ a, b); |
975 | |
943 | |
976 | 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) |
977 | { |
945 | { |
978 | /* 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 */ |
979 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
947 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
980 | { |
948 | { |
981 | #if USE_ERROR_CHECKING |
949 | #if USE_ERROR_CHECKING |
982 | SCHEME_V->no_memory = 1; |
950 | SCHEME_V->no_memory = 1; |
983 | return S_SINK; |
951 | return S_SINK; |
984 | #endif |
952 | #endif |
… | |
… | |
996 | } |
964 | } |
997 | } |
965 | } |
998 | |
966 | |
999 | /* To retain recent allocs before interpreter knows about them - |
967 | /* To retain recent allocs before interpreter knows about them - |
1000 | Tehom */ |
968 | Tehom */ |
1001 | |
|
|
1002 | static void |
969 | static void |
1003 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
970 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
1004 | { |
971 | { |
1005 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
972 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
1006 | |
973 | |
… | |
… | |
1028 | } |
995 | } |
1029 | |
996 | |
1030 | static pointer |
997 | static pointer |
1031 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
998 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1032 | { |
999 | { |
1033 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
1000 | pointer v = get_cell_x (SCHEME_A_ NIL, NIL); |
1034 | pointer *e = malloc (len * sizeof (pointer)); |
1001 | pointer *e = malloc (len * sizeof (pointer)); |
1035 | |
1002 | |
1036 | if (!e && USE_ERROR_CHECKING) |
1003 | if (!e && USE_ERROR_CHECKING) |
1037 | return S_SINK; |
1004 | return S_SINK; |
1038 | |
1005 | |
1039 | /* Record it as a vector so that gc understands it. */ |
1006 | /* Record it as a vector so that gc understands it. */ |
1040 | set_typeflag (v, T_VECTOR | T_ATOM); |
1007 | set_typeflag (v, T_VECTOR | T_ATOM); |
1041 | |
1008 | |
1042 | v->object.vector.vvalue = e; |
1009 | CELL(v)->object.vector.vvalue = e; |
1043 | v->object.vector.length = len; |
1010 | CELL(v)->object.vector.length = len; |
1044 | fill_vector (v, 0, init); |
1011 | fill_vector (v, 0, init); |
1045 | push_recent_alloc (SCHEME_A_ v, NIL); |
1012 | push_recent_alloc (SCHEME_A_ v, NIL); |
1046 | |
1013 | |
1047 | return v; |
1014 | return v; |
1048 | } |
1015 | } |
… | |
… | |
1057 | static void |
1024 | static void |
1058 | check_cell_alloced (pointer p, int expect_alloced) |
1025 | check_cell_alloced (pointer p, int expect_alloced) |
1059 | { |
1026 | { |
1060 | /* 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. */ |
1061 | if (typeflag (p) & !expect_alloced) |
1028 | if (typeflag (p) & !expect_alloced) |
1062 | xwrstr ("Cell is already allocated!\n"); |
1029 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1063 | |
1030 | |
1064 | if (!(typeflag (p)) & expect_alloced) |
1031 | if (!(typeflag (p)) & expect_alloced) |
1065 | xwrstr ("Cell is not allocated!\n"); |
1032 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1066 | } |
1033 | } |
1067 | |
1034 | |
1068 | static void |
1035 | static void |
1069 | check_range_alloced (pointer p, int n, int expect_alloced) |
1036 | check_range_alloced (pointer p, int n, int expect_alloced) |
1070 | { |
1037 | { |
… | |
… | |
1092 | set_cdr (x, b); |
1059 | set_cdr (x, b); |
1093 | |
1060 | |
1094 | return x; |
1061 | return x; |
1095 | } |
1062 | } |
1096 | |
1063 | |
1097 | /* ========== oblist implementation ========== */ |
|
|
1098 | |
|
|
1099 | static pointer |
1064 | static pointer |
1100 | generate_symbol (SCHEME_P_ const char *name) |
1065 | generate_symbol (SCHEME_P_ const char *name) |
1101 | { |
1066 | { |
1102 | pointer x = mk_string (SCHEME_A_ name); |
1067 | pointer x = mk_string (SCHEME_A_ name); |
1103 | setimmutable (x); |
1068 | setimmutable (x); |
1104 | x = immutable_cons (x, NIL); |
|
|
1105 | set_typeflag (x, T_SYMBOL); |
1069 | set_typeflag (x, T_SYMBOL | T_ATOM); |
1106 | return x; |
1070 | return x; |
1107 | } |
1071 | } |
|
|
1072 | |
|
|
1073 | /* ========== oblist implementation ========== */ |
1108 | |
1074 | |
1109 | #ifndef USE_OBJECT_LIST |
1075 | #ifndef USE_OBJECT_LIST |
1110 | |
1076 | |
1111 | static int |
1077 | static int |
1112 | hash_fn (const char *key, int table_size) |
1078 | hash_fn (const char *key, int table_size) |
… | |
… | |
1199 | |
1165 | |
1200 | /* returns the new symbol */ |
1166 | /* returns the new symbol */ |
1201 | static pointer |
1167 | static pointer |
1202 | oblist_add_by_name (SCHEME_P_ const char *name) |
1168 | oblist_add_by_name (SCHEME_P_ const char *name) |
1203 | { |
1169 | { |
1204 | pointer x = mk_string (SCHEME_A_ name); |
1170 | pointer x = generate_symbol (SCHEME_A_ name); |
1205 | set_typeflag (x, T_SYMBOL); |
|
|
1206 | setimmutable (x); |
|
|
1207 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1171 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1208 | return x; |
1172 | return x; |
1209 | } |
1173 | } |
1210 | |
1174 | |
1211 | static pointer |
1175 | static pointer |
… | |
… | |
1221 | mk_port (SCHEME_P_ port *p) |
1185 | mk_port (SCHEME_P_ port *p) |
1222 | { |
1186 | { |
1223 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1187 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1224 | |
1188 | |
1225 | set_typeflag (x, T_PORT | T_ATOM); |
1189 | set_typeflag (x, T_PORT | T_ATOM); |
1226 | x->object.port = p; |
1190 | set_port (x, p); |
1227 | |
1191 | |
1228 | return x; |
1192 | return x; |
1229 | } |
1193 | } |
1230 | #endif |
1194 | #endif |
1231 | |
1195 | |
1232 | pointer |
1196 | pointer |
1233 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1197 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1234 | { |
1198 | { |
1235 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1199 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1236 | |
1200 | |
1237 | set_typeflag (x, (T_FOREIGN | T_ATOM)); |
1201 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1238 | x->object.ff = f; |
1202 | CELL(x)->object.ff = f; |
1239 | |
1203 | |
1240 | return x; |
1204 | return x; |
1241 | } |
1205 | } |
1242 | |
1206 | |
1243 | INTERFACE pointer |
1207 | INTERFACE pointer |
1244 | mk_character (SCHEME_P_ int c) |
1208 | mk_character (SCHEME_P_ int c) |
1245 | { |
1209 | { |
1246 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1210 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1247 | |
1211 | |
1248 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1212 | set_typeflag (x, T_CHARACTER | T_ATOM); |
1249 | set_ivalue (x, c & 0xff); |
1213 | set_ivalue (x, c & 0xff); |
1250 | |
1214 | |
1251 | return x; |
1215 | return x; |
1252 | } |
1216 | } |
1253 | |
1217 | |
1254 | /* get number atom (integer) */ |
1218 | /* get number atom (integer) */ |
1255 | INTERFACE pointer |
1219 | INTERFACE pointer |
1256 | mk_integer (SCHEME_P_ long n) |
1220 | mk_integer (SCHEME_P_ long n) |
1257 | { |
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 | { |
1258 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1232 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1259 | |
1233 | |
1260 | 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 */ |
1261 | set_ivalue (x, n); |
1236 | set_ivalue (x, n); |
1262 | |
1237 | |
|
|
1238 | *pp = x; |
|
|
1239 | } |
|
|
1240 | |
1263 | return x; |
1241 | return *pp; |
1264 | } |
1242 | } |
1265 | |
1243 | |
1266 | INTERFACE pointer |
1244 | INTERFACE pointer |
1267 | mk_real (SCHEME_P_ RVALUE n) |
1245 | mk_real (SCHEME_P_ RVALUE n) |
1268 | { |
1246 | { |
1269 | #if USE_REAL |
1247 | #if USE_REAL |
1270 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1248 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1271 | |
1249 | |
1272 | set_typeflag (x, (T_REAL | T_ATOM)); |
1250 | set_typeflag (x, T_REAL | T_ATOM); |
1273 | set_rvalue (x, n); |
1251 | set_rvalue (x, n); |
1274 | |
1252 | |
1275 | return x; |
1253 | return x; |
1276 | #else |
1254 | #else |
1277 | return mk_integer (SCHEME_A_ n); |
1255 | return mk_integer (SCHEME_A_ n); |
… | |
… | |
1354 | |
1332 | |
1355 | for (i = start; i < veclength (vec); i++) |
1333 | for (i = start; i < veclength (vec); i++) |
1356 | vecvalue (vec)[i] = obj; |
1334 | vecvalue (vec)[i] = obj; |
1357 | } |
1335 | } |
1358 | |
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 | |
1359 | INTERFACE pointer |
1346 | INTERFACE pointer |
1360 | vector_get (pointer vec, uint32_t ielem) |
1347 | vector_get (pointer vec, uint32_t ielem) |
1361 | { |
1348 | { |
1362 | return vecvalue(vec)[ielem]; |
1349 | return vecvalue(vec)[ielem]; |
1363 | } |
1350 | } |
… | |
… | |
1384 | INTERFACE pointer |
1371 | INTERFACE pointer |
1385 | gensym (SCHEME_P) |
1372 | gensym (SCHEME_P) |
1386 | { |
1373 | { |
1387 | pointer x; |
1374 | pointer x; |
1388 | char name[40] = "gensym-"; |
1375 | char name[40] = "gensym-"; |
1389 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1376 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1390 | |
1377 | |
1391 | return generate_symbol (SCHEME_A_ name); |
1378 | return generate_symbol (SCHEME_A_ name); |
|
|
1379 | } |
|
|
1380 | |
|
|
1381 | static int |
|
|
1382 | is_gensym (SCHEME_P_ pointer x) |
|
|
1383 | { |
|
|
1384 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1392 | } |
1385 | } |
1393 | |
1386 | |
1394 | /* make symbol or number atom from string */ |
1387 | /* make symbol or number atom from string */ |
1395 | static pointer |
1388 | static pointer |
1396 | mk_atom (SCHEME_P_ char *q) |
1389 | mk_atom (SCHEME_P_ char *q) |
… | |
… | |
1482 | return S_F; |
1475 | return S_F; |
1483 | else if (*name == '\\') /* #\w (character) */ |
1476 | else if (*name == '\\') /* #\w (character) */ |
1484 | { |
1477 | { |
1485 | int c; |
1478 | int c; |
1486 | |
1479 | |
|
|
1480 | // TODO: optimise |
1487 | if (stricmp (name + 1, "space") == 0) |
1481 | if (stricmp (name + 1, "space") == 0) |
1488 | c = ' '; |
1482 | c = ' '; |
1489 | else if (stricmp (name + 1, "newline") == 0) |
1483 | else if (stricmp (name + 1, "newline") == 0) |
1490 | c = '\n'; |
1484 | c = '\n'; |
1491 | else if (stricmp (name + 1, "return") == 0) |
1485 | else if (stricmp (name + 1, "return") == 0) |
1492 | c = '\r'; |
1486 | c = '\r'; |
1493 | else if (stricmp (name + 1, "tab") == 0) |
1487 | else if (stricmp (name + 1, "tab") == 0) |
1494 | c = '\t'; |
1488 | c = '\t'; |
|
|
1489 | else if (stricmp (name + 1, "alarm") == 0) |
|
|
1490 | c = 0x07; |
|
|
1491 | else if (stricmp (name + 1, "backspace") == 0) |
|
|
1492 | c = 0x08; |
|
|
1493 | else if (stricmp (name + 1, "escape") == 0) |
|
|
1494 | c = 0x1b; |
|
|
1495 | else if (stricmp (name + 1, "delete") == 0) |
|
|
1496 | c = 0x7f; |
|
|
1497 | else if (stricmp (name + 1, "null") == 0) |
|
|
1498 | c = 0; |
1495 | else if (name[1] == 'x' && name[2] != 0) |
1499 | else if (name[1] == 'x' && name[2] != 0) |
1496 | { |
1500 | { |
1497 | long c1 = strtol (name + 2, 0, 16); |
1501 | long c1 = strtol (name + 2, 0, 16); |
1498 | |
1502 | |
1499 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1503 | if (0 <= c1 && c1 <= UCHAR_MAX) |
… | |
… | |
1605 | |
1609 | |
1606 | /* garbage collection. parameter a, b is marked. */ |
1610 | /* garbage collection. parameter a, b is marked. */ |
1607 | static void |
1611 | static void |
1608 | gc (SCHEME_P_ pointer a, pointer b) |
1612 | gc (SCHEME_P_ pointer a, pointer b) |
1609 | { |
1613 | { |
1610 | pointer p; |
|
|
1611 | int i; |
1614 | int i; |
1612 | |
1615 | |
1613 | if (SCHEME_V->gc_verbose) |
1616 | if (SCHEME_V->gc_verbose) |
1614 | putstr (SCHEME_A_ "gc..."); |
1617 | putstr (SCHEME_A_ "gc..."); |
1615 | |
1618 | |
… | |
… | |
1631 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1634 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1632 | mark (car (S_SINK)); |
1635 | mark (car (S_SINK)); |
1633 | /* Mark any older stuff above nested C calls */ |
1636 | /* Mark any older stuff above nested C calls */ |
1634 | mark (SCHEME_V->c_nest); |
1637 | mark (SCHEME_V->c_nest); |
1635 | |
1638 | |
|
|
1639 | #if USE_INTCACHE |
|
|
1640 | /* mark intcache */ |
|
|
1641 | for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i) |
|
|
1642 | if (SCHEME_V->intcache[i - INTCACHE_MIN]) |
|
|
1643 | mark (SCHEME_V->intcache[i - INTCACHE_MIN]); |
|
|
1644 | #endif |
|
|
1645 | |
1636 | /* mark variables a, b */ |
1646 | /* mark variables a, b */ |
1637 | mark (a); |
1647 | mark (a); |
1638 | mark (b); |
1648 | mark (b); |
1639 | |
1649 | |
1640 | /* garbage collect */ |
1650 | /* garbage collect */ |
1641 | clrmark (NIL); |
1651 | clrmark (NIL); |
1642 | SCHEME_V->fcells = 0; |
1652 | SCHEME_V->fcells = 0; |
1643 | SCHEME_V->free_cell = NIL; |
1653 | SCHEME_V->free_cell = NIL; |
1644 | |
1654 | |
1645 | /* free-list is kept sorted by address so as to maintain consecutive |
1655 | if (SCHEME_V->gc_verbose) |
1646 | ranges, if possible, for use with vectors. Here we scan the cells |
1656 | putstr (SCHEME_A_ "freeing..."); |
1647 | (which are also kept sorted by address) downwards to build the |
1657 | |
1648 | free-list in sorted order. |
1658 | uint32_t total = 0; |
1649 | */ |
1659 | |
|
|
1660 | /* Here we scan the cells to build the free-list. */ |
1650 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1661 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1651 | { |
1662 | { |
1652 | p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
1663 | struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1664 | struct cell *p; |
|
|
1665 | total += SCHEME_V->cell_segsize [i]; |
1653 | |
1666 | |
1654 | while (--p >= SCHEME_V->cell_seg[i]) |
1667 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
1655 | { |
1668 | { |
|
|
1669 | pointer c = POINTER (p); |
|
|
1670 | |
1656 | if (is_mark (p)) |
1671 | if (is_mark (c)) |
1657 | clrmark (p); |
1672 | clrmark (c); |
1658 | else |
1673 | else |
1659 | { |
1674 | { |
1660 | /* reclaim cell */ |
1675 | /* reclaim cell */ |
1661 | if (typeflag (p) != T_PAIR) |
1676 | if (typeflag (c) != T_PAIR) |
1662 | { |
1677 | { |
1663 | finalize_cell (SCHEME_A_ p); |
1678 | finalize_cell (SCHEME_A_ c); |
1664 | set_typeflag (p, T_PAIR); |
1679 | set_typeflag (c, T_PAIR); |
1665 | set_car (p, NIL); |
1680 | set_car (c, NIL); |
1666 | } |
1681 | } |
1667 | |
1682 | |
1668 | ++SCHEME_V->fcells; |
1683 | ++SCHEME_V->fcells; |
1669 | set_cdr (p, SCHEME_V->free_cell); |
1684 | set_cdr (c, SCHEME_V->free_cell); |
1670 | SCHEME_V->free_cell = p; |
1685 | SCHEME_V->free_cell = c; |
1671 | } |
1686 | } |
1672 | } |
1687 | } |
1673 | } |
1688 | } |
1674 | |
1689 | |
1675 | if (SCHEME_V->gc_verbose) |
1690 | if (SCHEME_V->gc_verbose) |
1676 | { |
1691 | { |
1677 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); |
1692 | 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"); |
1678 | } |
1693 | } |
1679 | } |
1694 | } |
1680 | |
1695 | |
1681 | static void |
1696 | static void |
1682 | finalize_cell (SCHEME_P_ pointer a) |
1697 | finalize_cell (SCHEME_P_ pointer a) |
1683 | { |
1698 | { |
1684 | /* TODO, fast bitmap check? */ |
1699 | /* TODO, fast bitmap check? */ |
1685 | if (is_string (a)) |
1700 | if (is_string (a) || is_symbol (a)) |
1686 | free (strvalue (a)); |
1701 | free (strvalue (a)); |
1687 | else if (is_vector (a)) |
1702 | else if (is_vector (a)) |
1688 | free (vecvalue (a)); |
1703 | free (vecvalue (a)); |
1689 | #if USE_PORTS |
1704 | #if USE_PORTS |
1690 | else if (is_port (a)) |
1705 | else if (is_port (a)) |
1691 | { |
1706 | { |
1692 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
1707 | if (port(a)->kind & port_file && port (a)->rep.stdio.closeit) |
1693 | port_close (SCHEME_A_ a, port_input | port_output); |
1708 | port_close (SCHEME_A_ a, port_input | port_output); |
1694 | |
1709 | |
1695 | free (a->object.port); |
1710 | free (port (a)); |
1696 | } |
1711 | } |
1697 | #endif |
1712 | #endif |
1698 | } |
1713 | } |
1699 | |
1714 | |
1700 | /* ========== Routines for Reading ========== */ |
1715 | /* ========== Routines for Reading ========== */ |
… | |
… | |
1716 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1731 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1717 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1732 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1718 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; |
1733 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; |
1719 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1734 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1720 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1735 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1721 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1736 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1722 | |
1737 | |
1723 | #if SHOW_ERROR_LINE |
1738 | #if SHOW_ERROR_LINE |
1724 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1739 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1725 | |
1740 | |
1726 | if (fname) |
1741 | if (fname) |
… | |
… | |
1743 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1758 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1744 | #if USE_PORTS |
1759 | #if USE_PORTS |
1745 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1760 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1746 | #endif |
1761 | #endif |
1747 | SCHEME_V->file_i--; |
1762 | SCHEME_V->file_i--; |
1748 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1763 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1749 | } |
1764 | } |
1750 | } |
1765 | } |
1751 | |
1766 | |
1752 | static int |
1767 | static int |
1753 | file_interactive (SCHEME_P) |
1768 | file_interactive (SCHEME_P) |
1754 | { |
1769 | { |
1755 | #if USE_PORTS |
1770 | #if USE_PORTS |
1756 | return SCHEME_V->file_i == 0 |
1771 | return SCHEME_V->file_i == 0 |
1757 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1772 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1758 | && (SCHEME_V->inport->object.port->kind & port_file); |
1773 | && (port (SCHEME_V->inport)->kind & port_file); |
1759 | #else |
1774 | #else |
1760 | return 0; |
1775 | return 0; |
1761 | #endif |
1776 | #endif |
1762 | } |
1777 | } |
1763 | |
1778 | |
… | |
… | |
1897 | } |
1912 | } |
1898 | |
1913 | |
1899 | static void |
1914 | static void |
1900 | port_close (SCHEME_P_ pointer p, int flag) |
1915 | port_close (SCHEME_P_ pointer p, int flag) |
1901 | { |
1916 | { |
1902 | port *pt = p->object.port; |
1917 | port *pt = port (p); |
1903 | |
1918 | |
1904 | pt->kind &= ~flag; |
1919 | pt->kind &= ~flag; |
1905 | |
1920 | |
1906 | if ((pt->kind & (port_input | port_output)) == 0) |
1921 | if ((pt->kind & (port_input | port_output)) == 0) |
1907 | { |
1922 | { |
… | |
… | |
1928 | /* get new character from input file */ |
1943 | /* get new character from input file */ |
1929 | static int |
1944 | static int |
1930 | inchar (SCHEME_P) |
1945 | inchar (SCHEME_P) |
1931 | { |
1946 | { |
1932 | int c; |
1947 | int c; |
1933 | port *pt; |
1948 | port *pt = port (SCHEME_V->inport); |
1934 | |
|
|
1935 | pt = SCHEME_V->inport->object.port; |
|
|
1936 | |
1949 | |
1937 | if (pt->kind & port_saw_EOF) |
1950 | if (pt->kind & port_saw_EOF) |
1938 | return EOF; |
1951 | return EOF; |
1939 | |
1952 | |
1940 | c = basic_inchar (pt); |
1953 | c = basic_inchar (pt); |
… | |
… | |
2007 | port *pt; |
2020 | port *pt; |
2008 | |
2021 | |
2009 | if (c == EOF) |
2022 | if (c == EOF) |
2010 | return; |
2023 | return; |
2011 | |
2024 | |
2012 | pt = SCHEME_V->inport->object.port; |
2025 | pt = port (SCHEME_V->inport); |
2013 | pt->unget = c; |
2026 | pt->unget = c; |
2014 | #else |
2027 | #else |
2015 | if (c == EOF) |
2028 | if (c == EOF) |
2016 | return; |
2029 | return; |
2017 | |
2030 | |
… | |
… | |
2045 | |
2058 | |
2046 | INTERFACE void |
2059 | INTERFACE void |
2047 | putstr (SCHEME_P_ const char *s) |
2060 | putstr (SCHEME_P_ const char *s) |
2048 | { |
2061 | { |
2049 | #if USE_PORTS |
2062 | #if USE_PORTS |
2050 | port *pt = SCHEME_V->outport->object.port; |
2063 | port *pt = port (SCHEME_V->outport); |
2051 | |
2064 | |
2052 | if (pt->kind & port_file) |
2065 | if (pt->kind & port_file) |
2053 | write (pt->rep.stdio.file, s, strlen (s)); |
2066 | write (pt->rep.stdio.file, s, strlen (s)); |
2054 | else |
2067 | else |
2055 | for (; *s; s++) |
2068 | for (; *s; s++) |
… | |
… | |
2057 | *pt->rep.string.curr++ = *s; |
2070 | *pt->rep.string.curr++ = *s; |
2058 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
2071 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
2059 | *pt->rep.string.curr++ = *s; |
2072 | *pt->rep.string.curr++ = *s; |
2060 | |
2073 | |
2061 | #else |
2074 | #else |
2062 | xwrstr (s); |
2075 | write (pt->rep.stdio.file, s, strlen (s)); |
2063 | #endif |
2076 | #endif |
2064 | } |
2077 | } |
2065 | |
2078 | |
2066 | static void |
2079 | static void |
2067 | putchars (SCHEME_P_ const char *s, int len) |
2080 | putchars (SCHEME_P_ const char *s, int len) |
2068 | { |
2081 | { |
2069 | #if USE_PORTS |
2082 | #if USE_PORTS |
2070 | port *pt = SCHEME_V->outport->object.port; |
2083 | port *pt = port (SCHEME_V->outport); |
2071 | |
2084 | |
2072 | if (pt->kind & port_file) |
2085 | if (pt->kind & port_file) |
2073 | write (pt->rep.stdio.file, s, len); |
2086 | write (pt->rep.stdio.file, s, len); |
2074 | else |
2087 | else |
2075 | { |
2088 | { |
… | |
… | |
2089 | |
2102 | |
2090 | INTERFACE void |
2103 | INTERFACE void |
2091 | putcharacter (SCHEME_P_ int c) |
2104 | putcharacter (SCHEME_P_ int c) |
2092 | { |
2105 | { |
2093 | #if USE_PORTS |
2106 | #if USE_PORTS |
2094 | port *pt = SCHEME_V->outport->object.port; |
2107 | port *pt = port (SCHEME_V->outport); |
2095 | |
2108 | |
2096 | if (pt->kind & port_file) |
2109 | if (pt->kind & port_file) |
2097 | { |
2110 | { |
2098 | char cc = c; |
2111 | char cc = c; |
2099 | write (pt->rep.stdio.file, &cc, 1); |
2112 | write (pt->rep.stdio.file, &cc, 1); |
… | |
… | |
2173 | case '7': |
2186 | case '7': |
2174 | state = st_oct1; |
2187 | state = st_oct1; |
2175 | c1 = c - '0'; |
2188 | c1 = c - '0'; |
2176 | break; |
2189 | break; |
2177 | |
2190 | |
|
|
2191 | case 'a': *p++ = '\a'; state = st_ok; break; |
|
|
2192 | case 'n': *p++ = '\n'; state = st_ok; break; |
|
|
2193 | case 'r': *p++ = '\r'; state = st_ok; break; |
|
|
2194 | case 't': *p++ = '\t'; state = st_ok; break; |
|
|
2195 | |
|
|
2196 | //TODO: \whitespace eol whitespace |
|
|
2197 | |
|
|
2198 | //TODO: x should end in ;, not two-digit hex |
2178 | case 'x': |
2199 | case 'x': |
2179 | case 'X': |
2200 | case 'X': |
2180 | state = st_x1; |
2201 | state = st_x1; |
2181 | c1 = 0; |
2202 | c1 = 0; |
2182 | break; |
|
|
2183 | |
|
|
2184 | case 'n': |
|
|
2185 | *p++ = '\n'; |
|
|
2186 | state = st_ok; |
|
|
2187 | break; |
|
|
2188 | |
|
|
2189 | case 't': |
|
|
2190 | *p++ = '\t'; |
|
|
2191 | state = st_ok; |
|
|
2192 | break; |
|
|
2193 | |
|
|
2194 | case 'r': |
|
|
2195 | *p++ = '\r'; |
|
|
2196 | state = st_ok; |
|
|
2197 | break; |
2203 | break; |
2198 | |
2204 | |
2199 | default: |
2205 | default: |
2200 | *p++ = c; |
2206 | *p++ = c; |
2201 | state = st_ok; |
2207 | state = st_ok; |
… | |
… | |
2256 | |
2262 | |
2257 | /* check c is in chars */ |
2263 | /* check c is in chars */ |
2258 | ecb_inline int |
2264 | ecb_inline int |
2259 | is_one_of (const char *s, int c) |
2265 | is_one_of (const char *s, int c) |
2260 | { |
2266 | { |
2261 | if (c == EOF) |
|
|
2262 | return 1; |
|
|
2263 | |
|
|
2264 | return !!strchr (s, c); |
2267 | return c == EOF || !!strchr (s, c); |
2265 | } |
2268 | } |
2266 | |
2269 | |
2267 | /* skip white characters */ |
2270 | /* skip white characters */ |
2268 | ecb_inline int |
2271 | ecb_inline int |
2269 | skipspace (SCHEME_P) |
2272 | skipspace (SCHEME_P) |
… | |
… | |
2271 | int c, curr_line = 0; |
2274 | int c, curr_line = 0; |
2272 | |
2275 | |
2273 | do |
2276 | do |
2274 | { |
2277 | { |
2275 | c = inchar (SCHEME_A); |
2278 | c = inchar (SCHEME_A); |
|
|
2279 | |
2276 | #if SHOW_ERROR_LINE |
2280 | #if SHOW_ERROR_LINE |
2277 | if (c == '\n') |
2281 | if (ecb_expect_false (c == '\n')) |
2278 | curr_line++; |
2282 | curr_line++; |
2279 | #endif |
2283 | #endif |
|
|
2284 | |
|
|
2285 | if (ecb_expect_false (c == EOF)) |
|
|
2286 | return c; |
2280 | } |
2287 | } |
2281 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2288 | while (is_one_of (WHITESPACE, c)); |
2282 | |
2289 | |
2283 | /* record it */ |
2290 | /* record it */ |
2284 | #if SHOW_ERROR_LINE |
2291 | #if SHOW_ERROR_LINE |
2285 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2292 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2286 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2293 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2287 | #endif |
2294 | #endif |
2288 | |
2295 | |
2289 | if (c != EOF) |
|
|
2290 | { |
|
|
2291 | backchar (SCHEME_A_ c); |
2296 | backchar (SCHEME_A_ c); |
2292 | return 1; |
2297 | return 1; |
2293 | } |
|
|
2294 | else |
|
|
2295 | return EOF; |
|
|
2296 | } |
2298 | } |
2297 | |
2299 | |
2298 | /* get token */ |
2300 | /* get token */ |
2299 | static int |
2301 | static int |
2300 | token (SCHEME_P) |
2302 | token (SCHEME_P) |
… | |
… | |
2316 | return TOK_RPAREN; |
2318 | return TOK_RPAREN; |
2317 | |
2319 | |
2318 | case '.': |
2320 | case '.': |
2319 | c = inchar (SCHEME_A); |
2321 | c = inchar (SCHEME_A); |
2320 | |
2322 | |
2321 | if (is_one_of (" \n\t", c)) |
2323 | if (is_one_of (WHITESPACE, c)) |
2322 | return TOK_DOT; |
2324 | return TOK_DOT; |
2323 | else |
2325 | else |
2324 | { |
2326 | { |
2325 | backchar (SCHEME_A_ c); |
2327 | backchar (SCHEME_A_ c); |
2326 | return TOK_DOTATOM; |
2328 | return TOK_DOTATOM; |
… | |
… | |
2467 | } |
2469 | } |
2468 | |
2470 | |
2469 | putcharacter (SCHEME_A_ '"'); |
2471 | putcharacter (SCHEME_A_ '"'); |
2470 | } |
2472 | } |
2471 | |
2473 | |
2472 | |
|
|
2473 | /* print atoms */ |
2474 | /* print atoms */ |
2474 | static void |
2475 | static void |
2475 | printatom (SCHEME_P_ pointer l, int f) |
2476 | printatom (SCHEME_P_ pointer l, int f) |
2476 | { |
2477 | { |
2477 | char *p; |
2478 | char *p; |
2478 | int len; |
2479 | int len; |
2479 | |
2480 | |
2480 | atom2str (SCHEME_A_ l, f, &p, &len); |
2481 | atom2str (SCHEME_A_ l, f, &p, &len); |
2481 | putchars (SCHEME_A_ p, len); |
2482 | putchars (SCHEME_A_ p, len); |
2482 | } |
2483 | } |
2483 | |
|
|
2484 | |
2484 | |
2485 | /* Uses internal buffer unless string pointer is already available */ |
2485 | /* Uses internal buffer unless string pointer is already available */ |
2486 | static void |
2486 | static void |
2487 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2487 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2488 | { |
2488 | { |
… | |
… | |
2649 | #endif |
2649 | #endif |
2650 | } |
2650 | } |
2651 | else if (is_continuation (l)) |
2651 | else if (is_continuation (l)) |
2652 | p = "#<CONTINUATION>"; |
2652 | p = "#<CONTINUATION>"; |
2653 | else |
2653 | else |
|
|
2654 | { |
|
|
2655 | #if USE_PRINTF |
|
|
2656 | p = SCHEME_V->strbuff; |
|
|
2657 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2658 | #else |
2654 | p = "#<ERROR>"; |
2659 | p = "#<ERROR>"; |
|
|
2660 | #endif |
|
|
2661 | } |
2655 | |
2662 | |
2656 | *pp = p; |
2663 | *pp = p; |
2657 | *plen = strlen (p); |
2664 | *plen = strlen (p); |
2658 | } |
2665 | } |
2659 | |
2666 | |
… | |
… | |
2929 | #endif /* USE_ALIST_ENV else */ |
2936 | #endif /* USE_ALIST_ENV else */ |
2930 | |
2937 | |
2931 | ecb_inline void |
2938 | ecb_inline void |
2932 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2939 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2933 | { |
2940 | { |
|
|
2941 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2934 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2942 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2935 | } |
2943 | } |
2936 | |
2944 | |
2937 | ecb_inline void |
2945 | ecb_inline void |
2938 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2946 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
… | |
… | |
3039 | { |
3047 | { |
3040 | int nframes = (uintptr_t)SCHEME_V->dump; |
3048 | int nframes = (uintptr_t)SCHEME_V->dump; |
3041 | struct dump_stack_frame *next_frame; |
3049 | struct dump_stack_frame *next_frame; |
3042 | |
3050 | |
3043 | /* enough room for the next frame? */ |
3051 | /* enough room for the next frame? */ |
3044 | if (nframes >= SCHEME_V->dump_size) |
3052 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3045 | { |
3053 | { |
3046 | SCHEME_V->dump_size += STACK_GROWTH; |
3054 | SCHEME_V->dump_size += STACK_GROWTH; |
3047 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3055 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3048 | } |
3056 | } |
3049 | |
3057 | |
… | |
… | |
3230 | |
3238 | |
3231 | #endif |
3239 | #endif |
3232 | |
3240 | |
3233 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3241 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3234 | |
3242 | |
|
|
3243 | #if EXPERIMENT |
|
|
3244 | static int |
|
|
3245 | debug (SCHEME_P_ int indent, pointer x) |
|
|
3246 | { |
|
|
3247 | int c; |
|
|
3248 | |
|
|
3249 | if (is_syntax (x)) |
|
|
3250 | { |
|
|
3251 | printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x)); |
|
|
3252 | return 8 + 8; |
|
|
3253 | } |
|
|
3254 | |
|
|
3255 | if (x == NIL) |
|
|
3256 | { |
|
|
3257 | printf ("%*sNIL\n", indent, ""); |
|
|
3258 | return 3; |
|
|
3259 | } |
|
|
3260 | |
|
|
3261 | switch (type (x)) |
|
|
3262 | { |
|
|
3263 | case T_INTEGER: |
|
|
3264 | printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x); |
|
|
3265 | return 32+8; |
|
|
3266 | |
|
|
3267 | case T_SYMBOL: |
|
|
3268 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
|
|
3269 | return 24+8; |
|
|
3270 | |
|
|
3271 | case T_CLOSURE: |
|
|
3272 | printf ("%*sS<%s>\n", indent, "", "closure"); |
|
|
3273 | debug (SCHEME_A_ indent + 3, cdr(x)); |
|
|
3274 | return 32 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3275 | |
|
|
3276 | case T_PAIR: |
|
|
3277 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
|
|
3278 | c = debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3279 | c += debug (SCHEME_A_ indent + 3, cdr (x)); |
|
|
3280 | return c + 1; |
|
|
3281 | |
|
|
3282 | case T_PORT: |
|
|
3283 | printf ("%*sS<%s>\n", indent, "", "port"); |
|
|
3284 | return 24+8; |
|
|
3285 | |
|
|
3286 | case T_VECTOR: |
|
|
3287 | printf ("%*sS<%s>\n", indent, "", "vector"); |
|
|
3288 | return 24+8; |
|
|
3289 | |
|
|
3290 | case T_ENVIRONMENT: |
|
|
3291 | printf ("%*sS<%s>\n", indent, "", "environment"); |
|
|
3292 | return 0 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3293 | |
|
|
3294 | default: |
|
|
3295 | printf ("unhandled type %d\n", type (x)); |
|
|
3296 | break; |
|
|
3297 | } |
|
|
3298 | } |
|
|
3299 | #endif |
|
|
3300 | |
3235 | static int |
3301 | static int |
3236 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3302 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3237 | { |
3303 | { |
3238 | pointer args = SCHEME_V->args; |
3304 | pointer args = SCHEME_V->args; |
3239 | pointer x, y; |
3305 | pointer x, y; |
3240 | |
3306 | |
3241 | switch (op) |
3307 | switch (op) |
3242 | { |
3308 | { |
|
|
3309 | #if EXPERIMENT //D |
|
|
3310 | case OP_DEBUG: |
|
|
3311 | printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); |
|
|
3312 | printf ("\n"); |
|
|
3313 | s_return (S_T); |
|
|
3314 | #endif |
3243 | case OP_LOAD: /* load */ |
3315 | case OP_LOAD: /* load */ |
3244 | if (file_interactive (SCHEME_A)) |
3316 | if (file_interactive (SCHEME_A)) |
3245 | { |
3317 | { |
3246 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3318 | putstr (SCHEME_A_ "Loading "); putstr (SCHEME_A_ strvalue (car (args))); putstr (SCHEME_A_ "\n"); |
3247 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3319 | //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3248 | } |
3320 | } |
3249 | |
3321 | |
3250 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3322 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3251 | Error_1 ("unable to open", car (args)); |
3323 | Error_1 ("unable to open", car (args)); |
3252 | else |
3324 | else |
… | |
… | |
3256 | } |
3328 | } |
3257 | |
3329 | |
3258 | case OP_T0LVL: /* top level */ |
3330 | case OP_T0LVL: /* top level */ |
3259 | |
3331 | |
3260 | /* If we reached the end of file, this loop is done. */ |
3332 | /* If we reached the end of file, this loop is done. */ |
3261 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3333 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3262 | { |
3334 | { |
3263 | if (SCHEME_V->file_i == 0) |
3335 | if (SCHEME_V->file_i == 0) |
3264 | { |
3336 | { |
3265 | SCHEME_V->args = NIL; |
3337 | SCHEME_V->args = NIL; |
3266 | s_goto (OP_QUIT); |
3338 | s_goto (OP_QUIT); |
… | |
… | |
3344 | #endif |
3416 | #endif |
3345 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3417 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3346 | { |
3418 | { |
3347 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3419 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3348 | |
3420 | |
3349 | if (x != NIL) |
3421 | if (x == NIL) |
3350 | s_return (slot_value_in_env (x)); |
|
|
3351 | else |
|
|
3352 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3422 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
|
|
3423 | |
|
|
3424 | s_return (slot_value_in_env (x)); |
3353 | } |
3425 | } |
3354 | else if (is_pair (SCHEME_V->code)) |
3426 | else if (is_pair (SCHEME_V->code)) |
3355 | { |
3427 | { |
3356 | x = car (SCHEME_V->code); |
3428 | x = car (SCHEME_V->code); |
3357 | |
3429 | |
… | |
… | |
3370 | } |
3442 | } |
3371 | else |
3443 | else |
3372 | s_return (SCHEME_V->code); |
3444 | s_return (SCHEME_V->code); |
3373 | |
3445 | |
3374 | case OP_E0ARGS: /* eval arguments */ |
3446 | case OP_E0ARGS: /* eval arguments */ |
3375 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3447 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3376 | { |
3448 | { |
3377 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3449 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3378 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3450 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3379 | SCHEME_V->code = SCHEME_V->value; |
3451 | SCHEME_V->code = SCHEME_V->value; |
3380 | s_goto (OP_APPLY); |
3452 | s_goto (OP_APPLY); |
… | |
… | |
3434 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3506 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3435 | else if (is_foreign (SCHEME_V->code)) |
3507 | else if (is_foreign (SCHEME_V->code)) |
3436 | { |
3508 | { |
3437 | /* Keep nested calls from GC'ing the arglist */ |
3509 | /* Keep nested calls from GC'ing the arglist */ |
3438 | push_recent_alloc (SCHEME_A_ args, NIL); |
3510 | push_recent_alloc (SCHEME_A_ args, NIL); |
3439 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3511 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3440 | |
3512 | |
3441 | s_return (x); |
3513 | s_return (x); |
3442 | } |
3514 | } |
3443 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3515 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3444 | { |
3516 | { |
… | |
… | |
3481 | |
3553 | |
3482 | case OP_DOMACRO: /* do macro */ |
3554 | case OP_DOMACRO: /* do macro */ |
3483 | SCHEME_V->code = SCHEME_V->value; |
3555 | SCHEME_V->code = SCHEME_V->value; |
3484 | s_goto (OP_EVAL); |
3556 | s_goto (OP_EVAL); |
3485 | |
3557 | |
3486 | #if 1 |
|
|
3487 | |
|
|
3488 | case OP_LAMBDA: /* lambda */ |
3558 | case OP_LAMBDA: /* lambda */ |
3489 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3559 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3490 | set SCHEME_V->value fall thru */ |
3560 | set SCHEME_V->value fall thru */ |
3491 | { |
3561 | { |
3492 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3562 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
… | |
… | |
3498 | SCHEME_V->code = slot_value_in_env (f); |
3568 | SCHEME_V->code = slot_value_in_env (f); |
3499 | s_goto (OP_APPLY); |
3569 | s_goto (OP_APPLY); |
3500 | } |
3570 | } |
3501 | |
3571 | |
3502 | SCHEME_V->value = SCHEME_V->code; |
3572 | SCHEME_V->value = SCHEME_V->code; |
3503 | /* Fallthru */ |
|
|
3504 | } |
3573 | } |
|
|
3574 | /* Fallthru */ |
3505 | |
3575 | |
3506 | case OP_LAMBDA1: |
3576 | case OP_LAMBDA1: |
3507 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3577 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3508 | |
|
|
3509 | #else |
|
|
3510 | |
|
|
3511 | case OP_LAMBDA: /* lambda */ |
|
|
3512 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
|
|
3513 | |
|
|
3514 | #endif |
|
|
3515 | |
3578 | |
3516 | case OP_MKCLOSURE: /* make-closure */ |
3579 | case OP_MKCLOSURE: /* make-closure */ |
3517 | x = car (args); |
3580 | x = car (args); |
3518 | |
3581 | |
3519 | if (car (x) == SCHEME_V->LAMBDA) |
3582 | if (car (x) == SCHEME_V->LAMBDA) |
… | |
… | |
3940 | |
4003 | |
3941 | switch (op) |
4004 | switch (op) |
3942 | { |
4005 | { |
3943 | #if USE_MATH |
4006 | #if USE_MATH |
3944 | case OP_INEX2EX: /* inexact->exact */ |
4007 | case OP_INEX2EX: /* inexact->exact */ |
3945 | { |
|
|
3946 | if (is_integer (x)) |
4008 | if (!is_integer (x)) |
3947 | s_return (x); |
4009 | { |
3948 | |
|
|
3949 | RVALUE r = rvalue_unchecked (x); |
4010 | RVALUE r = rvalue_unchecked (x); |
3950 | |
4011 | |
3951 | if (r == (RVALUE)(IVALUE)r) |
4012 | if (r == (RVALUE)(IVALUE)r) |
3952 | s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); |
4013 | x = mk_integer (SCHEME_A_ rvalue_unchecked (x)); |
3953 | else |
4014 | else |
3954 | Error_1 ("inexact->exact: not integral:", x); |
4015 | Error_1 ("inexact->exact: not integral:", x); |
3955 | } |
4016 | } |
3956 | |
4017 | |
|
|
4018 | s_return (x); |
|
|
4019 | |
|
|
4020 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4021 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4022 | case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x)))); |
|
|
4023 | case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x)))); |
|
|
4024 | |
|
|
4025 | case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
3957 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4026 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
3958 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); |
4027 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)) |
|
|
4028 | / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args)))))); |
3959 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4029 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
3960 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
4030 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
3961 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
4031 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
3962 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
4032 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
3963 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4033 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
3964 | |
4034 | |
3965 | case OP_ATAN: |
4035 | case OP_ATAN: |
|
|
4036 | s_return (mk_real (SCHEME_A_ |
3966 | if (cdr (args) == NIL) |
4037 | cdr (args) == NIL |
3967 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4038 | ? atan (rvalue (x)) |
3968 | else |
4039 | : atan2 (rvalue (x), rvalue (cadr (args))))); |
3969 | { |
|
|
3970 | pointer y = cadr (args); |
|
|
3971 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
|
|
3972 | } |
|
|
3973 | |
|
|
3974 | case OP_SQRT: |
|
|
3975 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
|
|
3976 | |
4040 | |
3977 | case OP_EXPT: |
4041 | case OP_EXPT: |
3978 | { |
4042 | { |
3979 | RVALUE result; |
4043 | RVALUE result; |
3980 | int real_result = 1; |
4044 | int real_result = 1; |
… | |
… | |
4003 | if (real_result) |
4067 | if (real_result) |
4004 | s_return (mk_real (SCHEME_A_ result)); |
4068 | s_return (mk_real (SCHEME_A_ result)); |
4005 | else |
4069 | else |
4006 | s_return (mk_integer (SCHEME_A_ result)); |
4070 | s_return (mk_integer (SCHEME_A_ result)); |
4007 | } |
4071 | } |
4008 | |
|
|
4009 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4010 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4011 | |
|
|
4012 | case OP_TRUNCATE: |
|
|
4013 | { |
|
|
4014 | RVALUE n = rvalue (x); |
|
|
4015 | s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n))); |
|
|
4016 | } |
|
|
4017 | |
|
|
4018 | case OP_ROUND: |
|
|
4019 | if (is_integer (x)) |
|
|
4020 | s_return (x); |
|
|
4021 | |
|
|
4022 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
|
|
4023 | #endif |
4072 | #endif |
4024 | |
4073 | |
4025 | case OP_ADD: /* + */ |
4074 | case OP_ADD: /* + */ |
4026 | v = num_zero; |
4075 | v = num_zero; |
4027 | |
4076 | |
… | |
… | |
4115 | else |
4164 | else |
4116 | Error_0 ("modulo: division by zero"); |
4165 | Error_0 ("modulo: division by zero"); |
4117 | |
4166 | |
4118 | s_return (mk_number (SCHEME_A_ v)); |
4167 | s_return (mk_number (SCHEME_A_ v)); |
4119 | |
4168 | |
4120 | case OP_CAR: /* car */ |
4169 | /* the compiler will optimize this mess... */ |
4121 | s_return (caar (args)); |
4170 | case OP_CAR: op_car: s_return (car (x)); |
4122 | |
4171 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4123 | case OP_CDR: /* cdr */ |
4172 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4124 | s_return (cdar (args)); |
4173 | case OP_CADR: op_cadr: x = cdr (x); goto op_car; |
|
|
4174 | case OP_CDAR: op_cdar: x = car (x); goto op_cdr; |
|
|
4175 | case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr; |
|
|
4176 | case OP_CAAAR: op_caaar: x = car (x); goto op_caar; |
|
|
4177 | case OP_CAADR: op_caadr: x = cdr (x); goto op_caar; |
|
|
4178 | case OP_CADAR: op_cadar: x = car (x); goto op_cadr; |
|
|
4179 | case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr; |
|
|
4180 | case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar; |
|
|
4181 | case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar; |
|
|
4182 | case OP_CDDAR: op_cddar: x = car (x); goto op_cddr; |
|
|
4183 | case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr; |
|
|
4184 | case OP_CAAAAR: x = car (x); goto op_caaar; |
|
|
4185 | case OP_CAAADR: x = cdr (x); goto op_caaar; |
|
|
4186 | case OP_CAADAR: x = car (x); goto op_caadr; |
|
|
4187 | case OP_CAADDR: x = cdr (x); goto op_caadr; |
|
|
4188 | case OP_CADAAR: x = car (x); goto op_cadar; |
|
|
4189 | case OP_CADADR: x = cdr (x); goto op_cadar; |
|
|
4190 | case OP_CADDAR: x = car (x); goto op_caddr; |
|
|
4191 | case OP_CADDDR: x = cdr (x); goto op_caddr; |
|
|
4192 | case OP_CDAAAR: x = car (x); goto op_cdaar; |
|
|
4193 | case OP_CDAADR: x = cdr (x); goto op_cdaar; |
|
|
4194 | case OP_CDADAR: x = car (x); goto op_cdadr; |
|
|
4195 | case OP_CDADDR: x = cdr (x); goto op_cdadr; |
|
|
4196 | case OP_CDDAAR: x = car (x); goto op_cddar; |
|
|
4197 | case OP_CDDADR: x = cdr (x); goto op_cddar; |
|
|
4198 | case OP_CDDDAR: x = car (x); goto op_cdddr; |
|
|
4199 | case OP_CDDDDR: x = cdr (x); goto op_cdddr; |
4125 | |
4200 | |
4126 | case OP_CONS: /* cons */ |
4201 | case OP_CONS: /* cons */ |
4127 | set_cdr (args, cadr (args)); |
4202 | set_cdr (args, cadr (args)); |
4128 | s_return (args); |
4203 | s_return (args); |
4129 | |
4204 | |
… | |
… | |
4303 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4378 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4304 | |
4379 | |
4305 | s_return (newstr); |
4380 | s_return (newstr); |
4306 | } |
4381 | } |
4307 | |
4382 | |
4308 | case OP_SUBSTR: /* substring */ |
4383 | case OP_STRING_COPY: /* substring/string-copy */ |
4309 | { |
4384 | { |
4310 | char *str = strvalue (x); |
4385 | char *str = strvalue (x); |
4311 | int index0 = ivalue_unchecked (cadr (args)); |
4386 | int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args)); |
4312 | int index1; |
4387 | int index1; |
4313 | int len; |
4388 | int len; |
4314 | |
4389 | |
4315 | if (index0 > strlength (x)) |
4390 | if (index0 > strlength (x)) |
4316 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4391 | Error_1 ("string->copy: start out of bounds:", cadr (args)); |
4317 | |
4392 | |
4318 | if (cddr (args) != NIL) |
4393 | if (cddr (args) != NIL) |
4319 | { |
4394 | { |
4320 | index1 = ivalue_unchecked (caddr (args)); |
4395 | index1 = ivalue_unchecked (caddr (args)); |
4321 | |
4396 | |
4322 | if (index1 > strlength (x) || index1 < index0) |
4397 | if (index1 > strlength (x) || index1 < index0) |
4323 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4398 | Error_1 ("string->copy: end out of bounds:", caddr (args)); |
4324 | } |
4399 | } |
4325 | else |
4400 | else |
4326 | index1 = strlength (x); |
4401 | index1 = strlength (x); |
4327 | |
4402 | |
4328 | len = index1 - index0; |
4403 | len = index1 - index0; |
4329 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4404 | x = mk_counted_string (SCHEME_A_ str + index0, len); |
4330 | memcpy (strvalue (x), str + index0, len); |
|
|
4331 | strvalue (x)[len] = 0; |
|
|
4332 | |
4405 | |
4333 | s_return (x); |
4406 | s_return (x); |
4334 | } |
4407 | } |
4335 | |
4408 | |
4336 | case OP_VECTOR: /* vector */ |
4409 | case OP_VECTOR: /* vector */ |
… | |
… | |
4377 | s_return (vec); |
4450 | s_return (vec); |
4378 | } |
4451 | } |
4379 | |
4452 | |
4380 | case OP_VECLEN: /* vector-length */ |
4453 | case OP_VECLEN: /* vector-length */ |
4381 | 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); |
4382 | |
4459 | |
4383 | case OP_VECREF: /* vector-ref */ |
4460 | case OP_VECREF: /* vector-ref */ |
4384 | { |
4461 | { |
4385 | int index = ivalue_unchecked (cadr (args)); |
4462 | int index = ivalue_unchecked (cadr (args)); |
4386 | |
4463 | |
… | |
… | |
4447 | pointer d = cdr (args); |
4524 | pointer d = cdr (args); |
4448 | int r; |
4525 | int r; |
4449 | |
4526 | |
4450 | switch (op) |
4527 | switch (op) |
4451 | { |
4528 | { |
4452 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4529 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4453 | 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; |
4454 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4531 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4455 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4532 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4456 | 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; |
4457 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4535 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4458 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4536 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4459 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4537 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4460 | 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 */ |
4461 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4539 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4462 | |
4540 | |
4463 | #if USE_CHAR_CLASSIFIERS |
4541 | #if USE_CHAR_CLASSIFIERS |
4464 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4542 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4465 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4543 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4466 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4544 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
… | |
… | |
4516 | } |
4594 | } |
4517 | else |
4595 | else |
4518 | s_return (SCHEME_V->code); |
4596 | s_return (SCHEME_V->code); |
4519 | |
4597 | |
4520 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4598 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4521 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4599 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4522 | s_return (SCHEME_V->value); |
4600 | s_return (SCHEME_V->value); |
4523 | |
4601 | |
4524 | #if USE_PORTS |
4602 | #if USE_PORTS |
|
|
4603 | |
|
|
4604 | case OP_EOF_OBJECT: /* eof-object */ |
|
|
4605 | s_return (S_EOF); |
4525 | |
4606 | |
4526 | case OP_WRITE: /* write */ |
4607 | case OP_WRITE: /* write */ |
4527 | case OP_DISPLAY: /* display */ |
4608 | case OP_DISPLAY: /* display */ |
4528 | case OP_WRITE_CHAR: /* write-char */ |
4609 | case OP_WRITE_CHAR: /* write-char */ |
4529 | if (is_pair (cdr (SCHEME_V->args))) |
4610 | if (is_pair (cdr (SCHEME_V->args))) |
… | |
… | |
4671 | SCHEME_V->gc_verbose = (a != S_F); |
4752 | SCHEME_V->gc_verbose = (a != S_F); |
4672 | s_retbool (was); |
4753 | s_retbool (was); |
4673 | } |
4754 | } |
4674 | |
4755 | |
4675 | case OP_NEWSEGMENT: /* new-segment */ |
4756 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
4757 | #if 0 |
4676 | if (!is_pair (args) || !is_number (a)) |
4758 | if (!is_pair (args) || !is_number (a)) |
4677 | Error_0 ("new-segment: argument must be a number"); |
4759 | Error_0 ("new-segment: argument must be a number"); |
4678 | |
4760 | #endif |
4679 | alloc_cellseg (SCHEME_A_ ivalue (a)); |
4761 | s_retbool (alloc_cellseg (SCHEME_A)); |
4680 | |
|
|
4681 | s_return (S_T); |
|
|
4682 | |
4762 | |
4683 | case OP_OBLIST: /* oblist */ |
4763 | case OP_OBLIST: /* oblist */ |
4684 | s_return (oblist_all_symbols (SCHEME_A)); |
4764 | s_return (oblist_all_symbols (SCHEME_A)); |
4685 | |
4765 | |
4686 | #if USE_PORTS |
4766 | #if USE_PORTS |
… | |
… | |
4756 | s_return (p == NIL ? S_F : p); |
4836 | s_return (p == NIL ? S_F : p); |
4757 | } |
4837 | } |
4758 | |
4838 | |
4759 | case OP_GET_OUTSTRING: /* get-output-string */ |
4839 | case OP_GET_OUTSTRING: /* get-output-string */ |
4760 | { |
4840 | { |
4761 | port *p; |
4841 | port *p = port (a); |
4762 | |
4842 | |
4763 | if ((p = a->object.port)->kind & port_string) |
4843 | if (p->kind & port_string) |
4764 | { |
4844 | { |
4765 | off_t size; |
4845 | off_t size; |
4766 | char *str; |
4846 | char *str; |
4767 | |
4847 | |
4768 | size = p->rep.string.curr - p->rep.string.start + 1; |
4848 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4873 | int res; |
4953 | int res; |
4874 | |
4954 | |
4875 | if (is_pair (args)) |
4955 | if (is_pair (args)) |
4876 | p = car (args); |
4956 | p = car (args); |
4877 | |
4957 | |
4878 | res = p->object.port->kind & port_string; |
4958 | res = port (p)->kind & port_string; |
4879 | |
4959 | |
4880 | s_retbool (res); |
4960 | s_retbool (res); |
4881 | } |
4961 | } |
4882 | |
4962 | |
4883 | case OP_SET_INPORT: /* set-input-port */ |
4963 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
4947 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); |
5027 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); |
4948 | |
5028 | |
4949 | case TOK_DOTATOM: |
5029 | case TOK_DOTATOM: |
4950 | SCHEME_V->strbuff[0] = '.'; |
5030 | SCHEME_V->strbuff[0] = '.'; |
4951 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
5031 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
|
|
5032 | |
|
|
5033 | case TOK_STRATOM: |
|
|
5034 | x = readstrexp (SCHEME_A_ '|'); |
|
|
5035 | //TODO: haven't checked whether the garbage collector could interfere |
|
|
5036 | s_return (mk_atom (SCHEME_A_ strvalue (x))); |
4952 | |
5037 | |
4953 | case TOK_DQUOTE: |
5038 | case TOK_DQUOTE: |
4954 | x = readstrexp (SCHEME_A_ '"'); |
5039 | x = readstrexp (SCHEME_A_ '"'); |
4955 | |
5040 | |
4956 | if (x == S_F) |
5041 | if (x == S_F) |
… | |
… | |
5203 | |
5288 | |
5204 | case OP_CLOSUREP: /* closure? */ |
5289 | case OP_CLOSUREP: /* closure? */ |
5205 | /* |
5290 | /* |
5206 | * Note, macro object is also a closure. |
5291 | * Note, macro object is also a closure. |
5207 | * Therefore, (closure? <#MACRO>) ==> #t |
5292 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5293 | * (schmorp) well, obviously not, fix? TODO |
5208 | */ |
5294 | */ |
5209 | s_retbool (is_closure (a)); |
5295 | s_retbool (is_closure (a)); |
5210 | |
5296 | |
5211 | case OP_MACROP: /* macro? */ |
5297 | case OP_MACROP: /* macro? */ |
5212 | s_retbool (is_macro (a)); |
5298 | s_retbool (is_macro (a)); |
… | |
… | |
5417 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5503 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5418 | return; |
5504 | return; |
5419 | |
5505 | |
5420 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5506 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5421 | { |
5507 | { |
5422 | xwrstr ("No memory!\n"); |
5508 | putstr (SCHEME_A_ "No memory!\n"); |
5423 | return; |
5509 | return; |
5424 | } |
5510 | } |
5425 | } |
5511 | } |
5426 | } |
5512 | } |
5427 | |
5513 | |
… | |
… | |
5453 | |
5539 | |
5454 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5540 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5455 | static int |
5541 | static int |
5456 | syntaxnum (pointer p) |
5542 | syntaxnum (pointer p) |
5457 | { |
5543 | { |
5458 | const char *s = strvalue (car (p)); |
5544 | const char *s = strvalue (p); |
5459 | |
5545 | |
5460 | switch (strlength (car (p))) |
5546 | switch (strlength (p)) |
5461 | { |
5547 | { |
5462 | case 2: |
5548 | case 2: |
5463 | if (s[0] == 'i') |
5549 | if (s[0] == 'i') |
5464 | return OP_IF0; /* if */ |
5550 | return OP_IF0; /* if */ |
5465 | else |
5551 | else |
… | |
… | |
5540 | ecb_cold int |
5626 | ecb_cold int |
5541 | scheme_init (SCHEME_P) |
5627 | scheme_init (SCHEME_P) |
5542 | { |
5628 | { |
5543 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5629 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5544 | pointer x; |
5630 | pointer x; |
|
|
5631 | |
|
|
5632 | /* this memset is not strictly correct, as we assume (intcache) |
|
|
5633 | * that memset 0 will also set pointers to 0, but memset does |
|
|
5634 | * of course not guarantee that. screw such systems. |
|
|
5635 | */ |
|
|
5636 | memset (SCHEME_V, 0, sizeof (*SCHEME_V)); |
5545 | |
5637 | |
5546 | num_set_fixnum (num_zero, 1); |
5638 | num_set_fixnum (num_zero, 1); |
5547 | num_set_ivalue (num_zero, 0); |
5639 | num_set_ivalue (num_zero, 0); |
5548 | num_set_fixnum (num_one, 1); |
5640 | num_set_fixnum (num_one, 1); |
5549 | num_set_ivalue (num_one, 1); |
5641 | num_set_ivalue (num_one, 1); |
… | |
… | |
5561 | SCHEME_V->save_inport = NIL; |
5653 | SCHEME_V->save_inport = NIL; |
5562 | SCHEME_V->loadport = NIL; |
5654 | SCHEME_V->loadport = NIL; |
5563 | SCHEME_V->nesting = 0; |
5655 | SCHEME_V->nesting = 0; |
5564 | SCHEME_V->interactive_repl = 0; |
5656 | SCHEME_V->interactive_repl = 0; |
5565 | |
5657 | |
5566 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5658 | if (!alloc_cellseg (SCHEME_A)) |
5567 | { |
5659 | { |
5568 | #if USE_ERROR_CHECKING |
5660 | #if USE_ERROR_CHECKING |
5569 | SCHEME_V->no_memory = 1; |
5661 | SCHEME_V->no_memory = 1; |
5570 | return 0; |
5662 | return 0; |
5571 | #endif |
5663 | #endif |
… | |
… | |
5901 | # endif |
5993 | # endif |
5902 | int fin; |
5994 | int fin; |
5903 | char *file_name = InitFile; |
5995 | char *file_name = InitFile; |
5904 | int retcode; |
5996 | int retcode; |
5905 | int isfile = 1; |
5997 | int isfile = 1; |
|
|
5998 | system ("ps v $PPID");//D |
5906 | |
5999 | |
5907 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6000 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5908 | { |
6001 | { |
5909 | xwrstr ("Usage: tinyscheme -?\n"); |
6002 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
5910 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6003 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
5911 | xwrstr ("followed by\n"); |
6004 | putstr (SCHEME_A_ "followed by\n"); |
5912 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6005 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
5913 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6006 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
5914 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6007 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
5915 | xwrstr ("Use - as filename for stdin.\n"); |
6008 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
5916 | return 1; |
6009 | return 1; |
5917 | } |
6010 | } |
5918 | |
6011 | |
5919 | if (!scheme_init (SCHEME_A)) |
6012 | if (!scheme_init (SCHEME_A)) |
5920 | { |
6013 | { |
5921 | xwrstr ("Could not initialize!\n"); |
6014 | putstr (SCHEME_A_ "Could not initialize!\n"); |
5922 | return 2; |
6015 | return 2; |
5923 | } |
6016 | } |
5924 | |
6017 | |
5925 | # if USE_PORTS |
6018 | # if USE_PORTS |
5926 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6019 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
5971 | fin = open (file_name, O_RDONLY); |
6064 | fin = open (file_name, O_RDONLY); |
5972 | #endif |
6065 | #endif |
5973 | |
6066 | |
5974 | if (isfile && fin < 0) |
6067 | if (isfile && fin < 0) |
5975 | { |
6068 | { |
5976 | xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); |
6069 | putstr (SCHEME_A_ "Could not open file "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); |
5977 | } |
6070 | } |
5978 | else |
6071 | else |
5979 | { |
6072 | { |
5980 | if (isfile) |
6073 | if (isfile) |
5981 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6074 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
… | |
… | |
5985 | #if USE_PORTS |
6078 | #if USE_PORTS |
5986 | if (!isfile || fin != STDIN_FILENO) |
6079 | if (!isfile || fin != STDIN_FILENO) |
5987 | { |
6080 | { |
5988 | if (SCHEME_V->retcode != 0) |
6081 | if (SCHEME_V->retcode != 0) |
5989 | { |
6082 | { |
5990 | xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); |
6083 | putstr (SCHEME_A_ "Errors encountered reading "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); |
5991 | } |
6084 | } |
5992 | |
6085 | |
5993 | if (isfile) |
6086 | if (isfile) |
5994 | close (fin); |
6087 | close (fin); |
5995 | } |
6088 | } |