… | |
… | |
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 | |
… | |
… | |
192 | # define prompt "ts> " |
194 | # define prompt "ts> " |
193 | #endif |
195 | #endif |
194 | |
196 | |
195 | #ifndef InitFile |
197 | #ifndef InitFile |
196 | # define InitFile "init.scm" |
198 | # define InitFile "init.scm" |
197 | #endif |
|
|
198 | |
|
|
199 | #ifndef FIRST_CELLSEGS |
|
|
200 | # define FIRST_CELLSEGS 3 |
|
|
201 | #endif |
199 | #endif |
202 | |
200 | |
203 | enum scheme_types |
201 | enum scheme_types |
204 | { |
202 | { |
205 | T_INTEGER, |
203 | T_INTEGER, |
… | |
… | |
266 | static int is_zero_rvalue (RVALUE x); |
264 | static int is_zero_rvalue (RVALUE x); |
267 | |
265 | |
268 | static num num_zero; |
266 | static num num_zero; |
269 | static num num_one; |
267 | static num num_one; |
270 | |
268 | |
|
|
269 | /* convert "pointer" to cell* / cell* to pointer */ |
|
|
270 | #define CELL(p) ((struct cell *)(p) + 0) |
|
|
271 | #define POINTER(c) ((void *)((c) - 0)) |
|
|
272 | |
271 | /* macros for cell operations */ |
273 | /* macros for cell operations */ |
272 | #define typeflag(p) ((p)->flag + 0) |
274 | #define typeflag(p) (CELL(p)->flag + 0) |
273 | #define set_typeflag(p,v) ((p)->flag = (v)) |
275 | #define set_typeflag(p,v) (CELL(p)->flag = (v)) |
274 | #define type(p) (typeflag (p) & T_MASKTYPE) |
276 | #define type(p) (typeflag (p) & T_MASKTYPE) |
275 | |
277 | |
276 | INTERFACE int |
278 | INTERFACE int |
277 | is_string (pointer p) |
279 | is_string (pointer p) |
278 | { |
280 | { |
279 | return type (p) == T_STRING; |
281 | return type (p) == T_STRING; |
280 | } |
282 | } |
281 | |
283 | |
282 | #define strvalue(p) ((p)->object.string.svalue) |
284 | #define strvalue(p) (CELL(p)->object.string.svalue) |
283 | #define strlength(p) ((p)->object.string.length) |
285 | #define strlength(p) (CELL(p)->object.string.length) |
284 | |
286 | |
285 | INTERFACE int |
287 | INTERFACE int |
286 | is_vector (pointer p) |
288 | is_vector (pointer p) |
287 | { |
289 | { |
288 | return type (p) == T_VECTOR; |
290 | return type (p) == T_VECTOR; |
289 | } |
291 | } |
290 | |
292 | |
291 | #define vecvalue(p) ((p)->object.vector.vvalue) |
293 | #define vecvalue(p) (CELL(p)->object.vector.vvalue) |
292 | #define veclength(p) ((p)->object.vector.length) |
294 | #define veclength(p) (CELL(p)->object.vector.length) |
293 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
295 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
294 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
296 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
295 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
297 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
296 | |
298 | |
297 | INTERFACE int |
299 | INTERFACE int |
… | |
… | |
323 | string_value (pointer p) |
325 | string_value (pointer p) |
324 | { |
326 | { |
325 | return strvalue (p); |
327 | return strvalue (p); |
326 | } |
328 | } |
327 | |
329 | |
328 | #define ivalue_unchecked(p) (p)->object.ivalue |
330 | #define ivalue_unchecked(p) CELL(p)->object.ivalue |
329 | #define set_ivalue(p,v) (p)->object.ivalue = (v) |
331 | #define set_ivalue(p,v) CELL(p)->object.ivalue = (v) |
330 | |
332 | |
331 | #if USE_REAL |
333 | #if USE_REAL |
332 | #define rvalue_unchecked(p) (p)->object.rvalue |
334 | #define rvalue_unchecked(p) CELL(p)->object.rvalue |
333 | #define set_rvalue(p,v) (p)->object.rvalue = (v) |
335 | #define set_rvalue(p,v) CELL(p)->object.rvalue = (v) |
334 | #else |
336 | #else |
335 | #define rvalue_unchecked(p) (p)->object.ivalue |
337 | #define rvalue_unchecked(p) CELL(p)->object.ivalue |
336 | #define set_rvalue(p,v) (p)->object.ivalue = (v) |
338 | #define set_rvalue(p,v) CELL(p)->object.ivalue = (v) |
337 | #endif |
339 | #endif |
338 | |
340 | |
339 | INTERFACE long |
341 | INTERFACE long |
340 | charvalue (pointer p) |
342 | charvalue (pointer p) |
341 | { |
343 | { |
342 | return ivalue_unchecked (p); |
344 | return ivalue_unchecked (p); |
343 | } |
345 | } |
344 | |
346 | |
|
|
347 | #define port(p) CELL(p)->object.port |
|
|
348 | #define set_port(p,v) port(p) = (v) |
345 | INTERFACE int |
349 | INTERFACE int |
346 | is_port (pointer p) |
350 | is_port (pointer p) |
347 | { |
351 | { |
348 | return type (p) == T_PORT; |
352 | return type (p) == T_PORT; |
349 | } |
353 | } |
350 | |
354 | |
351 | INTERFACE int |
355 | INTERFACE int |
352 | is_inport (pointer p) |
356 | is_inport (pointer p) |
353 | { |
357 | { |
354 | return is_port (p) && p->object.port->kind & port_input; |
358 | return is_port (p) && port (p)->kind & port_input; |
355 | } |
359 | } |
356 | |
360 | |
357 | INTERFACE int |
361 | INTERFACE int |
358 | is_outport (pointer p) |
362 | is_outport (pointer p) |
359 | { |
363 | { |
360 | return is_port (p) && p->object.port->kind & port_output; |
364 | return is_port (p) && port (p)->kind & port_output; |
361 | } |
365 | } |
362 | |
366 | |
363 | INTERFACE int |
367 | INTERFACE int |
364 | is_pair (pointer p) |
368 | is_pair (pointer p) |
365 | { |
369 | { |
366 | return type (p) == T_PAIR; |
370 | return type (p) == T_PAIR; |
367 | } |
371 | } |
368 | |
372 | |
369 | #define car(p) ((p)->object.cons.car + 0) |
373 | #define car(p) (POINTER (CELL(p)->object.cons.car)) |
370 | #define cdr(p) ((p)->object.cons.cdr + 0) |
374 | #define cdr(p) (POINTER (CELL(p)->object.cons.cdr)) |
371 | |
375 | |
372 | static pointer caar (pointer p) { return car (car (p)); } |
376 | static pointer caar (pointer p) { return car (car (p)); } |
373 | static pointer cadr (pointer p) { return car (cdr (p)); } |
377 | static pointer cadr (pointer p) { return car (cdr (p)); } |
374 | static pointer cdar (pointer p) { return cdr (car (p)); } |
378 | static pointer cdar (pointer p) { return cdr (car (p)); } |
375 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
379 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
… | |
… | |
379 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
383 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
380 | |
384 | |
381 | INTERFACE void |
385 | INTERFACE void |
382 | set_car (pointer p, pointer q) |
386 | set_car (pointer p, pointer q) |
383 | { |
387 | { |
384 | p->object.cons.car = q; |
388 | CELL(p)->object.cons.car = CELL (q); |
385 | } |
389 | } |
386 | |
390 | |
387 | INTERFACE void |
391 | INTERFACE void |
388 | set_cdr (pointer p, pointer q) |
392 | set_cdr (pointer p, pointer q) |
389 | { |
393 | { |
390 | p->object.cons.cdr = q; |
394 | CELL(p)->object.cons.cdr = CELL (q); |
391 | } |
395 | } |
392 | |
396 | |
393 | INTERFACE pointer |
397 | INTERFACE pointer |
394 | pair_car (pointer p) |
398 | pair_car (pointer p) |
395 | { |
399 | { |
… | |
… | |
670 | |
674 | |
671 | static int file_push (SCHEME_P_ const char *fname); |
675 | static int file_push (SCHEME_P_ const char *fname); |
672 | static void file_pop (SCHEME_P); |
676 | static void file_pop (SCHEME_P); |
673 | static int file_interactive (SCHEME_P); |
677 | static int file_interactive (SCHEME_P); |
674 | ecb_inline int is_one_of (const char *s, int c); |
678 | ecb_inline int is_one_of (const char *s, int c); |
675 | static int alloc_cellseg (SCHEME_P_ int n); |
679 | static int alloc_cellseg (SCHEME_P); |
676 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
680 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
677 | static void finalize_cell (SCHEME_P_ pointer a); |
681 | static void finalize_cell (SCHEME_P_ pointer a); |
678 | static int count_consecutive_cells (pointer x, int needed); |
682 | static int count_consecutive_cells (pointer x, int needed); |
679 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
683 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
680 | static pointer mk_number (SCHEME_P_ const num n); |
684 | static pointer mk_number (SCHEME_P_ const num n); |
… | |
… | |
914 | #endif |
918 | #endif |
915 | } |
919 | } |
916 | |
920 | |
917 | /* allocate new cell segment */ |
921 | /* allocate new cell segment */ |
918 | static int |
922 | static int |
919 | alloc_cellseg (SCHEME_P_ int n) |
923 | alloc_cellseg (SCHEME_P) |
920 | { |
924 | { |
921 | pointer newp; |
925 | struct cell *newp; |
922 | pointer last; |
926 | struct cell *last; |
923 | pointer p; |
927 | struct cell *p; |
924 | char *cp; |
928 | char *cp; |
925 | long i; |
929 | long i; |
926 | int k; |
930 | int k; |
927 | |
931 | |
928 | static int segsize = CELL_SEGSIZE >> 1; |
932 | static int segsize = CELL_SEGSIZE >> 1; |
929 | segsize <<= 1; |
933 | segsize <<= 1; |
930 | |
934 | |
931 | for (k = 0; k < n; k++) |
|
|
932 | { |
|
|
933 | if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1) |
|
|
934 | return k; |
|
|
935 | |
|
|
936 | cp = malloc (segsize * sizeof (struct cell)); |
935 | cp = malloc (segsize * sizeof (struct cell)); |
937 | |
936 | |
938 | if (!cp && USE_ERROR_CHECKING) |
937 | if (!cp && USE_ERROR_CHECKING) |
939 | return k; |
938 | return k; |
940 | |
939 | |
941 | i = ++SCHEME_V->last_cell_seg; |
940 | i = ++SCHEME_V->last_cell_seg; |
942 | SCHEME_V->alloc_seg[i] = cp; |
941 | SCHEME_V->alloc_seg[i] = cp; |
943 | |
942 | |
944 | newp = (pointer)cp; |
943 | newp = (struct cell *)cp; |
945 | SCHEME_V->cell_seg[i] = newp; |
944 | SCHEME_V->cell_seg[i] = newp; |
946 | SCHEME_V->cell_segsize[i] = segsize; |
945 | SCHEME_V->cell_segsize[i] = segsize; |
947 | SCHEME_V->fcells += segsize; |
946 | SCHEME_V->fcells += segsize; |
948 | last = newp + segsize - 1; |
947 | last = newp + segsize - 1; |
949 | |
948 | |
950 | for (p = newp; p <= last; p++) |
949 | for (p = newp; p <= last; p++) |
951 | { |
950 | { |
|
|
951 | pointer cp = POINTER (p); |
952 | set_typeflag (p, T_PAIR); |
952 | set_typeflag (cp, T_PAIR); |
953 | set_car (p, NIL); |
953 | set_car (cp, NIL); |
954 | set_cdr (p, p + 1); |
954 | set_cdr (cp, POINTER (p + 1)); |
955 | } |
955 | } |
956 | |
956 | |
957 | set_cdr (last, SCHEME_V->free_cell); |
957 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
958 | SCHEME_V->free_cell = newp; |
958 | SCHEME_V->free_cell = POINTER (newp); |
959 | } |
|
|
960 | |
959 | |
961 | return n; |
960 | return 1; |
962 | } |
961 | } |
963 | |
962 | |
964 | /* get new cell. parameter a, b is marked by gc. */ |
963 | /* get new cell. parameter a, b is marked by gc. */ |
965 | ecb_inline pointer |
964 | ecb_inline pointer |
966 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
965 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
… | |
… | |
977 | gc (SCHEME_A_ a, b); |
976 | gc (SCHEME_A_ a, b); |
978 | |
977 | |
979 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
978 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
980 | { |
979 | { |
981 | /* if only a few recovered, get more to avoid fruitless gc's */ |
980 | /* if only a few recovered, get more to avoid fruitless gc's */ |
982 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
981 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
983 | { |
982 | { |
984 | #if USE_ERROR_CHECKING |
983 | #if USE_ERROR_CHECKING |
985 | SCHEME_V->no_memory = 1; |
984 | SCHEME_V->no_memory = 1; |
986 | return S_SINK; |
985 | return S_SINK; |
987 | #endif |
986 | #endif |
… | |
… | |
999 | } |
998 | } |
1000 | } |
999 | } |
1001 | |
1000 | |
1002 | /* To retain recent allocs before interpreter knows about them - |
1001 | /* To retain recent allocs before interpreter knows about them - |
1003 | Tehom */ |
1002 | Tehom */ |
1004 | |
|
|
1005 | static void |
1003 | static void |
1006 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
1004 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
1007 | { |
1005 | { |
1008 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
1006 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
1009 | |
1007 | |
… | |
… | |
1040 | return S_SINK; |
1038 | return S_SINK; |
1041 | |
1039 | |
1042 | /* Record it as a vector so that gc understands it. */ |
1040 | /* Record it as a vector so that gc understands it. */ |
1043 | set_typeflag (v, T_VECTOR | T_ATOM); |
1041 | set_typeflag (v, T_VECTOR | T_ATOM); |
1044 | |
1042 | |
1045 | v->object.vector.vvalue = e; |
1043 | CELL(v)->object.vector.vvalue = e; |
1046 | v->object.vector.length = len; |
1044 | CELL(v)->object.vector.length = len; |
1047 | fill_vector (v, 0, init); |
1045 | fill_vector (v, 0, init); |
1048 | push_recent_alloc (SCHEME_A_ v, NIL); |
1046 | push_recent_alloc (SCHEME_A_ v, NIL); |
1049 | |
1047 | |
1050 | return v; |
1048 | return v; |
1051 | } |
1049 | } |
… | |
… | |
1221 | mk_port (SCHEME_P_ port *p) |
1219 | mk_port (SCHEME_P_ port *p) |
1222 | { |
1220 | { |
1223 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1221 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1224 | |
1222 | |
1225 | set_typeflag (x, T_PORT | T_ATOM); |
1223 | set_typeflag (x, T_PORT | T_ATOM); |
1226 | x->object.port = p; |
1224 | set_port (x, p); |
1227 | |
1225 | |
1228 | return x; |
1226 | return x; |
1229 | } |
1227 | } |
1230 | #endif |
1228 | #endif |
1231 | |
1229 | |
… | |
… | |
1233 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1231 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1234 | { |
1232 | { |
1235 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1233 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1236 | |
1234 | |
1237 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1235 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1238 | x->object.ff = f; |
1236 | CELL(x)->object.ff = f; |
1239 | |
1237 | |
1240 | return x; |
1238 | return x; |
1241 | } |
1239 | } |
1242 | |
1240 | |
1243 | INTERFACE pointer |
1241 | INTERFACE pointer |
… | |
… | |
1634 | |
1632 | |
1635 | /* garbage collection. parameter a, b is marked. */ |
1633 | /* garbage collection. parameter a, b is marked. */ |
1636 | static void |
1634 | static void |
1637 | gc (SCHEME_P_ pointer a, pointer b) |
1635 | gc (SCHEME_P_ pointer a, pointer b) |
1638 | { |
1636 | { |
1639 | pointer p; |
|
|
1640 | int i; |
1637 | int i; |
1641 | |
1638 | |
1642 | if (SCHEME_V->gc_verbose) |
1639 | if (SCHEME_V->gc_verbose) |
1643 | putstr (SCHEME_A_ "gc..."); |
1640 | putstr (SCHEME_A_ "gc..."); |
1644 | |
1641 | |
… | |
… | |
1684 | uint32_t total = 0; |
1681 | uint32_t total = 0; |
1685 | |
1682 | |
1686 | /* Here we scan the cells to build the free-list. */ |
1683 | /* Here we scan the cells to build the free-list. */ |
1687 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1684 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1688 | { |
1685 | { |
1689 | pointer end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
1686 | struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1687 | struct cell *p; |
1690 | total += SCHEME_V->cell_segsize [i]; |
1688 | total += SCHEME_V->cell_segsize [i]; |
1691 | |
1689 | |
1692 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
1690 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
1693 | { |
1691 | { |
|
|
1692 | pointer c = POINTER (p); |
|
|
1693 | |
1694 | if (is_mark (p)) |
1694 | if (is_mark (c)) |
1695 | clrmark (p); |
1695 | clrmark (c); |
1696 | else |
1696 | else |
1697 | { |
1697 | { |
1698 | /* reclaim cell */ |
1698 | /* reclaim cell */ |
1699 | if (typeflag (p) != T_PAIR) |
1699 | if (typeflag (c) != T_PAIR) |
1700 | { |
1700 | { |
1701 | finalize_cell (SCHEME_A_ p); |
1701 | finalize_cell (SCHEME_A_ c); |
1702 | set_typeflag (p, T_PAIR); |
1702 | set_typeflag (c, T_PAIR); |
1703 | set_car (p, NIL); |
1703 | set_car (c, NIL); |
1704 | } |
1704 | } |
1705 | |
1705 | |
1706 | ++SCHEME_V->fcells; |
1706 | ++SCHEME_V->fcells; |
1707 | set_cdr (p, SCHEME_V->free_cell); |
1707 | set_cdr (c, SCHEME_V->free_cell); |
1708 | SCHEME_V->free_cell = p; |
1708 | SCHEME_V->free_cell = c; |
1709 | } |
1709 | } |
1710 | } |
1710 | } |
1711 | } |
1711 | } |
1712 | |
1712 | |
1713 | if (SCHEME_V->gc_verbose) |
1713 | if (SCHEME_V->gc_verbose) |
… | |
… | |
1725 | else if (is_vector (a)) |
1725 | else if (is_vector (a)) |
1726 | free (vecvalue (a)); |
1726 | free (vecvalue (a)); |
1727 | #if USE_PORTS |
1727 | #if USE_PORTS |
1728 | else if (is_port (a)) |
1728 | else if (is_port (a)) |
1729 | { |
1729 | { |
1730 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
1730 | if (port(a)->kind & port_file && port (a)->rep.stdio.closeit) |
1731 | port_close (SCHEME_A_ a, port_input | port_output); |
1731 | port_close (SCHEME_A_ a, port_input | port_output); |
1732 | |
1732 | |
1733 | free (a->object.port); |
1733 | free (port (a)); |
1734 | } |
1734 | } |
1735 | #endif |
1735 | #endif |
1736 | } |
1736 | } |
1737 | |
1737 | |
1738 | /* ========== Routines for Reading ========== */ |
1738 | /* ========== Routines for Reading ========== */ |
… | |
… | |
1754 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1754 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1755 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1755 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1756 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; |
1756 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; |
1757 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1757 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1758 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1758 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1759 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1759 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1760 | |
1760 | |
1761 | #if SHOW_ERROR_LINE |
1761 | #if SHOW_ERROR_LINE |
1762 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1762 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1763 | |
1763 | |
1764 | if (fname) |
1764 | if (fname) |
… | |
… | |
1781 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1781 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1782 | #if USE_PORTS |
1782 | #if USE_PORTS |
1783 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1783 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1784 | #endif |
1784 | #endif |
1785 | SCHEME_V->file_i--; |
1785 | SCHEME_V->file_i--; |
1786 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1786 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1787 | } |
1787 | } |
1788 | } |
1788 | } |
1789 | |
1789 | |
1790 | static int |
1790 | static int |
1791 | file_interactive (SCHEME_P) |
1791 | file_interactive (SCHEME_P) |
1792 | { |
1792 | { |
1793 | #if USE_PORTS |
1793 | #if USE_PORTS |
1794 | return SCHEME_V->file_i == 0 |
1794 | return SCHEME_V->file_i == 0 |
1795 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1795 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1796 | && (SCHEME_V->inport->object.port->kind & port_file); |
1796 | && (port (SCHEME_V->inport)->kind & port_file); |
1797 | #else |
1797 | #else |
1798 | return 0; |
1798 | return 0; |
1799 | #endif |
1799 | #endif |
1800 | } |
1800 | } |
1801 | |
1801 | |
… | |
… | |
1935 | } |
1935 | } |
1936 | |
1936 | |
1937 | static void |
1937 | static void |
1938 | port_close (SCHEME_P_ pointer p, int flag) |
1938 | port_close (SCHEME_P_ pointer p, int flag) |
1939 | { |
1939 | { |
1940 | port *pt = p->object.port; |
1940 | port *pt = port (p); |
1941 | |
1941 | |
1942 | pt->kind &= ~flag; |
1942 | pt->kind &= ~flag; |
1943 | |
1943 | |
1944 | if ((pt->kind & (port_input | port_output)) == 0) |
1944 | if ((pt->kind & (port_input | port_output)) == 0) |
1945 | { |
1945 | { |
… | |
… | |
1966 | /* get new character from input file */ |
1966 | /* get new character from input file */ |
1967 | static int |
1967 | static int |
1968 | inchar (SCHEME_P) |
1968 | inchar (SCHEME_P) |
1969 | { |
1969 | { |
1970 | int c; |
1970 | int c; |
1971 | port *pt; |
1971 | port *pt = port (SCHEME_V->inport); |
1972 | |
|
|
1973 | pt = SCHEME_V->inport->object.port; |
|
|
1974 | |
1972 | |
1975 | if (pt->kind & port_saw_EOF) |
1973 | if (pt->kind & port_saw_EOF) |
1976 | return EOF; |
1974 | return EOF; |
1977 | |
1975 | |
1978 | c = basic_inchar (pt); |
1976 | c = basic_inchar (pt); |
… | |
… | |
2045 | port *pt; |
2043 | port *pt; |
2046 | |
2044 | |
2047 | if (c == EOF) |
2045 | if (c == EOF) |
2048 | return; |
2046 | return; |
2049 | |
2047 | |
2050 | pt = SCHEME_V->inport->object.port; |
2048 | pt = port (SCHEME_V->inport); |
2051 | pt->unget = c; |
2049 | pt->unget = c; |
2052 | #else |
2050 | #else |
2053 | if (c == EOF) |
2051 | if (c == EOF) |
2054 | return; |
2052 | return; |
2055 | |
2053 | |
… | |
… | |
2083 | |
2081 | |
2084 | INTERFACE void |
2082 | INTERFACE void |
2085 | putstr (SCHEME_P_ const char *s) |
2083 | putstr (SCHEME_P_ const char *s) |
2086 | { |
2084 | { |
2087 | #if USE_PORTS |
2085 | #if USE_PORTS |
2088 | port *pt = SCHEME_V->outport->object.port; |
2086 | port *pt = port (SCHEME_V->outport); |
2089 | |
2087 | |
2090 | if (pt->kind & port_file) |
2088 | if (pt->kind & port_file) |
2091 | write (pt->rep.stdio.file, s, strlen (s)); |
2089 | write (pt->rep.stdio.file, s, strlen (s)); |
2092 | else |
2090 | else |
2093 | for (; *s; s++) |
2091 | for (; *s; s++) |
… | |
… | |
2103 | |
2101 | |
2104 | static void |
2102 | static void |
2105 | putchars (SCHEME_P_ const char *s, int len) |
2103 | putchars (SCHEME_P_ const char *s, int len) |
2106 | { |
2104 | { |
2107 | #if USE_PORTS |
2105 | #if USE_PORTS |
2108 | port *pt = SCHEME_V->outport->object.port; |
2106 | port *pt = port (SCHEME_V->outport); |
2109 | |
2107 | |
2110 | if (pt->kind & port_file) |
2108 | if (pt->kind & port_file) |
2111 | write (pt->rep.stdio.file, s, len); |
2109 | write (pt->rep.stdio.file, s, len); |
2112 | else |
2110 | else |
2113 | { |
2111 | { |
… | |
… | |
2127 | |
2125 | |
2128 | INTERFACE void |
2126 | INTERFACE void |
2129 | putcharacter (SCHEME_P_ int c) |
2127 | putcharacter (SCHEME_P_ int c) |
2130 | { |
2128 | { |
2131 | #if USE_PORTS |
2129 | #if USE_PORTS |
2132 | port *pt = SCHEME_V->outport->object.port; |
2130 | port *pt = port (SCHEME_V->outport); |
2133 | |
2131 | |
2134 | if (pt->kind & port_file) |
2132 | if (pt->kind & port_file) |
2135 | { |
2133 | { |
2136 | char cc = c; |
2134 | char cc = c; |
2137 | write (pt->rep.stdio.file, &cc, 1); |
2135 | write (pt->rep.stdio.file, &cc, 1); |
… | |
… | |
3079 | { |
3077 | { |
3080 | int nframes = (uintptr_t)SCHEME_V->dump; |
3078 | int nframes = (uintptr_t)SCHEME_V->dump; |
3081 | struct dump_stack_frame *next_frame; |
3079 | struct dump_stack_frame *next_frame; |
3082 | |
3080 | |
3083 | /* enough room for the next frame? */ |
3081 | /* enough room for the next frame? */ |
3084 | if (nframes >= SCHEME_V->dump_size) |
3082 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3085 | { |
3083 | { |
3086 | SCHEME_V->dump_size += STACK_GROWTH; |
3084 | SCHEME_V->dump_size += STACK_GROWTH; |
3087 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3085 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3088 | } |
3086 | } |
3089 | |
3087 | |
… | |
… | |
3346 | #endif |
3344 | #endif |
3347 | case OP_LOAD: /* load */ |
3345 | case OP_LOAD: /* load */ |
3348 | if (file_interactive (SCHEME_A)) |
3346 | if (file_interactive (SCHEME_A)) |
3349 | { |
3347 | { |
3350 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3348 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3351 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3349 | //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3352 | } |
3350 | } |
3353 | |
3351 | |
3354 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3352 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3355 | Error_1 ("unable to open", car (args)); |
3353 | Error_1 ("unable to open", car (args)); |
3356 | else |
3354 | else |
… | |
… | |
3360 | } |
3358 | } |
3361 | |
3359 | |
3362 | case OP_T0LVL: /* top level */ |
3360 | case OP_T0LVL: /* top level */ |
3363 | |
3361 | |
3364 | /* If we reached the end of file, this loop is done. */ |
3362 | /* If we reached the end of file, this loop is done. */ |
3365 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3363 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3366 | { |
3364 | { |
3367 | if (SCHEME_V->file_i == 0) |
3365 | if (SCHEME_V->file_i == 0) |
3368 | { |
3366 | { |
3369 | SCHEME_V->args = NIL; |
3367 | SCHEME_V->args = NIL; |
3370 | s_goto (OP_QUIT); |
3368 | s_goto (OP_QUIT); |
… | |
… | |
3448 | #endif |
3446 | #endif |
3449 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3447 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3450 | { |
3448 | { |
3451 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3449 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3452 | |
3450 | |
3453 | if (x != NIL) |
3451 | if (x == NIL) |
3454 | s_return (slot_value_in_env (x)); |
|
|
3455 | else |
|
|
3456 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3452 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
|
|
3453 | |
|
|
3454 | s_return (slot_value_in_env (x)); |
3457 | } |
3455 | } |
3458 | else if (is_pair (SCHEME_V->code)) |
3456 | else if (is_pair (SCHEME_V->code)) |
3459 | { |
3457 | { |
3460 | x = car (SCHEME_V->code); |
3458 | x = car (SCHEME_V->code); |
3461 | |
3459 | |
… | |
… | |
3538 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3536 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3539 | else if (is_foreign (SCHEME_V->code)) |
3537 | else if (is_foreign (SCHEME_V->code)) |
3540 | { |
3538 | { |
3541 | /* Keep nested calls from GC'ing the arglist */ |
3539 | /* Keep nested calls from GC'ing the arglist */ |
3542 | push_recent_alloc (SCHEME_A_ args, NIL); |
3540 | push_recent_alloc (SCHEME_A_ args, NIL); |
3543 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3541 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3544 | |
3542 | |
3545 | s_return (x); |
3543 | s_return (x); |
3546 | } |
3544 | } |
3547 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3545 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3548 | { |
3546 | { |
… | |
… | |
4642 | } |
4640 | } |
4643 | else |
4641 | else |
4644 | s_return (SCHEME_V->code); |
4642 | s_return (SCHEME_V->code); |
4645 | |
4643 | |
4646 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4644 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4647 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4645 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4648 | s_return (SCHEME_V->value); |
4646 | s_return (SCHEME_V->value); |
4649 | |
4647 | |
4650 | #if USE_PORTS |
4648 | #if USE_PORTS |
4651 | |
4649 | |
4652 | case OP_WRITE: /* write */ |
4650 | case OP_WRITE: /* write */ |
… | |
… | |
4797 | SCHEME_V->gc_verbose = (a != S_F); |
4795 | SCHEME_V->gc_verbose = (a != S_F); |
4798 | s_retbool (was); |
4796 | s_retbool (was); |
4799 | } |
4797 | } |
4800 | |
4798 | |
4801 | case OP_NEWSEGMENT: /* new-segment */ |
4799 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
4800 | #if 0 |
4802 | if (!is_pair (args) || !is_number (a)) |
4801 | if (!is_pair (args) || !is_number (a)) |
4803 | Error_0 ("new-segment: argument must be a number"); |
4802 | Error_0 ("new-segment: argument must be a number"); |
4804 | |
4803 | #endif |
4805 | alloc_cellseg (SCHEME_A_ ivalue (a)); |
4804 | s_retbool (alloc_cellseg (SCHEME_A)); |
4806 | |
|
|
4807 | s_return (S_T); |
|
|
4808 | |
4805 | |
4809 | case OP_OBLIST: /* oblist */ |
4806 | case OP_OBLIST: /* oblist */ |
4810 | s_return (oblist_all_symbols (SCHEME_A)); |
4807 | s_return (oblist_all_symbols (SCHEME_A)); |
4811 | |
4808 | |
4812 | #if USE_PORTS |
4809 | #if USE_PORTS |
… | |
… | |
4882 | s_return (p == NIL ? S_F : p); |
4879 | s_return (p == NIL ? S_F : p); |
4883 | } |
4880 | } |
4884 | |
4881 | |
4885 | case OP_GET_OUTSTRING: /* get-output-string */ |
4882 | case OP_GET_OUTSTRING: /* get-output-string */ |
4886 | { |
4883 | { |
4887 | port *p; |
4884 | port *p = port (a); |
4888 | |
4885 | |
4889 | if ((p = a->object.port)->kind & port_string) |
4886 | if (p->kind & port_string) |
4890 | { |
4887 | { |
4891 | off_t size; |
4888 | off_t size; |
4892 | char *str; |
4889 | char *str; |
4893 | |
4890 | |
4894 | size = p->rep.string.curr - p->rep.string.start + 1; |
4891 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4999 | int res; |
4996 | int res; |
5000 | |
4997 | |
5001 | if (is_pair (args)) |
4998 | if (is_pair (args)) |
5002 | p = car (args); |
4999 | p = car (args); |
5003 | |
5000 | |
5004 | res = p->object.port->kind & port_string; |
5001 | res = port (p)->kind & port_string; |
5005 | |
5002 | |
5006 | s_retbool (res); |
5003 | s_retbool (res); |
5007 | } |
5004 | } |
5008 | |
5005 | |
5009 | case OP_SET_INPORT: /* set-input-port */ |
5006 | case OP_SET_INPORT: /* set-input-port */ |
… | |
… | |
5673 | scheme_init (SCHEME_P) |
5670 | scheme_init (SCHEME_P) |
5674 | { |
5671 | { |
5675 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5672 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5676 | pointer x; |
5673 | pointer x; |
5677 | |
5674 | |
|
|
5675 | /* this memset is not strictly correct, as we assume (intcache) |
|
|
5676 | * that memset 0 will also set pointers to 0, but memset does |
|
|
5677 | * of course not guarantee that. screw such systems. |
|
|
5678 | */ |
5678 | memset (SCHEME_V, 0, sizeof (*SCHEME_V));//TODO !iso c |
5679 | memset (SCHEME_V, 0, sizeof (*SCHEME_V)); |
5679 | |
5680 | |
5680 | num_set_fixnum (num_zero, 1); |
5681 | num_set_fixnum (num_zero, 1); |
5681 | num_set_ivalue (num_zero, 0); |
5682 | num_set_ivalue (num_zero, 0); |
5682 | num_set_fixnum (num_one, 1); |
5683 | num_set_fixnum (num_one, 1); |
5683 | num_set_ivalue (num_one, 1); |
5684 | num_set_ivalue (num_one, 1); |
… | |
… | |
5695 | SCHEME_V->save_inport = NIL; |
5696 | SCHEME_V->save_inport = NIL; |
5696 | SCHEME_V->loadport = NIL; |
5697 | SCHEME_V->loadport = NIL; |
5697 | SCHEME_V->nesting = 0; |
5698 | SCHEME_V->nesting = 0; |
5698 | SCHEME_V->interactive_repl = 0; |
5699 | SCHEME_V->interactive_repl = 0; |
5699 | |
5700 | |
5700 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5701 | if (!alloc_cellseg (SCHEME_A)) |
5701 | { |
5702 | { |
5702 | #if USE_ERROR_CHECKING |
5703 | #if USE_ERROR_CHECKING |
5703 | SCHEME_V->no_memory = 1; |
5704 | SCHEME_V->no_memory = 1; |
5704 | return 0; |
5705 | return 0; |
5705 | #endif |
5706 | #endif |