… | |
… | |
18 | * |
18 | * |
19 | */ |
19 | */ |
20 | |
20 | |
21 | #define EXPERIMENT 1 |
21 | #define EXPERIMENT 1 |
22 | |
22 | |
|
|
23 | #if 1 |
23 | #define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
24 | #define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
24 | #include "malloc.c" |
25 | #include "malloc.c" |
|
|
26 | #endif |
25 | |
27 | |
26 | #define SCHEME_SOURCE |
28 | #define SCHEME_SOURCE |
27 | #include "scheme-private.h" |
29 | #include "scheme-private.h" |
28 | #ifndef WIN32 |
30 | #ifndef WIN32 |
29 | # include <unistd.h> |
31 | # include <unistd.h> |
… | |
… | |
79 | |
81 | |
80 | #define BACKQUOTE '`' |
82 | #define BACKQUOTE '`' |
81 | #define WHITESPACE " \t\r\n\v\f" |
83 | #define WHITESPACE " \t\r\n\v\f" |
82 | #define DELIMITERS "()\";" WHITESPACE |
84 | #define DELIMITERS "()\";" WHITESPACE |
83 | |
85 | |
84 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
86 | #define NIL POINTER (&SCHEME_V->xNIL) |
85 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
87 | #define S_T POINTER (&SCHEME_V->xT) |
86 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
88 | #define S_F POINTER (&SCHEME_V->xF) |
87 | #define S_SINK (&SCHEME_V->xsink) |
89 | #define S_SINK POINTER (&SCHEME_V->xsink) |
88 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
90 | #define S_EOF POINTER (&SCHEME_V->xEOF_OBJ) |
89 | |
91 | |
90 | #if !USE_MULTIPLICITY |
92 | #if !USE_MULTIPLICITY |
91 | static scheme sc; |
93 | static scheme sc; |
92 | #endif |
94 | #endif |
93 | |
95 | |
… | |
… | |
101 | } |
103 | } |
102 | |
104 | |
103 | char *p = s; |
105 | char *p = s; |
104 | |
106 | |
105 | do { |
107 | do { |
106 | *p++ = '0' + n % base; |
108 | *p++ = "0123456789abcdef"[n % base]; |
107 | n /= base; |
109 | n /= base; |
108 | } while (n); |
110 | } while (n); |
109 | |
111 | |
110 | *p-- = 0; |
112 | *p-- = 0; |
111 | |
113 | |
… | |
… | |
121 | { |
123 | { |
122 | xbase (s, n, 10); |
124 | xbase (s, n, 10); |
123 | } |
125 | } |
124 | |
126 | |
125 | static void |
127 | static void |
126 | xwrstr (const char *s) |
128 | putnum (SCHEME_P_ long n) |
127 | { |
|
|
128 | write (1, s, strlen (s)); |
|
|
129 | } |
|
|
130 | |
|
|
131 | static void |
|
|
132 | xwrnum (long n) |
|
|
133 | { |
129 | { |
134 | char buf[64]; |
130 | char buf[64]; |
135 | |
131 | |
136 | xnum (buf, n); |
132 | xnum (buf, n); |
137 | xwrstr (buf); |
133 | putstr (SCHEME_A_ buf); |
138 | } |
134 | } |
139 | |
135 | |
140 | static char |
136 | static char |
141 | xtoupper (char c) |
137 | xtoupper (char c) |
142 | { |
138 | { |
… | |
… | |
192 | # define prompt "ts> " |
188 | # define prompt "ts> " |
193 | #endif |
189 | #endif |
194 | |
190 | |
195 | #ifndef InitFile |
191 | #ifndef InitFile |
196 | # define InitFile "init.scm" |
192 | # define InitFile "init.scm" |
197 | #endif |
|
|
198 | |
|
|
199 | #ifndef FIRST_CELLSEGS |
|
|
200 | # define FIRST_CELLSEGS 3 |
|
|
201 | #endif |
193 | #endif |
202 | |
194 | |
203 | enum scheme_types |
195 | enum scheme_types |
204 | { |
196 | { |
205 | T_INTEGER, |
197 | T_INTEGER, |
… | |
… | |
258 | 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); |
259 | static num num_intdiv (num a, num b); |
251 | static num num_intdiv (num a, num b); |
260 | static num num_rem (num a, num b); |
252 | static num num_rem (num a, num b); |
261 | static num num_mod (num a, num b); |
253 | static num num_mod (num a, num b); |
262 | |
254 | |
263 | #if USE_MATH |
|
|
264 | static double round_per_R5RS (double x); |
|
|
265 | #endif |
|
|
266 | static int is_zero_rvalue (RVALUE x); |
255 | static int is_zero_rvalue (RVALUE x); |
267 | |
256 | |
268 | static num num_zero; |
257 | static num num_zero; |
269 | static num num_one; |
258 | static num num_one; |
270 | |
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 | |
271 | /* macros for cell operations */ |
264 | /* macros for cell operations */ |
272 | #define typeflag(p) ((p)->flag + 0) |
265 | #define typeflag(p) (CELL(p)->flag + 0) |
273 | #define set_typeflag(p,v) ((p)->flag = (v)) |
266 | #define set_typeflag(p,v) (CELL(p)->flag = (v)) |
274 | #define type(p) (typeflag (p) & T_MASKTYPE) |
267 | #define type(p) (typeflag (p) & T_MASKTYPE) |
275 | |
268 | |
276 | INTERFACE int |
269 | INTERFACE int |
277 | is_string (pointer p) |
270 | is_string (pointer p) |
278 | { |
271 | { |
279 | return type (p) == T_STRING; |
272 | return type (p) == T_STRING; |
280 | } |
273 | } |
281 | |
274 | |
282 | #define strvalue(p) ((p)->object.string.svalue) |
275 | #define strvalue(p) (CELL(p)->object.string.svalue) |
283 | #define strlength(p) ((p)->object.string.length) |
276 | #define strlength(p) (CELL(p)->object.string.length) |
284 | |
277 | |
285 | INTERFACE int |
278 | INTERFACE int |
286 | is_vector (pointer p) |
279 | is_vector (pointer p) |
287 | { |
280 | { |
288 | return type (p) == T_VECTOR; |
281 | return type (p) == T_VECTOR; |
289 | } |
282 | } |
290 | |
283 | |
291 | #define vecvalue(p) ((p)->object.vector.vvalue) |
284 | #define vecvalue(p) (CELL(p)->object.vector.vvalue) |
292 | #define veclength(p) ((p)->object.vector.length) |
285 | #define veclength(p) (CELL(p)->object.vector.length) |
293 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
286 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
294 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
287 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
295 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
288 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
296 | |
289 | |
297 | INTERFACE int |
290 | INTERFACE int |
… | |
… | |
323 | string_value (pointer p) |
316 | string_value (pointer p) |
324 | { |
317 | { |
325 | return strvalue (p); |
318 | return strvalue (p); |
326 | } |
319 | } |
327 | |
320 | |
328 | #define ivalue_unchecked(p) (p)->object.ivalue |
321 | #define ivalue_unchecked(p) CELL(p)->object.ivalue |
329 | #define set_ivalue(p,v) (p)->object.ivalue = (v) |
322 | #define set_ivalue(p,v) CELL(p)->object.ivalue = (v) |
330 | |
323 | |
331 | #if USE_REAL |
324 | #if USE_REAL |
332 | #define rvalue_unchecked(p) (p)->object.rvalue |
325 | #define rvalue_unchecked(p) CELL(p)->object.rvalue |
333 | #define set_rvalue(p,v) (p)->object.rvalue = (v) |
326 | #define set_rvalue(p,v) CELL(p)->object.rvalue = (v) |
334 | #else |
327 | #else |
335 | #define rvalue_unchecked(p) (p)->object.ivalue |
328 | #define rvalue_unchecked(p) CELL(p)->object.ivalue |
336 | #define set_rvalue(p,v) (p)->object.ivalue = (v) |
329 | #define set_rvalue(p,v) CELL(p)->object.ivalue = (v) |
337 | #endif |
330 | #endif |
338 | |
331 | |
339 | INTERFACE long |
332 | INTERFACE long |
340 | charvalue (pointer p) |
333 | charvalue (pointer p) |
341 | { |
334 | { |
342 | return ivalue_unchecked (p); |
335 | return ivalue_unchecked (p); |
343 | } |
336 | } |
344 | |
337 | |
|
|
338 | #define port(p) CELL(p)->object.port |
|
|
339 | #define set_port(p,v) port(p) = (v) |
345 | INTERFACE int |
340 | INTERFACE int |
346 | is_port (pointer p) |
341 | is_port (pointer p) |
347 | { |
342 | { |
348 | return type (p) == T_PORT; |
343 | return type (p) == T_PORT; |
349 | } |
344 | } |
350 | |
345 | |
351 | INTERFACE int |
346 | INTERFACE int |
352 | is_inport (pointer p) |
347 | is_inport (pointer p) |
353 | { |
348 | { |
354 | return is_port (p) && p->object.port->kind & port_input; |
349 | return is_port (p) && port (p)->kind & port_input; |
355 | } |
350 | } |
356 | |
351 | |
357 | INTERFACE int |
352 | INTERFACE int |
358 | is_outport (pointer p) |
353 | is_outport (pointer p) |
359 | { |
354 | { |
360 | return is_port (p) && p->object.port->kind & port_output; |
355 | return is_port (p) && port (p)->kind & port_output; |
361 | } |
356 | } |
362 | |
357 | |
363 | INTERFACE int |
358 | INTERFACE int |
364 | is_pair (pointer p) |
359 | is_pair (pointer p) |
365 | { |
360 | { |
366 | return type (p) == T_PAIR; |
361 | return type (p) == T_PAIR; |
367 | } |
362 | } |
368 | |
363 | |
369 | #define car(p) ((p)->object.cons.car + 0) |
364 | #define car(p) (POINTER (CELL(p)->object.cons.car)) |
370 | #define cdr(p) ((p)->object.cons.cdr + 0) |
365 | #define cdr(p) (POINTER (CELL(p)->object.cons.cdr)) |
371 | |
366 | |
372 | static pointer caar (pointer p) { return car (car (p)); } |
367 | static pointer caar (pointer p) { return car (car (p)); } |
373 | static pointer cadr (pointer p) { return car (cdr (p)); } |
368 | static pointer cadr (pointer p) { return car (cdr (p)); } |
374 | static pointer cdar (pointer p) { return cdr (car (p)); } |
369 | static pointer cdar (pointer p) { return cdr (car (p)); } |
375 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
370 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
… | |
… | |
379 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
374 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
380 | |
375 | |
381 | INTERFACE void |
376 | INTERFACE void |
382 | set_car (pointer p, pointer q) |
377 | set_car (pointer p, pointer q) |
383 | { |
378 | { |
384 | p->object.cons.car = q; |
379 | CELL(p)->object.cons.car = CELL (q); |
385 | } |
380 | } |
386 | |
381 | |
387 | INTERFACE void |
382 | INTERFACE void |
388 | set_cdr (pointer p, pointer q) |
383 | set_cdr (pointer p, pointer q) |
389 | { |
384 | { |
390 | p->object.cons.cdr = q; |
385 | CELL(p)->object.cons.cdr = CELL (q); |
391 | } |
386 | } |
392 | |
387 | |
393 | INTERFACE pointer |
388 | INTERFACE pointer |
394 | pair_car (pointer p) |
389 | pair_car (pointer p) |
395 | { |
390 | { |
… | |
… | |
413 | { |
408 | { |
414 | return strvalue (p); |
409 | return strvalue (p); |
415 | } |
410 | } |
416 | |
411 | |
417 | #if USE_PLIST |
412 | #if USE_PLIST |
|
|
413 | #error plists are broken because symbols are no longer pairs |
418 | #define symprop(p) cdr(p) |
414 | #define symprop(p) cdr(p) |
419 | SCHEME_EXPORT int |
415 | SCHEME_EXPORT int |
420 | hasprop (pointer p) |
416 | hasprop (pointer p) |
421 | { |
417 | { |
422 | return typeflag (p) & T_SYMBOL; |
418 | return typeflag (p) & T_SYMBOL; |
… | |
… | |
669 | |
665 | |
670 | static int file_push (SCHEME_P_ const char *fname); |
666 | static int file_push (SCHEME_P_ const char *fname); |
671 | static void file_pop (SCHEME_P); |
667 | static void file_pop (SCHEME_P); |
672 | static int file_interactive (SCHEME_P); |
668 | static int file_interactive (SCHEME_P); |
673 | ecb_inline int is_one_of (const char *s, int c); |
669 | ecb_inline int is_one_of (const char *s, int c); |
674 | static int alloc_cellseg (SCHEME_P_ int n); |
670 | static int alloc_cellseg (SCHEME_P); |
675 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
671 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
676 | static void finalize_cell (SCHEME_P_ pointer a); |
672 | static void finalize_cell (SCHEME_P_ pointer a); |
677 | static int count_consecutive_cells (pointer x, int needed); |
673 | static int count_consecutive_cells (pointer x, int needed); |
678 | 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); |
679 | static pointer mk_number (SCHEME_P_ const num n); |
675 | static pointer mk_number (SCHEME_P_ const num n); |
… | |
… | |
873 | } |
869 | } |
874 | |
870 | |
875 | return ret; |
871 | return ret; |
876 | } |
872 | } |
877 | |
873 | |
878 | #if USE_MATH |
|
|
879 | |
|
|
880 | /* Round to nearest. Round to even if midway */ |
|
|
881 | static double |
|
|
882 | round_per_R5RS (double x) |
|
|
883 | { |
|
|
884 | double fl = floor (x); |
|
|
885 | double ce = ceil (x); |
|
|
886 | double dfl = x - fl; |
|
|
887 | double dce = ce - x; |
|
|
888 | |
|
|
889 | if (dfl > dce) |
|
|
890 | return ce; |
|
|
891 | else if (dfl < dce) |
|
|
892 | return fl; |
|
|
893 | else |
|
|
894 | { |
|
|
895 | if (fmod (fl, 2) == 0) /* I imagine this holds */ |
|
|
896 | return fl; |
|
|
897 | else |
|
|
898 | return ce; |
|
|
899 | } |
|
|
900 | } |
|
|
901 | #endif |
|
|
902 | |
|
|
903 | static int |
874 | static int |
904 | is_zero_rvalue (RVALUE x) |
875 | is_zero_rvalue (RVALUE x) |
905 | { |
876 | { |
906 | return x == 0; |
877 | return x == 0; |
907 | #if 0 |
878 | #if 0 |
… | |
… | |
913 | #endif |
884 | #endif |
914 | } |
885 | } |
915 | |
886 | |
916 | /* allocate new cell segment */ |
887 | /* allocate new cell segment */ |
917 | static int |
888 | static int |
918 | alloc_cellseg (SCHEME_P_ int n) |
889 | alloc_cellseg (SCHEME_P) |
919 | { |
890 | { |
920 | pointer newp; |
891 | struct cell *newp; |
921 | pointer last; |
892 | struct cell *last; |
922 | pointer p; |
893 | struct cell *p; |
923 | char *cp; |
894 | char *cp; |
924 | long i; |
895 | long i; |
925 | int k; |
896 | int k; |
926 | |
897 | |
927 | static int segsize = CELL_SEGSIZE >> 1; |
898 | static int segsize = CELL_SEGSIZE >> 1; |
928 | segsize <<= 1; |
899 | segsize <<= 1; |
929 | |
900 | |
930 | for (k = 0; k < n; k++) |
|
|
931 | { |
|
|
932 | if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1) |
|
|
933 | return k; |
|
|
934 | |
|
|
935 | cp = malloc (segsize * sizeof (struct cell)); |
901 | cp = malloc (segsize * sizeof (struct cell)); |
936 | |
902 | |
937 | if (!cp && USE_ERROR_CHECKING) |
903 | if (!cp && USE_ERROR_CHECKING) |
938 | return k; |
904 | return k; |
939 | |
905 | |
940 | i = ++SCHEME_V->last_cell_seg; |
906 | i = ++SCHEME_V->last_cell_seg; |
941 | SCHEME_V->alloc_seg[i] = cp; |
907 | SCHEME_V->alloc_seg[i] = cp; |
942 | |
908 | |
943 | newp = (pointer)cp; |
909 | newp = (struct cell *)cp; |
944 | SCHEME_V->cell_seg[i] = newp; |
910 | SCHEME_V->cell_seg[i] = newp; |
945 | SCHEME_V->cell_segsize[i] = segsize; |
911 | SCHEME_V->cell_segsize[i] = segsize; |
946 | SCHEME_V->fcells += segsize; |
912 | SCHEME_V->fcells += segsize; |
947 | last = newp + segsize - 1; |
913 | last = newp + segsize - 1; |
948 | |
914 | |
949 | for (p = newp; p <= last; p++) |
915 | for (p = newp; p <= last; p++) |
950 | { |
916 | { |
|
|
917 | pointer cp = POINTER (p); |
951 | set_typeflag (p, T_PAIR); |
918 | set_typeflag (cp, T_PAIR); |
952 | set_car (p, NIL); |
919 | set_car (cp, NIL); |
953 | set_cdr (p, p + 1); |
920 | set_cdr (cp, POINTER (p + 1)); |
954 | } |
921 | } |
955 | |
922 | |
956 | set_cdr (last, SCHEME_V->free_cell); |
923 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
957 | SCHEME_V->free_cell = newp; |
924 | SCHEME_V->free_cell = POINTER (newp); |
958 | } |
|
|
959 | |
925 | |
960 | return n; |
926 | return 1; |
961 | } |
927 | } |
962 | |
928 | |
963 | /* get new cell. parameter a, b is marked by gc. */ |
929 | /* get new cell. parameter a, b is marked by gc. */ |
964 | ecb_inline pointer |
930 | ecb_inline pointer |
965 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
931 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
… | |
… | |
969 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
935 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
970 | return S_SINK; |
936 | return S_SINK; |
971 | |
937 | |
972 | if (SCHEME_V->free_cell == NIL) |
938 | if (SCHEME_V->free_cell == NIL) |
973 | { |
939 | { |
974 | const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 2; |
940 | const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1; |
975 | |
941 | |
976 | gc (SCHEME_A_ a, b); |
942 | gc (SCHEME_A_ a, b); |
977 | |
943 | |
978 | 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) |
979 | { |
945 | { |
980 | /* 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 */ |
981 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
947 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
982 | { |
948 | { |
983 | #if USE_ERROR_CHECKING |
949 | #if USE_ERROR_CHECKING |
984 | SCHEME_V->no_memory = 1; |
950 | SCHEME_V->no_memory = 1; |
985 | return S_SINK; |
951 | return S_SINK; |
986 | #endif |
952 | #endif |
… | |
… | |
998 | } |
964 | } |
999 | } |
965 | } |
1000 | |
966 | |
1001 | /* To retain recent allocs before interpreter knows about them - |
967 | /* To retain recent allocs before interpreter knows about them - |
1002 | Tehom */ |
968 | Tehom */ |
1003 | |
|
|
1004 | static void |
969 | static void |
1005 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
970 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
1006 | { |
971 | { |
1007 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
972 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
1008 | |
973 | |
… | |
… | |
1039 | return S_SINK; |
1004 | return S_SINK; |
1040 | |
1005 | |
1041 | /* Record it as a vector so that gc understands it. */ |
1006 | /* Record it as a vector so that gc understands it. */ |
1042 | set_typeflag (v, T_VECTOR | T_ATOM); |
1007 | set_typeflag (v, T_VECTOR | T_ATOM); |
1043 | |
1008 | |
1044 | v->object.vector.vvalue = e; |
1009 | CELL(v)->object.vector.vvalue = e; |
1045 | v->object.vector.length = len; |
1010 | CELL(v)->object.vector.length = len; |
1046 | fill_vector (v, 0, init); |
1011 | fill_vector (v, 0, init); |
1047 | push_recent_alloc (SCHEME_A_ v, NIL); |
1012 | push_recent_alloc (SCHEME_A_ v, NIL); |
1048 | |
1013 | |
1049 | return v; |
1014 | return v; |
1050 | } |
1015 | } |
… | |
… | |
1059 | static void |
1024 | static void |
1060 | check_cell_alloced (pointer p, int expect_alloced) |
1025 | check_cell_alloced (pointer p, int expect_alloced) |
1061 | { |
1026 | { |
1062 | /* 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. */ |
1063 | if (typeflag (p) & !expect_alloced) |
1028 | if (typeflag (p) & !expect_alloced) |
1064 | xwrstr ("Cell is already allocated!\n"); |
1029 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1065 | |
1030 | |
1066 | if (!(typeflag (p)) & expect_alloced) |
1031 | if (!(typeflag (p)) & expect_alloced) |
1067 | xwrstr ("Cell is not allocated!\n"); |
1032 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1068 | } |
1033 | } |
1069 | |
1034 | |
1070 | static void |
1035 | static void |
1071 | check_range_alloced (pointer p, int n, int expect_alloced) |
1036 | check_range_alloced (pointer p, int n, int expect_alloced) |
1072 | { |
1037 | { |
… | |
… | |
1200 | |
1165 | |
1201 | /* returns the new symbol */ |
1166 | /* returns the new symbol */ |
1202 | static pointer |
1167 | static pointer |
1203 | oblist_add_by_name (SCHEME_P_ const char *name) |
1168 | oblist_add_by_name (SCHEME_P_ const char *name) |
1204 | { |
1169 | { |
1205 | pointer x = mk_string (SCHEME_A_ name); |
1170 | pointer x = generate_symbol (SCHEME_A_ name); |
1206 | set_typeflag (x, T_SYMBOL); |
|
|
1207 | setimmutable (x); |
|
|
1208 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1171 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1209 | return x; |
1172 | return x; |
1210 | } |
1173 | } |
1211 | |
1174 | |
1212 | static pointer |
1175 | static pointer |
… | |
… | |
1222 | mk_port (SCHEME_P_ port *p) |
1185 | mk_port (SCHEME_P_ port *p) |
1223 | { |
1186 | { |
1224 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1187 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1225 | |
1188 | |
1226 | set_typeflag (x, T_PORT | T_ATOM); |
1189 | set_typeflag (x, T_PORT | T_ATOM); |
1227 | x->object.port = p; |
1190 | set_port (x, p); |
1228 | |
1191 | |
1229 | return x; |
1192 | return x; |
1230 | } |
1193 | } |
1231 | #endif |
1194 | #endif |
1232 | |
1195 | |
1233 | pointer |
1196 | pointer |
1234 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1197 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1235 | { |
1198 | { |
1236 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1199 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1237 | |
1200 | |
1238 | set_typeflag (x, (T_FOREIGN | T_ATOM)); |
1201 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1239 | x->object.ff = f; |
1202 | CELL(x)->object.ff = f; |
1240 | |
1203 | |
1241 | return x; |
1204 | return x; |
1242 | } |
1205 | } |
1243 | |
1206 | |
1244 | INTERFACE pointer |
1207 | INTERFACE pointer |
1245 | mk_character (SCHEME_P_ int c) |
1208 | mk_character (SCHEME_P_ int c) |
1246 | { |
1209 | { |
1247 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1210 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1248 | |
1211 | |
1249 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1212 | set_typeflag (x, T_CHARACTER | T_ATOM); |
1250 | set_ivalue (x, c & 0xff); |
1213 | set_ivalue (x, c & 0xff); |
1251 | |
1214 | |
1252 | return x; |
1215 | return x; |
1253 | } |
1216 | } |
1254 | |
1217 | |
1255 | /* get number atom (integer) */ |
1218 | /* get number atom (integer) */ |
1256 | INTERFACE pointer |
1219 | INTERFACE pointer |
1257 | mk_integer (SCHEME_P_ long n) |
1220 | mk_integer (SCHEME_P_ long n) |
1258 | { |
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 | { |
1259 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1232 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1260 | |
1233 | |
1261 | 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 */ |
1262 | set_ivalue (x, n); |
1236 | set_ivalue (x, n); |
1263 | |
1237 | |
|
|
1238 | *pp = x; |
|
|
1239 | } |
|
|
1240 | |
1264 | return x; |
1241 | return *pp; |
1265 | } |
1242 | } |
1266 | |
1243 | |
1267 | INTERFACE pointer |
1244 | INTERFACE pointer |
1268 | mk_real (SCHEME_P_ RVALUE n) |
1245 | mk_real (SCHEME_P_ RVALUE n) |
1269 | { |
1246 | { |
1270 | #if USE_REAL |
1247 | #if USE_REAL |
1271 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1248 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1272 | |
1249 | |
1273 | set_typeflag (x, (T_REAL | T_ATOM)); |
1250 | set_typeflag (x, T_REAL | T_ATOM); |
1274 | set_rvalue (x, n); |
1251 | set_rvalue (x, n); |
1275 | |
1252 | |
1276 | return x; |
1253 | return x; |
1277 | #else |
1254 | #else |
1278 | return mk_integer (SCHEME_A_ n); |
1255 | return mk_integer (SCHEME_A_ n); |
… | |
… | |
1621 | |
1598 | |
1622 | /* garbage collection. parameter a, b is marked. */ |
1599 | /* garbage collection. parameter a, b is marked. */ |
1623 | static void |
1600 | static void |
1624 | gc (SCHEME_P_ pointer a, pointer b) |
1601 | gc (SCHEME_P_ pointer a, pointer b) |
1625 | { |
1602 | { |
1626 | pointer p; |
|
|
1627 | int i; |
1603 | int i; |
1628 | |
1604 | |
1629 | if (SCHEME_V->gc_verbose) |
1605 | if (SCHEME_V->gc_verbose) |
1630 | putstr (SCHEME_A_ "gc..."); |
1606 | putstr (SCHEME_A_ "gc..."); |
1631 | |
1607 | |
… | |
… | |
1647 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1623 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1648 | mark (car (S_SINK)); |
1624 | mark (car (S_SINK)); |
1649 | /* Mark any older stuff above nested C calls */ |
1625 | /* Mark any older stuff above nested C calls */ |
1650 | mark (SCHEME_V->c_nest); |
1626 | mark (SCHEME_V->c_nest); |
1651 | |
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 | |
1652 | /* mark variables a, b */ |
1635 | /* mark variables a, b */ |
1653 | mark (a); |
1636 | mark (a); |
1654 | mark (b); |
1637 | mark (b); |
1655 | |
1638 | |
1656 | /* garbage collect */ |
1639 | /* garbage collect */ |
1657 | clrmark (NIL); |
1640 | clrmark (NIL); |
1658 | SCHEME_V->fcells = 0; |
1641 | SCHEME_V->fcells = 0; |
1659 | SCHEME_V->free_cell = NIL; |
1642 | SCHEME_V->free_cell = NIL; |
1660 | |
1643 | |
1661 | if (SCHEME_V->gc_verbose) |
1644 | if (SCHEME_V->gc_verbose) |
1662 | xwrstr ("freeing..."); |
1645 | putstr (SCHEME_A_ "freeing..."); |
1663 | |
1646 | |
1664 | uint32_t total = 0; |
1647 | uint32_t total = 0; |
1665 | |
1648 | |
1666 | /* Here we scan the cells to build the free-list. */ |
1649 | /* Here we scan the cells to build the free-list. */ |
1667 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1650 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1668 | { |
1651 | { |
1669 | pointer end = 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; |
1670 | total += SCHEME_V->cell_segsize [i]; |
1654 | total += SCHEME_V->cell_segsize [i]; |
1671 | |
1655 | |
1672 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
1656 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
1673 | { |
1657 | { |
|
|
1658 | pointer c = POINTER (p); |
|
|
1659 | |
1674 | if (is_mark (p)) |
1660 | if (is_mark (c)) |
1675 | clrmark (p); |
1661 | clrmark (c); |
1676 | else |
1662 | else |
1677 | { |
1663 | { |
1678 | /* reclaim cell */ |
1664 | /* reclaim cell */ |
1679 | if (typeflag (p) != T_PAIR) |
1665 | if (typeflag (c) != T_PAIR) |
1680 | { |
1666 | { |
1681 | finalize_cell (SCHEME_A_ p); |
1667 | finalize_cell (SCHEME_A_ c); |
1682 | set_typeflag (p, T_PAIR); |
1668 | set_typeflag (c, T_PAIR); |
1683 | set_car (p, NIL); |
1669 | set_car (c, NIL); |
1684 | } |
1670 | } |
1685 | |
1671 | |
1686 | ++SCHEME_V->fcells; |
1672 | ++SCHEME_V->fcells; |
1687 | set_cdr (p, SCHEME_V->free_cell); |
1673 | set_cdr (c, SCHEME_V->free_cell); |
1688 | SCHEME_V->free_cell = p; |
1674 | SCHEME_V->free_cell = c; |
1689 | } |
1675 | } |
1690 | } |
1676 | } |
1691 | } |
1677 | } |
1692 | |
1678 | |
1693 | if (SCHEME_V->gc_verbose) |
1679 | if (SCHEME_V->gc_verbose) |
1694 | { |
1680 | { |
1695 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" out of "); xwrnum (total); 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"); |
1696 | } |
1682 | } |
1697 | } |
1683 | } |
1698 | |
1684 | |
1699 | static void |
1685 | static void |
1700 | finalize_cell (SCHEME_P_ pointer a) |
1686 | finalize_cell (SCHEME_P_ pointer a) |
… | |
… | |
1705 | else if (is_vector (a)) |
1691 | else if (is_vector (a)) |
1706 | free (vecvalue (a)); |
1692 | free (vecvalue (a)); |
1707 | #if USE_PORTS |
1693 | #if USE_PORTS |
1708 | else if (is_port (a)) |
1694 | else if (is_port (a)) |
1709 | { |
1695 | { |
1710 | 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) |
1711 | port_close (SCHEME_A_ a, port_input | port_output); |
1697 | port_close (SCHEME_A_ a, port_input | port_output); |
1712 | |
1698 | |
1713 | free (a->object.port); |
1699 | free (port (a)); |
1714 | } |
1700 | } |
1715 | #endif |
1701 | #endif |
1716 | } |
1702 | } |
1717 | |
1703 | |
1718 | /* ========== Routines for Reading ========== */ |
1704 | /* ========== Routines for Reading ========== */ |
… | |
… | |
1734 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1720 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1735 | 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; |
1736 | 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; |
1737 | 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; |
1738 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1724 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1739 | 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); |
1740 | |
1726 | |
1741 | #if SHOW_ERROR_LINE |
1727 | #if SHOW_ERROR_LINE |
1742 | 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; |
1743 | |
1729 | |
1744 | if (fname) |
1730 | if (fname) |
… | |
… | |
1761 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1747 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1762 | #if USE_PORTS |
1748 | #if USE_PORTS |
1763 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1749 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1764 | #endif |
1750 | #endif |
1765 | SCHEME_V->file_i--; |
1751 | SCHEME_V->file_i--; |
1766 | 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); |
1767 | } |
1753 | } |
1768 | } |
1754 | } |
1769 | |
1755 | |
1770 | static int |
1756 | static int |
1771 | file_interactive (SCHEME_P) |
1757 | file_interactive (SCHEME_P) |
1772 | { |
1758 | { |
1773 | #if USE_PORTS |
1759 | #if USE_PORTS |
1774 | return SCHEME_V->file_i == 0 |
1760 | return SCHEME_V->file_i == 0 |
1775 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1761 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1776 | && (SCHEME_V->inport->object.port->kind & port_file); |
1762 | && (port (SCHEME_V->inport)->kind & port_file); |
1777 | #else |
1763 | #else |
1778 | return 0; |
1764 | return 0; |
1779 | #endif |
1765 | #endif |
1780 | } |
1766 | } |
1781 | |
1767 | |
… | |
… | |
1915 | } |
1901 | } |
1916 | |
1902 | |
1917 | static void |
1903 | static void |
1918 | port_close (SCHEME_P_ pointer p, int flag) |
1904 | port_close (SCHEME_P_ pointer p, int flag) |
1919 | { |
1905 | { |
1920 | port *pt = p->object.port; |
1906 | port *pt = port (p); |
1921 | |
1907 | |
1922 | pt->kind &= ~flag; |
1908 | pt->kind &= ~flag; |
1923 | |
1909 | |
1924 | if ((pt->kind & (port_input | port_output)) == 0) |
1910 | if ((pt->kind & (port_input | port_output)) == 0) |
1925 | { |
1911 | { |
… | |
… | |
1946 | /* get new character from input file */ |
1932 | /* get new character from input file */ |
1947 | static int |
1933 | static int |
1948 | inchar (SCHEME_P) |
1934 | inchar (SCHEME_P) |
1949 | { |
1935 | { |
1950 | int c; |
1936 | int c; |
1951 | port *pt; |
1937 | port *pt = port (SCHEME_V->inport); |
1952 | |
|
|
1953 | pt = SCHEME_V->inport->object.port; |
|
|
1954 | |
1938 | |
1955 | if (pt->kind & port_saw_EOF) |
1939 | if (pt->kind & port_saw_EOF) |
1956 | return EOF; |
1940 | return EOF; |
1957 | |
1941 | |
1958 | c = basic_inchar (pt); |
1942 | c = basic_inchar (pt); |
… | |
… | |
2025 | port *pt; |
2009 | port *pt; |
2026 | |
2010 | |
2027 | if (c == EOF) |
2011 | if (c == EOF) |
2028 | return; |
2012 | return; |
2029 | |
2013 | |
2030 | pt = SCHEME_V->inport->object.port; |
2014 | pt = port (SCHEME_V->inport); |
2031 | pt->unget = c; |
2015 | pt->unget = c; |
2032 | #else |
2016 | #else |
2033 | if (c == EOF) |
2017 | if (c == EOF) |
2034 | return; |
2018 | return; |
2035 | |
2019 | |
… | |
… | |
2063 | |
2047 | |
2064 | INTERFACE void |
2048 | INTERFACE void |
2065 | putstr (SCHEME_P_ const char *s) |
2049 | putstr (SCHEME_P_ const char *s) |
2066 | { |
2050 | { |
2067 | #if USE_PORTS |
2051 | #if USE_PORTS |
2068 | port *pt = SCHEME_V->outport->object.port; |
2052 | port *pt = port (SCHEME_V->outport); |
2069 | |
2053 | |
2070 | if (pt->kind & port_file) |
2054 | if (pt->kind & port_file) |
2071 | write (pt->rep.stdio.file, s, strlen (s)); |
2055 | write (pt->rep.stdio.file, s, strlen (s)); |
2072 | else |
2056 | else |
2073 | for (; *s; s++) |
2057 | for (; *s; s++) |
… | |
… | |
2075 | *pt->rep.string.curr++ = *s; |
2059 | *pt->rep.string.curr++ = *s; |
2076 | 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)) |
2077 | *pt->rep.string.curr++ = *s; |
2061 | *pt->rep.string.curr++ = *s; |
2078 | |
2062 | |
2079 | #else |
2063 | #else |
2080 | xwrstr (s); |
2064 | write (pt->rep.stdio.file, s, strlen (s)); |
2081 | #endif |
2065 | #endif |
2082 | } |
2066 | } |
2083 | |
2067 | |
2084 | static void |
2068 | static void |
2085 | putchars (SCHEME_P_ const char *s, int len) |
2069 | putchars (SCHEME_P_ const char *s, int len) |
2086 | { |
2070 | { |
2087 | #if USE_PORTS |
2071 | #if USE_PORTS |
2088 | port *pt = SCHEME_V->outport->object.port; |
2072 | port *pt = port (SCHEME_V->outport); |
2089 | |
2073 | |
2090 | if (pt->kind & port_file) |
2074 | if (pt->kind & port_file) |
2091 | write (pt->rep.stdio.file, s, len); |
2075 | write (pt->rep.stdio.file, s, len); |
2092 | else |
2076 | else |
2093 | { |
2077 | { |
… | |
… | |
2107 | |
2091 | |
2108 | INTERFACE void |
2092 | INTERFACE void |
2109 | putcharacter (SCHEME_P_ int c) |
2093 | putcharacter (SCHEME_P_ int c) |
2110 | { |
2094 | { |
2111 | #if USE_PORTS |
2095 | #if USE_PORTS |
2112 | port *pt = SCHEME_V->outport->object.port; |
2096 | port *pt = port (SCHEME_V->outport); |
2113 | |
2097 | |
2114 | if (pt->kind & port_file) |
2098 | if (pt->kind & port_file) |
2115 | { |
2099 | { |
2116 | char cc = c; |
2100 | char cc = c; |
2117 | write (pt->rep.stdio.file, &cc, 1); |
2101 | write (pt->rep.stdio.file, &cc, 1); |
… | |
… | |
3059 | { |
3043 | { |
3060 | int nframes = (uintptr_t)SCHEME_V->dump; |
3044 | int nframes = (uintptr_t)SCHEME_V->dump; |
3061 | struct dump_stack_frame *next_frame; |
3045 | struct dump_stack_frame *next_frame; |
3062 | |
3046 | |
3063 | /* enough room for the next frame? */ |
3047 | /* enough room for the next frame? */ |
3064 | if (nframes >= SCHEME_V->dump_size) |
3048 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3065 | { |
3049 | { |
3066 | SCHEME_V->dump_size += STACK_GROWTH; |
3050 | SCHEME_V->dump_size += STACK_GROWTH; |
3067 | 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); |
3068 | } |
3052 | } |
3069 | |
3053 | |
… | |
… | |
3325 | s_return (S_T); |
3309 | s_return (S_T); |
3326 | #endif |
3310 | #endif |
3327 | case OP_LOAD: /* load */ |
3311 | case OP_LOAD: /* load */ |
3328 | if (file_interactive (SCHEME_A)) |
3312 | if (file_interactive (SCHEME_A)) |
3329 | { |
3313 | { |
3330 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3314 | putstr (SCHEME_A_ "Loading "); putstr (SCHEME_A_ strvalue (car (args))); putstr (SCHEME_A_ "\n"); |
3331 | //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))); |
3332 | } |
3316 | } |
3333 | |
3317 | |
3334 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3318 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3335 | Error_1 ("unable to open", car (args)); |
3319 | Error_1 ("unable to open", car (args)); |
3336 | else |
3320 | else |
… | |
… | |
3340 | } |
3324 | } |
3341 | |
3325 | |
3342 | case OP_T0LVL: /* top level */ |
3326 | case OP_T0LVL: /* top level */ |
3343 | |
3327 | |
3344 | /* If we reached the end of file, this loop is done. */ |
3328 | /* If we reached the end of file, this loop is done. */ |
3345 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3329 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3346 | { |
3330 | { |
3347 | if (SCHEME_V->file_i == 0) |
3331 | if (SCHEME_V->file_i == 0) |
3348 | { |
3332 | { |
3349 | SCHEME_V->args = NIL; |
3333 | SCHEME_V->args = NIL; |
3350 | s_goto (OP_QUIT); |
3334 | s_goto (OP_QUIT); |
… | |
… | |
3428 | #endif |
3412 | #endif |
3429 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3413 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3430 | { |
3414 | { |
3431 | 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); |
3432 | |
3416 | |
3433 | if (x != NIL) |
3417 | if (x == NIL) |
3434 | s_return (slot_value_in_env (x)); |
|
|
3435 | else |
|
|
3436 | 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)); |
3437 | } |
3421 | } |
3438 | else if (is_pair (SCHEME_V->code)) |
3422 | else if (is_pair (SCHEME_V->code)) |
3439 | { |
3423 | { |
3440 | x = car (SCHEME_V->code); |
3424 | x = car (SCHEME_V->code); |
3441 | |
3425 | |
… | |
… | |
3518 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3502 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3519 | else if (is_foreign (SCHEME_V->code)) |
3503 | else if (is_foreign (SCHEME_V->code)) |
3520 | { |
3504 | { |
3521 | /* Keep nested calls from GC'ing the arglist */ |
3505 | /* Keep nested calls from GC'ing the arglist */ |
3522 | push_recent_alloc (SCHEME_A_ args, NIL); |
3506 | push_recent_alloc (SCHEME_A_ args, NIL); |
3523 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3507 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3524 | |
3508 | |
3525 | s_return (x); |
3509 | s_return (x); |
3526 | } |
3510 | } |
3527 | 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 */ |
3528 | { |
3512 | { |
… | |
… | |
3565 | |
3549 | |
3566 | case OP_DOMACRO: /* do macro */ |
3550 | case OP_DOMACRO: /* do macro */ |
3567 | SCHEME_V->code = SCHEME_V->value; |
3551 | SCHEME_V->code = SCHEME_V->value; |
3568 | s_goto (OP_EVAL); |
3552 | s_goto (OP_EVAL); |
3569 | |
3553 | |
3570 | #if 1 |
|
|
3571 | |
|
|
3572 | case OP_LAMBDA: /* lambda */ |
3554 | case OP_LAMBDA: /* lambda */ |
3573 | /* 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 |
3574 | set SCHEME_V->value fall thru */ |
3556 | set SCHEME_V->value fall thru */ |
3575 | { |
3557 | { |
3576 | 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); |
… | |
… | |
3582 | SCHEME_V->code = slot_value_in_env (f); |
3564 | SCHEME_V->code = slot_value_in_env (f); |
3583 | s_goto (OP_APPLY); |
3565 | s_goto (OP_APPLY); |
3584 | } |
3566 | } |
3585 | |
3567 | |
3586 | SCHEME_V->value = SCHEME_V->code; |
3568 | SCHEME_V->value = SCHEME_V->code; |
3587 | /* Fallthru */ |
|
|
3588 | } |
3569 | } |
|
|
3570 | /* Fallthru */ |
3589 | |
3571 | |
3590 | case OP_LAMBDA1: |
3572 | case OP_LAMBDA1: |
3591 | 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)); |
3592 | |
|
|
3593 | #else |
|
|
3594 | |
|
|
3595 | case OP_LAMBDA: /* lambda */ |
|
|
3596 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
|
|
3597 | |
|
|
3598 | #endif |
|
|
3599 | |
3574 | |
3600 | case OP_MKCLOSURE: /* make-closure */ |
3575 | case OP_MKCLOSURE: /* make-closure */ |
3601 | x = car (args); |
3576 | x = car (args); |
3602 | |
3577 | |
3603 | if (car (x) == SCHEME_V->LAMBDA) |
3578 | if (car (x) == SCHEME_V->LAMBDA) |
… | |
… | |
4088 | s_return (mk_real (SCHEME_A_ result)); |
4063 | s_return (mk_real (SCHEME_A_ result)); |
4089 | else |
4064 | else |
4090 | s_return (mk_integer (SCHEME_A_ result)); |
4065 | s_return (mk_integer (SCHEME_A_ result)); |
4091 | } |
4066 | } |
4092 | |
4067 | |
4093 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
4068 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
4094 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
4069 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
4095 | |
|
|
4096 | case OP_TRUNCATE: |
|
|
4097 | { |
|
|
4098 | RVALUE n = rvalue (x); |
|
|
4099 | s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n))); |
|
|
4100 | } |
|
|
4101 | |
|
|
4102 | case OP_ROUND: |
|
|
4103 | if (is_integer (x)) |
|
|
4104 | s_return (x); |
|
|
4105 | |
|
|
4106 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
4070 | case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x)))); |
|
|
4071 | case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x)))); |
4107 | #endif |
4072 | #endif |
4108 | |
4073 | |
4109 | case OP_ADD: /* + */ |
4074 | case OP_ADD: /* + */ |
4110 | v = num_zero; |
4075 | v = num_zero; |
4111 | |
4076 | |
… | |
… | |
4199 | else |
4164 | else |
4200 | Error_0 ("modulo: division by zero"); |
4165 | Error_0 ("modulo: division by zero"); |
4201 | |
4166 | |
4202 | s_return (mk_number (SCHEME_A_ v)); |
4167 | s_return (mk_number (SCHEME_A_ v)); |
4203 | |
4168 | |
4204 | case OP_CAR: /* car */ |
4169 | /* the compiler will optimize this mess... */ |
4205 | s_return (caar (args)); |
4170 | case OP_CAR: op_car: s_return (car (x)); |
4206 | |
4171 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4207 | case OP_CDR: /* cdr */ |
4172 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4208 | 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; |
4209 | |
4200 | |
4210 | case OP_CONS: /* cons */ |
4201 | case OP_CONS: /* cons */ |
4211 | set_cdr (args, cadr (args)); |
4202 | set_cdr (args, cadr (args)); |
4212 | s_return (args); |
4203 | s_return (args); |
4213 | |
4204 | |
… | |
… | |
4605 | } |
4596 | } |
4606 | else |
4597 | else |
4607 | s_return (SCHEME_V->code); |
4598 | s_return (SCHEME_V->code); |
4608 | |
4599 | |
4609 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4600 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4610 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4601 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4611 | s_return (SCHEME_V->value); |
4602 | s_return (SCHEME_V->value); |
4612 | |
4603 | |
4613 | #if USE_PORTS |
4604 | #if USE_PORTS |
4614 | |
4605 | |
4615 | case OP_WRITE: /* write */ |
4606 | case OP_WRITE: /* write */ |
… | |
… | |
4760 | SCHEME_V->gc_verbose = (a != S_F); |
4751 | SCHEME_V->gc_verbose = (a != S_F); |
4761 | s_retbool (was); |
4752 | s_retbool (was); |
4762 | } |
4753 | } |
4763 | |
4754 | |
4764 | case OP_NEWSEGMENT: /* new-segment */ |
4755 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
4756 | #if 0 |
4765 | if (!is_pair (args) || !is_number (a)) |
4757 | if (!is_pair (args) || !is_number (a)) |
4766 | Error_0 ("new-segment: argument must be a number"); |
4758 | Error_0 ("new-segment: argument must be a number"); |
4767 | |
4759 | #endif |
4768 | alloc_cellseg (SCHEME_A_ ivalue (a)); |
4760 | s_retbool (alloc_cellseg (SCHEME_A)); |
4769 | |
|
|
4770 | s_return (S_T); |
|
|
4771 | |
4761 | |
4772 | case OP_OBLIST: /* oblist */ |
4762 | case OP_OBLIST: /* oblist */ |
4773 | s_return (oblist_all_symbols (SCHEME_A)); |
4763 | s_return (oblist_all_symbols (SCHEME_A)); |
4774 | |
4764 | |
4775 | #if USE_PORTS |
4765 | #if USE_PORTS |
… | |
… | |
4845 | s_return (p == NIL ? S_F : p); |
4835 | s_return (p == NIL ? S_F : p); |
4846 | } |
4836 | } |
4847 | |
4837 | |
4848 | case OP_GET_OUTSTRING: /* get-output-string */ |
4838 | case OP_GET_OUTSTRING: /* get-output-string */ |
4849 | { |
4839 | { |
4850 | port *p; |
4840 | port *p = port (a); |
4851 | |
4841 | |
4852 | if ((p = a->object.port)->kind & port_string) |
4842 | if (p->kind & port_string) |
4853 | { |
4843 | { |
4854 | off_t size; |
4844 | off_t size; |
4855 | char *str; |
4845 | char *str; |
4856 | |
4846 | |
4857 | size = p->rep.string.curr - p->rep.string.start + 1; |
4847 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4962 | int res; |
4952 | int res; |
4963 | |
4953 | |
4964 | if (is_pair (args)) |
4954 | if (is_pair (args)) |
4965 | p = car (args); |
4955 | p = car (args); |
4966 | |
4956 | |
4967 | res = p->object.port->kind & port_string; |
4957 | res = port (p)->kind & port_string; |
4968 | |
4958 | |
4969 | s_retbool (res); |
4959 | s_retbool (res); |
4970 | } |
4960 | } |
4971 | |
4961 | |
4972 | case OP_SET_INPORT: /* set-input-port */ |
4962 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
5512 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5502 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5513 | return; |
5503 | return; |
5514 | |
5504 | |
5515 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5505 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5516 | { |
5506 | { |
5517 | xwrstr ("No memory!\n"); |
5507 | putstr (SCHEME_A_ "No memory!\n"); |
5518 | return; |
5508 | return; |
5519 | } |
5509 | } |
5520 | } |
5510 | } |
5521 | } |
5511 | } |
5522 | |
5512 | |
… | |
… | |
5635 | ecb_cold int |
5625 | ecb_cold int |
5636 | scheme_init (SCHEME_P) |
5626 | scheme_init (SCHEME_P) |
5637 | { |
5627 | { |
5638 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5628 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5639 | pointer x; |
5629 | pointer x; |
|
|
5630 | |
|
|
5631 | /* this memset is not strictly correct, as we assume (intcache) |
|
|
5632 | * that memset 0 will also set pointers to 0, but memset does |
|
|
5633 | * of course not guarantee that. screw such systems. |
|
|
5634 | */ |
|
|
5635 | memset (SCHEME_V, 0, sizeof (*SCHEME_V)); |
5640 | |
5636 | |
5641 | num_set_fixnum (num_zero, 1); |
5637 | num_set_fixnum (num_zero, 1); |
5642 | num_set_ivalue (num_zero, 0); |
5638 | num_set_ivalue (num_zero, 0); |
5643 | num_set_fixnum (num_one, 1); |
5639 | num_set_fixnum (num_one, 1); |
5644 | num_set_ivalue (num_one, 1); |
5640 | num_set_ivalue (num_one, 1); |
… | |
… | |
5656 | SCHEME_V->save_inport = NIL; |
5652 | SCHEME_V->save_inport = NIL; |
5657 | SCHEME_V->loadport = NIL; |
5653 | SCHEME_V->loadport = NIL; |
5658 | SCHEME_V->nesting = 0; |
5654 | SCHEME_V->nesting = 0; |
5659 | SCHEME_V->interactive_repl = 0; |
5655 | SCHEME_V->interactive_repl = 0; |
5660 | |
5656 | |
5661 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5657 | if (!alloc_cellseg (SCHEME_A)) |
5662 | { |
5658 | { |
5663 | #if USE_ERROR_CHECKING |
5659 | #if USE_ERROR_CHECKING |
5664 | SCHEME_V->no_memory = 1; |
5660 | SCHEME_V->no_memory = 1; |
5665 | return 0; |
5661 | return 0; |
5666 | #endif |
5662 | #endif |
… | |
… | |
6000 | int isfile = 1; |
5996 | int isfile = 1; |
6001 | system ("ps v $PPID");//D |
5997 | system ("ps v $PPID");//D |
6002 | |
5998 | |
6003 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5999 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6004 | { |
6000 | { |
6005 | xwrstr ("Usage: tinyscheme -?\n"); |
6001 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
6006 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6002 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
6007 | xwrstr ("followed by\n"); |
6003 | putstr (SCHEME_A_ "followed by\n"); |
6008 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6004 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
6009 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6005 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6010 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6006 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
6011 | xwrstr ("Use - as filename for stdin.\n"); |
6007 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
6012 | return 1; |
6008 | return 1; |
6013 | } |
6009 | } |
6014 | |
6010 | |
6015 | if (!scheme_init (SCHEME_A)) |
6011 | if (!scheme_init (SCHEME_A)) |
6016 | { |
6012 | { |
6017 | xwrstr ("Could not initialize!\n"); |
6013 | putstr (SCHEME_A_ "Could not initialize!\n"); |
6018 | return 2; |
6014 | return 2; |
6019 | } |
6015 | } |
6020 | |
6016 | |
6021 | # if USE_PORTS |
6017 | # if USE_PORTS |
6022 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6018 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
6067 | fin = open (file_name, O_RDONLY); |
6063 | fin = open (file_name, O_RDONLY); |
6068 | #endif |
6064 | #endif |
6069 | |
6065 | |
6070 | if (isfile && fin < 0) |
6066 | if (isfile && fin < 0) |
6071 | { |
6067 | { |
6072 | xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); |
6068 | putstr (SCHEME_A_ "Could not open file "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); |
6073 | } |
6069 | } |
6074 | else |
6070 | else |
6075 | { |
6071 | { |
6076 | if (isfile) |
6072 | if (isfile) |
6077 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6073 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
… | |
… | |
6081 | #if USE_PORTS |
6077 | #if USE_PORTS |
6082 | if (!isfile || fin != STDIN_FILENO) |
6078 | if (!isfile || fin != STDIN_FILENO) |
6083 | { |
6079 | { |
6084 | if (SCHEME_V->retcode != 0) |
6080 | if (SCHEME_V->retcode != 0) |
6085 | { |
6081 | { |
6086 | xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); |
6082 | putstr (SCHEME_A_ "Errors encountered reading "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); |
6087 | } |
6083 | } |
6088 | |
6084 | |
6089 | if (isfile) |
6085 | if (isfile) |
6090 | close (fin); |
6086 | close (fin); |
6091 | } |
6087 | } |