… | |
… | |
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 | |
21 | #define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
23 | #define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
22 | #include "malloc.c" |
24 | #include "malloc.c" |
23 | |
25 | |
24 | #define SCHEME_SOURCE |
26 | #define SCHEME_SOURCE |
25 | #include "scheme-private.h" |
27 | #include "scheme-private.h" |
… | |
… | |
34 | |
36 | |
35 | #include <sys/types.h> |
37 | #include <sys/types.h> |
36 | #include <sys/stat.h> |
38 | #include <sys/stat.h> |
37 | #include <fcntl.h> |
39 | #include <fcntl.h> |
38 | |
40 | |
|
|
41 | #if !USE_ERROR_CHECKING |
|
|
42 | # define NDEBUG |
|
|
43 | #endif |
|
|
44 | |
|
|
45 | #include <assert.h> |
|
|
46 | #include <stdlib.h> |
39 | #include <string.h> |
47 | #include <string.h> |
40 | #include <stdlib.h> |
|
|
41 | |
48 | |
42 | #include <limits.h> |
49 | #include <limits.h> |
43 | #include <inttypes.h> |
50 | #include <inttypes.h> |
44 | #include <float.h> |
51 | #include <float.h> |
45 | //#include <ctype.h> |
52 | //#include <ctype.h> |
|
|
53 | |
|
|
54 | #if '1' != '0' + 1 \ |
|
|
55 | || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \ |
|
|
56 | || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \ |
|
|
57 | || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \ |
|
|
58 | || 'f' != 'a' + 5 |
|
|
59 | # error "execution character set digits not consecutive" |
|
|
60 | #endif |
46 | |
61 | |
47 | enum { |
62 | enum { |
48 | TOK_EOF, |
63 | TOK_EOF, |
49 | TOK_LPAREN, |
64 | TOK_LPAREN, |
50 | TOK_RPAREN, |
65 | TOK_RPAREN, |
51 | TOK_DOT, |
66 | TOK_DOT, |
52 | TOK_ATOM, |
67 | TOK_ATOM, |
|
|
68 | TOK_DOTATOM, /* atom name starting with '.' */ |
|
|
69 | TOK_STRATOM, /* atom name enclosed in | */ |
53 | TOK_QUOTE, |
70 | TOK_QUOTE, |
54 | TOK_DQUOTE, |
71 | TOK_DQUOTE, |
55 | TOK_BQUOTE, |
72 | TOK_BQUOTE, |
56 | TOK_COMMA, |
73 | TOK_COMMA, |
57 | TOK_ATMARK, |
74 | TOK_ATMARK, |
… | |
… | |
59 | TOK_SHARP_CONST, |
76 | TOK_SHARP_CONST, |
60 | TOK_VEC |
77 | TOK_VEC |
61 | }; |
78 | }; |
62 | |
79 | |
63 | #define BACKQUOTE '`' |
80 | #define BACKQUOTE '`' |
64 | #define DELIMITERS "()\";\f\t\v\n\r " |
81 | #define WHITESPACE " \t\r\n\v\f" |
|
|
82 | #define DELIMITERS "()\";" WHITESPACE |
65 | |
83 | |
66 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
84 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
67 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
85 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
68 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
86 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
69 | #define S_SINK (&SCHEME_V->xsink) |
87 | #define S_SINK (&SCHEME_V->xsink) |
… | |
… | |
270 | return type (p) == T_VECTOR; |
288 | return type (p) == T_VECTOR; |
271 | } |
289 | } |
272 | |
290 | |
273 | #define vecvalue(p) ((p)->object.vector.vvalue) |
291 | #define vecvalue(p) ((p)->object.vector.vvalue) |
274 | #define veclength(p) ((p)->object.vector.length) |
292 | #define veclength(p) ((p)->object.vector.length) |
275 | INTERFACE void fill_vector (pointer vec, pointer obj); |
293 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
276 | INTERFACE uint32_t vector_length (pointer vec); |
|
|
277 | INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); |
294 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
278 | INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); |
295 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
279 | |
|
|
280 | INTERFACE uint32_t |
|
|
281 | vector_length (pointer vec) |
|
|
282 | { |
|
|
283 | return vec->object.vector.length; |
|
|
284 | } |
|
|
285 | |
296 | |
286 | INTERFACE int |
297 | INTERFACE int |
287 | is_integer (pointer p) |
298 | is_integer (pointer p) |
288 | { |
299 | { |
289 | return type (p) == T_INTEGER; |
300 | return type (p) == T_INTEGER; |
… | |
… | |
398 | } |
409 | } |
399 | |
410 | |
400 | INTERFACE char * |
411 | INTERFACE char * |
401 | symname (pointer p) |
412 | symname (pointer p) |
402 | { |
413 | { |
403 | return strvalue (car (p)); |
414 | return strvalue (p); |
404 | } |
415 | } |
405 | |
416 | |
406 | #if USE_PLIST |
417 | #if USE_PLIST |
|
|
418 | #error plists are broken because symbols are no longer pairs |
|
|
419 | #define symprop(p) cdr(p) |
407 | SCHEME_EXPORT int |
420 | SCHEME_EXPORT int |
408 | hasprop (pointer p) |
421 | hasprop (pointer p) |
409 | { |
422 | { |
410 | return typeflag (p) & T_SYMBOL; |
423 | return typeflag (p) & T_SYMBOL; |
411 | } |
424 | } |
412 | |
|
|
413 | # define symprop(p) cdr(p) |
|
|
414 | #endif |
425 | #endif |
415 | |
426 | |
416 | INTERFACE int |
427 | INTERFACE int |
417 | is_syntax (pointer p) |
428 | is_syntax (pointer p) |
418 | { |
429 | { |
… | |
… | |
432 | } |
443 | } |
433 | |
444 | |
434 | INTERFACE char * |
445 | INTERFACE char * |
435 | syntaxname (pointer p) |
446 | syntaxname (pointer p) |
436 | { |
447 | { |
437 | return strvalue (car (p)); |
448 | return strvalue (p); |
438 | } |
449 | } |
439 | |
450 | |
440 | #define procnum(p) ivalue_unchecked (p) |
451 | #define procnum(p) ivalue_unchecked (p) |
441 | static const char *procname (pointer x); |
452 | static const char *procname (pointer x); |
442 | |
453 | |
… | |
… | |
658 | #endif |
669 | #endif |
659 | |
670 | |
660 | static int file_push (SCHEME_P_ const char *fname); |
671 | static int file_push (SCHEME_P_ const char *fname); |
661 | static void file_pop (SCHEME_P); |
672 | static void file_pop (SCHEME_P); |
662 | static int file_interactive (SCHEME_P); |
673 | static int file_interactive (SCHEME_P); |
663 | ecb_inline int is_one_of (char *s, int c); |
674 | ecb_inline int is_one_of (const char *s, int c); |
664 | static int alloc_cellseg (SCHEME_P_ int n); |
675 | static int alloc_cellseg (SCHEME_P_ int n); |
665 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
676 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
666 | static void finalize_cell (SCHEME_P_ pointer a); |
677 | static void finalize_cell (SCHEME_P_ pointer a); |
667 | static int count_consecutive_cells (pointer x, int needed); |
678 | static int count_consecutive_cells (pointer x, int needed); |
668 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
679 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
… | |
… | |
685 | static void mark (pointer a); |
696 | static void mark (pointer a); |
686 | static void gc (SCHEME_P_ pointer a, pointer b); |
697 | static void gc (SCHEME_P_ pointer a, pointer b); |
687 | static int basic_inchar (port *pt); |
698 | static int basic_inchar (port *pt); |
688 | static int inchar (SCHEME_P); |
699 | static int inchar (SCHEME_P); |
689 | static void backchar (SCHEME_P_ int c); |
700 | static void backchar (SCHEME_P_ int c); |
690 | static char *readstr_upto (SCHEME_P_ char *delim); |
701 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
691 | static pointer readstrexp (SCHEME_P); |
702 | static pointer readstrexp (SCHEME_P_ char delim); |
692 | ecb_inline int skipspace (SCHEME_P); |
703 | ecb_inline int skipspace (SCHEME_P); |
693 | static int token (SCHEME_P); |
704 | static int token (SCHEME_P); |
694 | static void printslashstring (SCHEME_P_ char *s, int len); |
705 | static void printslashstring (SCHEME_P_ char *s, int len); |
695 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
706 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
696 | static void printatom (SCHEME_P_ pointer l, int f); |
707 | static void printatom (SCHEME_P_ pointer l, int f); |
… | |
… | |
928 | return k; |
939 | return k; |
929 | |
940 | |
930 | i = ++SCHEME_V->last_cell_seg; |
941 | i = ++SCHEME_V->last_cell_seg; |
931 | SCHEME_V->alloc_seg[i] = cp; |
942 | SCHEME_V->alloc_seg[i] = cp; |
932 | |
943 | |
933 | /* insert new segment in address order */ |
|
|
934 | newp = (pointer)cp; |
944 | newp = (pointer)cp; |
935 | SCHEME_V->cell_seg[i] = newp; |
945 | SCHEME_V->cell_seg[i] = newp; |
936 | SCHEME_V->cell_segsize[i] = segsize; |
946 | SCHEME_V->cell_segsize[i] = segsize; |
937 | |
|
|
938 | //TODO: insert, not swap |
|
|
939 | while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i]) |
|
|
940 | { |
|
|
941 | p = SCHEME_V->cell_seg[i]; |
|
|
942 | SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1]; |
|
|
943 | SCHEME_V->cell_seg[i - 1] = p; |
|
|
944 | |
|
|
945 | k = SCHEME_V->cell_segsize[i]; |
|
|
946 | SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1]; |
|
|
947 | SCHEME_V->cell_segsize[i - 1] = k; |
|
|
948 | |
|
|
949 | --i; |
|
|
950 | } |
|
|
951 | |
|
|
952 | SCHEME_V->fcells += segsize; |
947 | SCHEME_V->fcells += segsize; |
953 | last = newp + segsize - 1; |
948 | last = newp + segsize - 1; |
954 | |
949 | |
955 | for (p = newp; p <= last; p++) |
950 | for (p = newp; p <= last; p++) |
956 | { |
951 | { |
957 | set_typeflag (p, T_PAIR); |
952 | set_typeflag (p, T_PAIR); |
958 | set_car (p, NIL); |
953 | set_car (p, NIL); |
959 | set_cdr (p, p + 1); |
954 | set_cdr (p, p + 1); |
960 | } |
955 | } |
961 | |
956 | |
962 | /* insert new cells in address order on free list */ |
|
|
963 | if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell) |
|
|
964 | { |
|
|
965 | set_cdr (last, SCHEME_V->free_cell); |
957 | set_cdr (last, SCHEME_V->free_cell); |
966 | SCHEME_V->free_cell = newp; |
958 | SCHEME_V->free_cell = newp; |
967 | } |
|
|
968 | else |
|
|
969 | { |
|
|
970 | p = SCHEME_V->free_cell; |
|
|
971 | |
|
|
972 | while (cdr (p) != NIL && newp > cdr (p)) |
|
|
973 | p = cdr (p); |
|
|
974 | |
|
|
975 | set_cdr (last, cdr (p)); |
|
|
976 | set_cdr (p, newp); |
|
|
977 | } |
|
|
978 | } |
959 | } |
979 | |
960 | |
980 | return n; |
961 | return n; |
981 | } |
962 | } |
982 | |
963 | |
… | |
… | |
989 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
970 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
990 | return S_SINK; |
971 | return S_SINK; |
991 | |
972 | |
992 | if (SCHEME_V->free_cell == NIL) |
973 | if (SCHEME_V->free_cell == NIL) |
993 | { |
974 | { |
994 | const int min_to_be_recovered = SCHEME_V->last_cell_seg < 128 ? 128 * 8 : SCHEME_V->last_cell_seg * 8; |
975 | const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1; |
995 | |
976 | |
996 | gc (SCHEME_A_ a, b); |
977 | gc (SCHEME_A_ a, b); |
997 | |
978 | |
998 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
979 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
999 | { |
980 | { |
… | |
… | |
1050 | } |
1031 | } |
1051 | |
1032 | |
1052 | static pointer |
1033 | static pointer |
1053 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1034 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1054 | { |
1035 | { |
1055 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
1036 | pointer v = get_cell_x (SCHEME_A_ NIL, NIL); |
1056 | pointer *e = malloc (len * sizeof (pointer)); |
1037 | pointer *e = malloc (len * sizeof (pointer)); |
1057 | |
1038 | |
1058 | if (!e && USE_ERROR_CHECKING) |
1039 | if (!e && USE_ERROR_CHECKING) |
1059 | return S_SINK; |
1040 | return S_SINK; |
1060 | |
1041 | |
1061 | /* Record it as a vector so that gc understands it. */ |
1042 | /* Record it as a vector so that gc understands it. */ |
1062 | set_typeflag (v, T_VECTOR | T_ATOM); |
1043 | set_typeflag (v, T_VECTOR | T_ATOM); |
1063 | |
1044 | |
1064 | v->object.vector.vvalue = e; |
1045 | v->object.vector.vvalue = e; |
1065 | v->object.vector.length = len; |
1046 | v->object.vector.length = len; |
1066 | fill_vector (v, init); |
1047 | fill_vector (v, 0, init); |
1067 | push_recent_alloc (SCHEME_A_ v, NIL); |
1048 | push_recent_alloc (SCHEME_A_ v, NIL); |
1068 | |
1049 | |
1069 | return v; |
1050 | return v; |
1070 | } |
1051 | } |
1071 | |
1052 | |
… | |
… | |
1114 | set_cdr (x, b); |
1095 | set_cdr (x, b); |
1115 | |
1096 | |
1116 | return x; |
1097 | return x; |
1117 | } |
1098 | } |
1118 | |
1099 | |
|
|
1100 | static pointer |
|
|
1101 | generate_symbol (SCHEME_P_ const char *name) |
|
|
1102 | { |
|
|
1103 | pointer x = mk_string (SCHEME_A_ name); |
|
|
1104 | setimmutable (x); |
|
|
1105 | set_typeflag (x, T_SYMBOL | T_ATOM); |
|
|
1106 | return x; |
|
|
1107 | } |
|
|
1108 | |
1119 | /* ========== oblist implementation ========== */ |
1109 | /* ========== oblist implementation ========== */ |
1120 | |
1110 | |
1121 | #ifndef USE_OBJECT_LIST |
1111 | #ifndef USE_OBJECT_LIST |
1122 | |
1112 | |
|
|
1113 | static int |
1123 | static int hash_fn (const char *key, int table_size); |
1114 | hash_fn (const char *key, int table_size) |
|
|
1115 | { |
|
|
1116 | const unsigned char *p = key; |
|
|
1117 | uint32_t hash = 2166136261; |
|
|
1118 | |
|
|
1119 | while (*p) |
|
|
1120 | hash = (hash ^ *p++) * 16777619; |
|
|
1121 | |
|
|
1122 | return hash % table_size; |
|
|
1123 | } |
1124 | |
1124 | |
1125 | static pointer |
1125 | static pointer |
1126 | oblist_initial_value (SCHEME_P) |
1126 | oblist_initial_value (SCHEME_P) |
1127 | { |
1127 | { |
1128 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1128 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
… | |
… | |
1130 | |
1130 | |
1131 | /* returns the new symbol */ |
1131 | /* returns the new symbol */ |
1132 | static pointer |
1132 | static pointer |
1133 | oblist_add_by_name (SCHEME_P_ const char *name) |
1133 | oblist_add_by_name (SCHEME_P_ const char *name) |
1134 | { |
1134 | { |
1135 | int location; |
1135 | pointer x = generate_symbol (SCHEME_A_ name); |
1136 | |
|
|
1137 | pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1138 | set_typeflag (x, T_SYMBOL); |
|
|
1139 | setimmutable (car (x)); |
|
|
1140 | |
|
|
1141 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1136 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1142 | set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); |
1137 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1143 | return x; |
1138 | return x; |
1144 | } |
1139 | } |
1145 | |
1140 | |
1146 | ecb_inline pointer |
1141 | ecb_inline pointer |
1147 | oblist_find_by_name (SCHEME_P_ const char *name) |
1142 | oblist_find_by_name (SCHEME_P_ const char *name) |
… | |
… | |
1150 | pointer x; |
1145 | pointer x; |
1151 | char *s; |
1146 | char *s; |
1152 | |
1147 | |
1153 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1148 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1154 | |
1149 | |
1155 | for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1150 | for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1156 | { |
1151 | { |
1157 | s = symname (car (x)); |
1152 | s = symname (car (x)); |
1158 | |
1153 | |
1159 | /* case-insensitive, per R5RS section 2 */ |
1154 | /* case-insensitive, per R5RS section 2 */ |
1160 | if (stricmp (name, s) == 0) |
1155 | if (stricmp (name, s) == 0) |
… | |
… | |
1170 | int i; |
1165 | int i; |
1171 | pointer x; |
1166 | pointer x; |
1172 | pointer ob_list = NIL; |
1167 | pointer ob_list = NIL; |
1173 | |
1168 | |
1174 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1169 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1175 | for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1170 | for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1176 | ob_list = cons (x, ob_list); |
1171 | ob_list = cons (x, ob_list); |
1177 | |
1172 | |
1178 | return ob_list; |
1173 | return ob_list; |
1179 | } |
1174 | } |
1180 | |
1175 | |
… | |
… | |
1206 | |
1201 | |
1207 | /* returns the new symbol */ |
1202 | /* returns the new symbol */ |
1208 | static pointer |
1203 | static pointer |
1209 | oblist_add_by_name (SCHEME_P_ const char *name) |
1204 | oblist_add_by_name (SCHEME_P_ const char *name) |
1210 | { |
1205 | { |
1211 | pointer x; |
1206 | pointer x = generate_symbol (SCHEME_A_ name); |
1212 | |
|
|
1213 | x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1214 | set_typeflag (x, T_SYMBOL); |
|
|
1215 | setimmutable (car (x)); |
|
|
1216 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1207 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1217 | return x; |
1208 | return x; |
1218 | } |
1209 | } |
1219 | |
1210 | |
1220 | static pointer |
1211 | static pointer |
… | |
… | |
1241 | pointer |
1232 | pointer |
1242 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1233 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1243 | { |
1234 | { |
1244 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1235 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1245 | |
1236 | |
1246 | set_typeflag (x, (T_FOREIGN | T_ATOM)); |
1237 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1247 | x->object.ff = f; |
1238 | x->object.ff = f; |
1248 | |
1239 | |
1249 | return x; |
1240 | return x; |
1250 | } |
1241 | } |
1251 | |
1242 | |
1252 | INTERFACE pointer |
1243 | INTERFACE pointer |
1253 | mk_character (SCHEME_P_ int c) |
1244 | mk_character (SCHEME_P_ int c) |
1254 | { |
1245 | { |
1255 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1246 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1256 | |
1247 | |
1257 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1248 | set_typeflag (x, T_CHARACTER | T_ATOM); |
1258 | set_ivalue (x, c & 0xff); |
1249 | set_ivalue (x, c & 0xff); |
1259 | |
1250 | |
1260 | return x; |
1251 | return x; |
1261 | } |
1252 | } |
1262 | |
1253 | |
1263 | /* get number atom (integer) */ |
1254 | /* get number atom (integer) */ |
1264 | INTERFACE pointer |
1255 | INTERFACE pointer |
1265 | mk_integer (SCHEME_P_ long n) |
1256 | mk_integer (SCHEME_P_ long n) |
1266 | { |
1257 | { |
|
|
1258 | pointer p = 0; |
|
|
1259 | pointer *pp = &p; |
|
|
1260 | |
|
|
1261 | #if USE_INTCACHE |
|
|
1262 | if (n >= INTCACHE_MIN && n <= INTCACHE_MAX) |
|
|
1263 | pp = &SCHEME_V->intcache[n - INTCACHE_MIN]; |
|
|
1264 | #endif |
|
|
1265 | |
|
|
1266 | if (!*pp) |
|
|
1267 | { |
1267 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1268 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1268 | |
1269 | |
1269 | set_typeflag (x, (T_INTEGER | T_ATOM)); |
1270 | set_typeflag (x, T_INTEGER | T_ATOM); |
|
|
1271 | setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */ |
1270 | set_ivalue (x, n); |
1272 | set_ivalue (x, n); |
1271 | |
1273 | |
|
|
1274 | *pp = x; |
|
|
1275 | } |
|
|
1276 | |
1272 | return x; |
1277 | return *pp; |
1273 | } |
1278 | } |
1274 | |
1279 | |
1275 | INTERFACE pointer |
1280 | INTERFACE pointer |
1276 | mk_real (SCHEME_P_ RVALUE n) |
1281 | mk_real (SCHEME_P_ RVALUE n) |
1277 | { |
1282 | { |
1278 | #if USE_REAL |
1283 | #if USE_REAL |
1279 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1284 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1280 | |
1285 | |
1281 | set_typeflag (x, (T_REAL | T_ATOM)); |
1286 | set_typeflag (x, T_REAL | T_ATOM); |
1282 | set_rvalue (x, n); |
1287 | set_rvalue (x, n); |
1283 | |
1288 | |
1284 | return x; |
1289 | return x; |
1285 | #else |
1290 | #else |
1286 | return mk_integer (SCHEME_A_ n); |
1291 | return mk_integer (SCHEME_A_ n); |
… | |
… | |
1310 | SCHEME_V->no_memory = 1; |
1315 | SCHEME_V->no_memory = 1; |
1311 | return SCHEME_V->strbuff; |
1316 | return SCHEME_V->strbuff; |
1312 | } |
1317 | } |
1313 | |
1318 | |
1314 | if (str) |
1319 | if (str) |
1315 | { |
1320 | memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */ |
1316 | int l = strlen (str); |
|
|
1317 | |
|
|
1318 | if (l > len_str) |
|
|
1319 | l = len_str; |
|
|
1320 | |
|
|
1321 | memcpy (q, str, l); |
|
|
1322 | q[l] = 0; |
|
|
1323 | } |
|
|
1324 | else |
1321 | else |
1325 | { |
|
|
1326 | memset (q, fill, len_str); |
1322 | memset (q, fill, len_str); |
|
|
1323 | |
1327 | q[len_str] = 0; |
1324 | q[len_str] = 0; |
1328 | } |
|
|
1329 | |
1325 | |
1330 | return q; |
1326 | return q; |
1331 | } |
1327 | } |
1332 | |
1328 | |
1333 | INTERFACE pointer |
1329 | INTERFACE pointer |
… | |
… | |
1347 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1343 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1348 | |
1344 | |
1349 | set_typeflag (x, T_STRING | T_ATOM); |
1345 | set_typeflag (x, T_STRING | T_ATOM); |
1350 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1346 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1351 | strlength (x) = len; |
1347 | strlength (x) = len; |
|
|
1348 | |
1352 | return x; |
1349 | return x; |
1353 | } |
1350 | } |
1354 | |
1351 | |
1355 | INTERFACE pointer |
1352 | INTERFACE pointer |
1356 | mk_string (SCHEME_P_ const char *str) |
1353 | mk_string (SCHEME_P_ const char *str) |
… | |
… | |
1363 | { |
1360 | { |
1364 | return get_vector_object (SCHEME_A_ len, NIL); |
1361 | return get_vector_object (SCHEME_A_ len, NIL); |
1365 | } |
1362 | } |
1366 | |
1363 | |
1367 | INTERFACE void |
1364 | INTERFACE void |
1368 | fill_vector (pointer vec, pointer obj) |
1365 | fill_vector (pointer vec, uint32_t start, pointer obj) |
1369 | { |
1366 | { |
1370 | int i; |
1367 | int i; |
1371 | |
1368 | |
1372 | for (i = 0; i < vec->object.vector.length; i++) |
1369 | for (i = start; i < veclength (vec); i++) |
1373 | vecvalue (vec)[i] = obj; |
1370 | vecvalue (vec)[i] = obj; |
1374 | } |
1371 | } |
1375 | |
1372 | |
|
|
1373 | INTERFACE void |
|
|
1374 | vector_resize (pointer vec, uint32_t newsize, pointer fill) |
|
|
1375 | { |
|
|
1376 | uint32_t oldsize = veclength (vec); |
|
|
1377 | vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer)); |
|
|
1378 | veclength (vec) = newsize; |
|
|
1379 | fill_vector (vec, oldsize, fill); |
|
|
1380 | } |
|
|
1381 | |
1376 | INTERFACE pointer |
1382 | INTERFACE pointer |
1377 | vector_elem (pointer vec, uint32_t ielem) |
1383 | vector_get (pointer vec, uint32_t ielem) |
1378 | { |
1384 | { |
1379 | return vecvalue(vec)[ielem]; |
1385 | return vecvalue(vec)[ielem]; |
1380 | } |
1386 | } |
1381 | |
1387 | |
1382 | INTERFACE void |
1388 | INTERFACE void |
1383 | set_vector_elem (pointer vec, uint32_t ielem, pointer a) |
1389 | vector_set (pointer vec, uint32_t ielem, pointer a) |
1384 | { |
1390 | { |
1385 | vecvalue(vec)[ielem] = a; |
1391 | vecvalue(vec)[ielem] = a; |
1386 | } |
1392 | } |
1387 | |
1393 | |
1388 | /* get new symbol */ |
1394 | /* get new symbol */ |
… | |
… | |
1400 | |
1406 | |
1401 | INTERFACE pointer |
1407 | INTERFACE pointer |
1402 | gensym (SCHEME_P) |
1408 | gensym (SCHEME_P) |
1403 | { |
1409 | { |
1404 | pointer x; |
1410 | pointer x; |
1405 | |
|
|
1406 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
|
|
1407 | { |
|
|
1408 | char name[40] = "gensym-"; |
1411 | char name[40] = "gensym-"; |
1409 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1412 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1410 | |
1413 | |
1411 | /* first check oblist */ |
1414 | return generate_symbol (SCHEME_A_ name); |
1412 | x = oblist_find_by_name (SCHEME_A_ name); |
1415 | } |
1413 | |
1416 | |
1414 | if (x == NIL) |
1417 | static int |
1415 | { |
1418 | is_gensym (SCHEME_P_ pointer x) |
1416 | x = oblist_add_by_name (SCHEME_A_ name); |
1419 | { |
1417 | return x; |
1420 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1418 | } |
|
|
1419 | } |
|
|
1420 | |
|
|
1421 | return NIL; |
|
|
1422 | } |
1421 | } |
1423 | |
1422 | |
1424 | /* make symbol or number atom from string */ |
1423 | /* make symbol or number atom from string */ |
1425 | static pointer |
1424 | static pointer |
1426 | mk_atom (SCHEME_P_ char *q) |
1425 | mk_atom (SCHEME_P_ char *q) |
… | |
… | |
1578 | |
1577 | |
1579 | if (ecb_expect_false (is_vector (p))) |
1578 | if (ecb_expect_false (is_vector (p))) |
1580 | { |
1579 | { |
1581 | int i; |
1580 | int i; |
1582 | |
1581 | |
1583 | for (i = 0; i < p->object.vector.length; i++) |
1582 | for (i = 0; i < veclength (p); i++) |
1584 | mark (vecvalue (p)[i]); |
1583 | mark (vecvalue (p)[i]); |
1585 | } |
1584 | } |
1586 | |
1585 | |
1587 | if (is_atom (p)) |
1586 | if (is_atom (p)) |
1588 | goto E6; |
1587 | goto E6; |
… | |
… | |
1661 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1660 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1662 | mark (car (S_SINK)); |
1661 | mark (car (S_SINK)); |
1663 | /* Mark any older stuff above nested C calls */ |
1662 | /* Mark any older stuff above nested C calls */ |
1664 | mark (SCHEME_V->c_nest); |
1663 | mark (SCHEME_V->c_nest); |
1665 | |
1664 | |
|
|
1665 | #if USE_INTCACHE |
|
|
1666 | /* mark intcache */ |
|
|
1667 | for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i) |
|
|
1668 | if (SCHEME_V->intcache[i - INTCACHE_MIN]) |
|
|
1669 | mark (SCHEME_V->intcache[i - INTCACHE_MIN]); |
|
|
1670 | #endif |
|
|
1671 | |
1666 | /* mark variables a, b */ |
1672 | /* mark variables a, b */ |
1667 | mark (a); |
1673 | mark (a); |
1668 | mark (b); |
1674 | mark (b); |
1669 | |
1675 | |
1670 | /* garbage collect */ |
1676 | /* garbage collect */ |
1671 | clrmark (NIL); |
1677 | clrmark (NIL); |
1672 | SCHEME_V->fcells = 0; |
1678 | SCHEME_V->fcells = 0; |
1673 | SCHEME_V->free_cell = NIL; |
1679 | SCHEME_V->free_cell = NIL; |
1674 | |
1680 | |
1675 | /* free-list is kept sorted by address so as to maintain consecutive |
1681 | if (SCHEME_V->gc_verbose) |
1676 | ranges, if possible, for use with vectors. Here we scan the cells |
1682 | xwrstr ("freeing..."); |
1677 | (which are also kept sorted by address) downwards to build the |
1683 | |
1678 | free-list in sorted order. |
1684 | uint32_t total = 0; |
1679 | */ |
1685 | |
|
|
1686 | /* Here we scan the cells to build the free-list. */ |
1680 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1687 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1681 | { |
1688 | { |
1682 | p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
1689 | pointer end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1690 | total += SCHEME_V->cell_segsize [i]; |
1683 | |
1691 | |
1684 | while (--p >= SCHEME_V->cell_seg[i]) |
1692 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
1685 | { |
1693 | { |
1686 | if (is_mark (p)) |
1694 | if (is_mark (p)) |
1687 | clrmark (p); |
1695 | clrmark (p); |
1688 | else |
1696 | else |
1689 | { |
1697 | { |
… | |
… | |
1702 | } |
1710 | } |
1703 | } |
1711 | } |
1704 | |
1712 | |
1705 | if (SCHEME_V->gc_verbose) |
1713 | if (SCHEME_V->gc_verbose) |
1706 | { |
1714 | { |
1707 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); |
1715 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" out of "); xwrnum (total); xwrstr (" cells were recovered.\n"); |
1708 | } |
1716 | } |
1709 | } |
1717 | } |
1710 | |
1718 | |
1711 | static void |
1719 | static void |
1712 | finalize_cell (SCHEME_P_ pointer a) |
1720 | finalize_cell (SCHEME_P_ pointer a) |
1713 | { |
1721 | { |
1714 | /* TODO, fast bitmap check? */ |
1722 | /* TODO, fast bitmap check? */ |
1715 | if (is_string (a)) |
1723 | if (is_string (a) || is_symbol (a)) |
1716 | free (strvalue (a)); |
1724 | free (strvalue (a)); |
1717 | else if (is_vector (a)) |
1725 | else if (is_vector (a)) |
1718 | free (vecvalue (a)); |
1726 | free (vecvalue (a)); |
1719 | #if USE_PORTS |
1727 | #if USE_PORTS |
1720 | else if (is_port (a)) |
1728 | else if (is_port (a)) |
… | |
… | |
2142 | #endif |
2150 | #endif |
2143 | } |
2151 | } |
2144 | |
2152 | |
2145 | /* read characters up to delimiter, but cater to character constants */ |
2153 | /* read characters up to delimiter, but cater to character constants */ |
2146 | static char * |
2154 | static char * |
2147 | readstr_upto (SCHEME_P_ char *delim) |
2155 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2148 | { |
2156 | { |
2149 | char *p = SCHEME_V->strbuff; |
2157 | char *p = SCHEME_V->strbuff + skip; |
2150 | |
2158 | |
2151 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2159 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2152 | |
2160 | |
2153 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2161 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2154 | *p = 0; |
2162 | *p = 0; |
… | |
… | |
2161 | return SCHEME_V->strbuff; |
2169 | return SCHEME_V->strbuff; |
2162 | } |
2170 | } |
2163 | |
2171 | |
2164 | /* read string expression "xxx...xxx" */ |
2172 | /* read string expression "xxx...xxx" */ |
2165 | static pointer |
2173 | static pointer |
2166 | readstrexp (SCHEME_P) |
2174 | readstrexp (SCHEME_P_ char delim) |
2167 | { |
2175 | { |
2168 | char *p = SCHEME_V->strbuff; |
2176 | char *p = SCHEME_V->strbuff; |
2169 | int c; |
2177 | int c; |
2170 | int c1 = 0; |
2178 | int c1 = 0; |
2171 | enum |
|
|
2172 | { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2179 | enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2173 | |
2180 | |
2174 | for (;;) |
2181 | for (;;) |
2175 | { |
2182 | { |
2176 | c = inchar (SCHEME_A); |
2183 | c = inchar (SCHEME_A); |
2177 | |
2184 | |
… | |
… | |
2179 | return S_F; |
2186 | return S_F; |
2180 | |
2187 | |
2181 | switch (state) |
2188 | switch (state) |
2182 | { |
2189 | { |
2183 | case st_ok: |
2190 | case st_ok: |
2184 | switch (c) |
2191 | if (ecb_expect_false (c == delim)) |
2185 | { |
|
|
2186 | case '\\': |
|
|
2187 | state = st_bsl; |
|
|
2188 | break; |
|
|
2189 | |
|
|
2190 | case '"': |
|
|
2191 | *p = 0; |
|
|
2192 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2192 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2193 | |
2193 | |
2194 | default: |
2194 | if (ecb_expect_false (c == '\\')) |
|
|
2195 | state = st_bsl; |
|
|
2196 | else |
2195 | *p++ = c; |
2197 | *p++ = c; |
2196 | break; |
|
|
2197 | } |
|
|
2198 | |
2198 | |
2199 | break; |
2199 | break; |
2200 | |
2200 | |
2201 | case st_bsl: |
2201 | case st_bsl: |
2202 | switch (c) |
2202 | switch (c) |
… | |
… | |
2232 | case 'r': |
2232 | case 'r': |
2233 | *p++ = '\r'; |
2233 | *p++ = '\r'; |
2234 | state = st_ok; |
2234 | state = st_ok; |
2235 | break; |
2235 | break; |
2236 | |
2236 | |
2237 | case '"': |
|
|
2238 | *p++ = '"'; |
|
|
2239 | state = st_ok; |
|
|
2240 | break; |
|
|
2241 | |
|
|
2242 | default: |
2237 | default: |
2243 | *p++ = c; |
2238 | *p++ = c; |
2244 | state = st_ok; |
2239 | state = st_ok; |
2245 | break; |
2240 | break; |
2246 | } |
2241 | } |
2247 | |
2242 | |
2248 | break; |
2243 | break; |
2249 | |
2244 | |
2250 | case st_x1: |
2245 | case st_x1: |
2251 | case st_x2: |
2246 | case st_x2: |
2252 | c = toupper (c); |
2247 | c = tolower (c); |
2253 | |
2248 | |
2254 | if (c >= '0' && c <= 'F') |
2249 | if (c >= '0' && c <= '9') |
2255 | { |
|
|
2256 | if (c <= '9') |
|
|
2257 | c1 = (c1 << 4) + c - '0'; |
2250 | c1 = (c1 << 4) + c - '0'; |
2258 | else |
2251 | else if (c >= 'a' && c <= 'f') |
2259 | c1 = (c1 << 4) + c - 'A' + 10; |
2252 | c1 = (c1 << 4) + c - 'a' + 10; |
2260 | |
|
|
2261 | if (state == st_x1) |
|
|
2262 | state = st_x2; |
|
|
2263 | else |
|
|
2264 | { |
|
|
2265 | *p++ = c1; |
|
|
2266 | state = st_ok; |
|
|
2267 | } |
|
|
2268 | } |
|
|
2269 | else |
2253 | else |
2270 | return S_F; |
2254 | return S_F; |
|
|
2255 | |
|
|
2256 | if (state == st_x1) |
|
|
2257 | state = st_x2; |
|
|
2258 | else |
|
|
2259 | { |
|
|
2260 | *p++ = c1; |
|
|
2261 | state = st_ok; |
|
|
2262 | } |
2271 | |
2263 | |
2272 | break; |
2264 | break; |
2273 | |
2265 | |
2274 | case st_oct1: |
2266 | case st_oct1: |
2275 | case st_oct2: |
2267 | case st_oct2: |
… | |
… | |
2279 | backchar (SCHEME_A_ c); |
2271 | backchar (SCHEME_A_ c); |
2280 | state = st_ok; |
2272 | state = st_ok; |
2281 | } |
2273 | } |
2282 | else |
2274 | else |
2283 | { |
2275 | { |
2284 | if (state == st_oct2 && c1 >= 32) |
2276 | if (state == st_oct2 && c1 >= ' ') |
2285 | return S_F; |
2277 | return S_F; |
2286 | |
2278 | |
2287 | c1 = (c1 << 3) + (c - '0'); |
2279 | c1 = (c1 << 3) + (c - '0'); |
2288 | |
2280 | |
2289 | if (state == st_oct1) |
2281 | if (state == st_oct1) |
… | |
… | |
2294 | state = st_ok; |
2286 | state = st_ok; |
2295 | } |
2287 | } |
2296 | } |
2288 | } |
2297 | |
2289 | |
2298 | break; |
2290 | break; |
2299 | |
|
|
2300 | } |
2291 | } |
2301 | } |
2292 | } |
2302 | } |
2293 | } |
2303 | |
2294 | |
2304 | /* check c is in chars */ |
2295 | /* check c is in chars */ |
2305 | ecb_inline int |
2296 | ecb_inline int |
2306 | is_one_of (char *s, int c) |
2297 | is_one_of (const char *s, int c) |
2307 | { |
2298 | { |
2308 | if (c == EOF) |
|
|
2309 | return 1; |
|
|
2310 | |
|
|
2311 | return !!strchr (s, c); |
2299 | return c == EOF || !!strchr (s, c); |
2312 | } |
2300 | } |
2313 | |
2301 | |
2314 | /* skip white characters */ |
2302 | /* skip white characters */ |
2315 | ecb_inline int |
2303 | ecb_inline int |
2316 | skipspace (SCHEME_P) |
2304 | skipspace (SCHEME_P) |
… | |
… | |
2318 | int c, curr_line = 0; |
2306 | int c, curr_line = 0; |
2319 | |
2307 | |
2320 | do |
2308 | do |
2321 | { |
2309 | { |
2322 | c = inchar (SCHEME_A); |
2310 | c = inchar (SCHEME_A); |
|
|
2311 | |
2323 | #if SHOW_ERROR_LINE |
2312 | #if SHOW_ERROR_LINE |
2324 | if (c == '\n') |
2313 | if (ecb_expect_false (c == '\n')) |
2325 | curr_line++; |
2314 | curr_line++; |
2326 | #endif |
2315 | #endif |
|
|
2316 | |
|
|
2317 | if (ecb_expect_false (c == EOF)) |
|
|
2318 | return c; |
2327 | } |
2319 | } |
2328 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2320 | while (is_one_of (WHITESPACE, c)); |
2329 | |
2321 | |
2330 | /* record it */ |
2322 | /* record it */ |
2331 | #if SHOW_ERROR_LINE |
2323 | #if SHOW_ERROR_LINE |
2332 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2324 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2333 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2325 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2334 | #endif |
2326 | #endif |
2335 | |
2327 | |
2336 | if (c != EOF) |
|
|
2337 | { |
|
|
2338 | backchar (SCHEME_A_ c); |
2328 | backchar (SCHEME_A_ c); |
2339 | return 1; |
2329 | return 1; |
2340 | } |
|
|
2341 | else |
|
|
2342 | return EOF; |
|
|
2343 | } |
2330 | } |
2344 | |
2331 | |
2345 | /* get token */ |
2332 | /* get token */ |
2346 | static int |
2333 | static int |
2347 | token (SCHEME_P) |
2334 | token (SCHEME_P) |
… | |
… | |
2363 | return TOK_RPAREN; |
2350 | return TOK_RPAREN; |
2364 | |
2351 | |
2365 | case '.': |
2352 | case '.': |
2366 | c = inchar (SCHEME_A); |
2353 | c = inchar (SCHEME_A); |
2367 | |
2354 | |
2368 | if (is_one_of (" \n\t", c)) |
2355 | if (is_one_of (WHITESPACE, c)) |
2369 | return TOK_DOT; |
2356 | return TOK_DOT; |
2370 | else |
2357 | else |
2371 | { |
2358 | { |
2372 | //TODO: ungetc twice in a row is not supported in C |
|
|
2373 | backchar (SCHEME_A_ c); |
2359 | backchar (SCHEME_A_ c); |
2374 | backchar (SCHEME_A_ '.'); |
|
|
2375 | return TOK_ATOM; |
2360 | return TOK_DOTATOM; |
2376 | } |
2361 | } |
|
|
2362 | |
|
|
2363 | case '|': |
|
|
2364 | return TOK_STRATOM; |
2377 | |
2365 | |
2378 | case '\'': |
2366 | case '\'': |
2379 | return TOK_QUOTE; |
2367 | return TOK_QUOTE; |
2380 | |
2368 | |
2381 | case ';': |
2369 | case ';': |
… | |
… | |
2513 | } |
2501 | } |
2514 | |
2502 | |
2515 | putcharacter (SCHEME_A_ '"'); |
2503 | putcharacter (SCHEME_A_ '"'); |
2516 | } |
2504 | } |
2517 | |
2505 | |
2518 | |
|
|
2519 | /* print atoms */ |
2506 | /* print atoms */ |
2520 | static void |
2507 | static void |
2521 | printatom (SCHEME_P_ pointer l, int f) |
2508 | printatom (SCHEME_P_ pointer l, int f) |
2522 | { |
2509 | { |
2523 | char *p; |
2510 | char *p; |
2524 | int len; |
2511 | int len; |
2525 | |
2512 | |
2526 | atom2str (SCHEME_A_ l, f, &p, &len); |
2513 | atom2str (SCHEME_A_ l, f, &p, &len); |
2527 | putchars (SCHEME_A_ p, len); |
2514 | putchars (SCHEME_A_ p, len); |
2528 | } |
2515 | } |
2529 | |
|
|
2530 | |
2516 | |
2531 | /* Uses internal buffer unless string pointer is already available */ |
2517 | /* Uses internal buffer unless string pointer is already available */ |
2532 | static void |
2518 | static void |
2533 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2519 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2534 | { |
2520 | { |
… | |
… | |
2695 | #endif |
2681 | #endif |
2696 | } |
2682 | } |
2697 | else if (is_continuation (l)) |
2683 | else if (is_continuation (l)) |
2698 | p = "#<CONTINUATION>"; |
2684 | p = "#<CONTINUATION>"; |
2699 | else |
2685 | else |
|
|
2686 | { |
|
|
2687 | #if USE_PRINTF |
|
|
2688 | p = SCHEME_V->strbuff; |
|
|
2689 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2690 | #else |
2700 | p = "#<ERROR>"; |
2691 | p = "#<ERROR>"; |
|
|
2692 | #endif |
|
|
2693 | } |
2701 | |
2694 | |
2702 | *pp = p; |
2695 | *pp = p; |
2703 | *plen = strlen (p); |
2696 | *plen = strlen (p); |
2704 | } |
2697 | } |
2705 | |
2698 | |
… | |
… | |
2849 | /* () is #t in R5RS */ |
2842 | /* () is #t in R5RS */ |
2850 | #define is_true(p) ((p) != S_F) |
2843 | #define is_true(p) ((p) != S_F) |
2851 | #define is_false(p) ((p) == S_F) |
2844 | #define is_false(p) ((p) == S_F) |
2852 | |
2845 | |
2853 | /* ========== Environment implementation ========== */ |
2846 | /* ========== Environment implementation ========== */ |
2854 | |
|
|
2855 | #if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) |
|
|
2856 | |
|
|
2857 | static int |
|
|
2858 | hash_fn (const char *key, int table_size) |
|
|
2859 | { |
|
|
2860 | const unsigned char *p = key; |
|
|
2861 | uint32_t hash = 2166136261; |
|
|
2862 | |
|
|
2863 | while (*p) |
|
|
2864 | hash = (hash ^ *p++) * 16777619; |
|
|
2865 | |
|
|
2866 | return hash % table_size; |
|
|
2867 | } |
|
|
2868 | #endif |
|
|
2869 | |
2847 | |
2870 | #ifndef USE_ALIST_ENV |
2848 | #ifndef USE_ALIST_ENV |
2871 | |
2849 | |
2872 | /* |
2850 | /* |
2873 | * In this implementation, each frame of the environment may be |
2851 | * In this implementation, each frame of the environment may be |
… | |
… | |
2890 | |
2868 | |
2891 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2869 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2892 | setenvironment (SCHEME_V->envir); |
2870 | setenvironment (SCHEME_V->envir); |
2893 | } |
2871 | } |
2894 | |
2872 | |
|
|
2873 | static uint32_t |
|
|
2874 | sym_hash (pointer sym, uint32_t size) |
|
|
2875 | { |
|
|
2876 | uintptr_t ptr = (uintptr_t)sym; |
|
|
2877 | |
|
|
2878 | #if 0 |
|
|
2879 | /* table size is prime, so why mix */ |
|
|
2880 | ptr += ptr >> 32; |
|
|
2881 | ptr += ptr >> 16; |
|
|
2882 | ptr += ptr >> 8; |
|
|
2883 | #endif |
|
|
2884 | |
|
|
2885 | return ptr % size; |
|
|
2886 | } |
|
|
2887 | |
2895 | ecb_inline void |
2888 | ecb_inline void |
2896 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2889 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2897 | { |
2890 | { |
2898 | pointer slot = immutable_cons (variable, value); |
2891 | pointer slot = immutable_cons (variable, value); |
2899 | |
2892 | |
2900 | if (is_vector (car (env))) |
2893 | if (is_vector (car (env))) |
2901 | { |
2894 | { |
2902 | int location = hash_fn (symname (variable), veclength (car (env))); |
2895 | int location = sym_hash (variable, veclength (car (env))); |
2903 | |
|
|
2904 | set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); |
2896 | vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); |
2905 | } |
2897 | } |
2906 | else |
2898 | else |
2907 | set_car (env, immutable_cons (slot, car (env))); |
2899 | set_car (env, immutable_cons (slot, car (env))); |
2908 | } |
2900 | } |
2909 | |
2901 | |
2910 | static pointer |
2902 | static pointer |
2911 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2903 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2912 | { |
2904 | { |
2913 | pointer x, y; |
2905 | pointer x, y; |
2914 | int location; |
|
|
2915 | |
2906 | |
2916 | for (x = env; x != NIL; x = cdr (x)) |
2907 | for (x = env; x != NIL; x = cdr (x)) |
2917 | { |
2908 | { |
2918 | if (is_vector (car (x))) |
2909 | if (is_vector (car (x))) |
2919 | { |
2910 | { |
2920 | location = hash_fn (symname (hdl), veclength (car (x))); |
2911 | int location = sym_hash (hdl, veclength (car (x))); |
2921 | y = vector_elem (car (x), location); |
2912 | y = vector_get (car (x), location); |
2922 | } |
2913 | } |
2923 | else |
2914 | else |
2924 | y = car (x); |
2915 | y = car (x); |
2925 | |
2916 | |
2926 | for (; y != NIL; y = cdr (y)) |
2917 | for (; y != NIL; y = cdr (y)) |
2927 | if (caar (y) == hdl) |
2918 | if (caar (y) == hdl) |
2928 | break; |
2919 | break; |
2929 | |
2920 | |
2930 | if (y != NIL) |
2921 | if (y != NIL) |
|
|
2922 | return car (y); |
|
|
2923 | |
|
|
2924 | if (!all) |
2931 | break; |
2925 | break; |
2932 | |
|
|
2933 | if (!all) |
|
|
2934 | return NIL; |
|
|
2935 | } |
2926 | } |
2936 | |
|
|
2937 | if (x != NIL) |
|
|
2938 | return car (y); |
|
|
2939 | |
2927 | |
2940 | return NIL; |
2928 | return NIL; |
2941 | } |
2929 | } |
2942 | |
2930 | |
2943 | #else /* USE_ALIST_ENV */ |
2931 | #else /* USE_ALIST_ENV */ |
… | |
… | |
2965 | for (y = car (x); y != NIL; y = cdr (y)) |
2953 | for (y = car (x); y != NIL; y = cdr (y)) |
2966 | if (caar (y) == hdl) |
2954 | if (caar (y) == hdl) |
2967 | break; |
2955 | break; |
2968 | |
2956 | |
2969 | if (y != NIL) |
2957 | if (y != NIL) |
|
|
2958 | return car (y); |
2970 | break; |
2959 | break; |
2971 | |
2960 | |
2972 | if (!all) |
2961 | if (!all) |
2973 | return NIL; |
2962 | break; |
2974 | } |
2963 | } |
2975 | |
|
|
2976 | if (x != NIL) |
|
|
2977 | return car (y); |
|
|
2978 | |
2964 | |
2979 | return NIL; |
2965 | return NIL; |
2980 | } |
2966 | } |
2981 | |
2967 | |
2982 | #endif /* USE_ALIST_ENV else */ |
2968 | #endif /* USE_ALIST_ENV else */ |
2983 | |
2969 | |
2984 | ecb_inline void |
2970 | ecb_inline void |
2985 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2971 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2986 | { |
2972 | { |
|
|
2973 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2987 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2974 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2988 | } |
2975 | } |
2989 | |
2976 | |
2990 | ecb_inline void |
2977 | ecb_inline void |
2991 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2978 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
… | |
… | |
3283 | |
3270 | |
3284 | #endif |
3271 | #endif |
3285 | |
3272 | |
3286 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3273 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3287 | |
3274 | |
|
|
3275 | #if EXPERIMENT |
|
|
3276 | static int |
|
|
3277 | debug (SCHEME_P_ int indent, pointer x) |
|
|
3278 | { |
|
|
3279 | int c; |
|
|
3280 | |
|
|
3281 | if (is_syntax (x)) |
|
|
3282 | { |
|
|
3283 | printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x)); |
|
|
3284 | return 8 + 8; |
|
|
3285 | } |
|
|
3286 | |
|
|
3287 | if (x == NIL) |
|
|
3288 | { |
|
|
3289 | printf ("%*sNIL\n", indent, ""); |
|
|
3290 | return 3; |
|
|
3291 | } |
|
|
3292 | |
|
|
3293 | switch (type (x)) |
|
|
3294 | { |
|
|
3295 | case T_INTEGER: |
|
|
3296 | printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x); |
|
|
3297 | return 32+8; |
|
|
3298 | |
|
|
3299 | case T_SYMBOL: |
|
|
3300 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
|
|
3301 | return 24+8; |
|
|
3302 | |
|
|
3303 | case T_CLOSURE: |
|
|
3304 | printf ("%*sS<%s>\n", indent, "", "closure"); |
|
|
3305 | debug (SCHEME_A_ indent + 3, cdr(x)); |
|
|
3306 | return 32 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3307 | |
|
|
3308 | case T_PAIR: |
|
|
3309 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
|
|
3310 | c = debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3311 | c += debug (SCHEME_A_ indent + 3, cdr (x)); |
|
|
3312 | return c + 1; |
|
|
3313 | |
|
|
3314 | case T_PORT: |
|
|
3315 | printf ("%*sS<%s>\n", indent, "", "port"); |
|
|
3316 | return 24+8; |
|
|
3317 | |
|
|
3318 | case T_VECTOR: |
|
|
3319 | printf ("%*sS<%s>\n", indent, "", "vector"); |
|
|
3320 | return 24+8; |
|
|
3321 | |
|
|
3322 | case T_ENVIRONMENT: |
|
|
3323 | printf ("%*sS<%s>\n", indent, "", "environment"); |
|
|
3324 | return 0 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3325 | |
|
|
3326 | default: |
|
|
3327 | printf ("unhandled type %d\n", type (x)); |
|
|
3328 | break; |
|
|
3329 | } |
|
|
3330 | } |
|
|
3331 | #endif |
|
|
3332 | |
3288 | static int |
3333 | static int |
3289 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3334 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3290 | { |
3335 | { |
3291 | pointer args = SCHEME_V->args; |
3336 | pointer args = SCHEME_V->args; |
3292 | pointer x, y; |
3337 | pointer x, y; |
3293 | |
3338 | |
3294 | switch (op) |
3339 | switch (op) |
3295 | { |
3340 | { |
|
|
3341 | #if EXPERIMENT //D |
|
|
3342 | case OP_DEBUG: |
|
|
3343 | printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); |
|
|
3344 | printf ("\n"); |
|
|
3345 | s_return (S_T); |
|
|
3346 | #endif |
3296 | case OP_LOAD: /* load */ |
3347 | case OP_LOAD: /* load */ |
3297 | if (file_interactive (SCHEME_A)) |
3348 | if (file_interactive (SCHEME_A)) |
3298 | { |
3349 | { |
3299 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3350 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3300 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3351 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
… | |
… | |
3423 | } |
3474 | } |
3424 | else |
3475 | else |
3425 | s_return (SCHEME_V->code); |
3476 | s_return (SCHEME_V->code); |
3426 | |
3477 | |
3427 | case OP_E0ARGS: /* eval arguments */ |
3478 | case OP_E0ARGS: /* eval arguments */ |
3428 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3479 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3429 | { |
3480 | { |
3430 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3481 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3431 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3482 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3432 | SCHEME_V->code = SCHEME_V->value; |
3483 | SCHEME_V->code = SCHEME_V->value; |
3433 | s_goto (OP_APPLY); |
3484 | s_goto (OP_APPLY); |
… | |
… | |
3534 | |
3585 | |
3535 | case OP_DOMACRO: /* do macro */ |
3586 | case OP_DOMACRO: /* do macro */ |
3536 | SCHEME_V->code = SCHEME_V->value; |
3587 | SCHEME_V->code = SCHEME_V->value; |
3537 | s_goto (OP_EVAL); |
3588 | s_goto (OP_EVAL); |
3538 | |
3589 | |
3539 | #if 1 |
|
|
3540 | |
|
|
3541 | case OP_LAMBDA: /* lambda */ |
3590 | case OP_LAMBDA: /* lambda */ |
3542 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3591 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3543 | set SCHEME_V->value fall thru */ |
3592 | set SCHEME_V->value fall thru */ |
3544 | { |
3593 | { |
3545 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3594 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
… | |
… | |
3551 | SCHEME_V->code = slot_value_in_env (f); |
3600 | SCHEME_V->code = slot_value_in_env (f); |
3552 | s_goto (OP_APPLY); |
3601 | s_goto (OP_APPLY); |
3553 | } |
3602 | } |
3554 | |
3603 | |
3555 | SCHEME_V->value = SCHEME_V->code; |
3604 | SCHEME_V->value = SCHEME_V->code; |
3556 | /* Fallthru */ |
|
|
3557 | } |
3605 | } |
|
|
3606 | /* Fallthru */ |
3558 | |
3607 | |
3559 | case OP_LAMBDA1: |
3608 | case OP_LAMBDA1: |
3560 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3609 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3561 | |
|
|
3562 | #else |
|
|
3563 | |
|
|
3564 | case OP_LAMBDA: /* lambda */ |
|
|
3565 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
|
|
3566 | |
|
|
3567 | #endif |
|
|
3568 | |
3610 | |
3569 | case OP_MKCLOSURE: /* make-closure */ |
3611 | case OP_MKCLOSURE: /* make-closure */ |
3570 | x = car (args); |
3612 | x = car (args); |
3571 | |
3613 | |
3572 | if (car (x) == SCHEME_V->LAMBDA) |
3614 | if (car (x) == SCHEME_V->LAMBDA) |
… | |
… | |
4168 | else |
4210 | else |
4169 | Error_0 ("modulo: division by zero"); |
4211 | Error_0 ("modulo: division by zero"); |
4170 | |
4212 | |
4171 | s_return (mk_number (SCHEME_A_ v)); |
4213 | s_return (mk_number (SCHEME_A_ v)); |
4172 | |
4214 | |
4173 | case OP_CAR: /* car */ |
4215 | /* the compiler will optimize this mess... */ |
4174 | s_return (caar (args)); |
4216 | case OP_CAR: op_car: s_return (car (x)); |
4175 | |
4217 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4176 | case OP_CDR: /* cdr */ |
4218 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4177 | s_return (cdar (args)); |
4219 | case OP_CADR: op_cadr: x = cdr (x); goto op_car; |
|
|
4220 | case OP_CDAR: op_cdar: x = car (x); goto op_cdr; |
|
|
4221 | case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr; |
|
|
4222 | case OP_CAAAR: op_caaar: x = car (x); goto op_caar; |
|
|
4223 | case OP_CAADR: op_caadr: x = cdr (x); goto op_caar; |
|
|
4224 | case OP_CADAR: op_cadar: x = car (x); goto op_cadr; |
|
|
4225 | case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr; |
|
|
4226 | case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar; |
|
|
4227 | case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar; |
|
|
4228 | case OP_CDDAR: op_cddar: x = car (x); goto op_cddr; |
|
|
4229 | case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr; |
|
|
4230 | case OP_CAAAAR: x = car (x); goto op_caaar; |
|
|
4231 | case OP_CAAADR: x = cdr (x); goto op_caaar; |
|
|
4232 | case OP_CAADAR: x = car (x); goto op_caadr; |
|
|
4233 | case OP_CAADDR: x = cdr (x); goto op_caadr; |
|
|
4234 | case OP_CADAAR: x = car (x); goto op_cadar; |
|
|
4235 | case OP_CADADR: x = cdr (x); goto op_cadar; |
|
|
4236 | case OP_CADDAR: x = car (x); goto op_caddr; |
|
|
4237 | case OP_CADDDR: x = cdr (x); goto op_caddr; |
|
|
4238 | case OP_CDAAAR: x = car (x); goto op_cdaar; |
|
|
4239 | case OP_CDAADR: x = cdr (x); goto op_cdaar; |
|
|
4240 | case OP_CDADAR: x = car (x); goto op_cdadr; |
|
|
4241 | case OP_CDADDR: x = cdr (x); goto op_cdadr; |
|
|
4242 | case OP_CDDAAR: x = car (x); goto op_cddar; |
|
|
4243 | case OP_CDDADR: x = cdr (x); goto op_cddar; |
|
|
4244 | case OP_CDDDAR: x = car (x); goto op_cdddr; |
|
|
4245 | case OP_CDDDDR: x = cdr (x); goto op_cdddr; |
4178 | |
4246 | |
4179 | case OP_CONS: /* cons */ |
4247 | case OP_CONS: /* cons */ |
4180 | set_cdr (args, cadr (args)); |
4248 | set_cdr (args, cadr (args)); |
4181 | s_return (args); |
4249 | s_return (args); |
4182 | |
4250 | |
… | |
… | |
4401 | if (SCHEME_V->no_memory) |
4469 | if (SCHEME_V->no_memory) |
4402 | s_return (S_SINK); |
4470 | s_return (S_SINK); |
4403 | #endif |
4471 | #endif |
4404 | |
4472 | |
4405 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4473 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4406 | set_vector_elem (vec, i, car (x)); |
4474 | vector_set (vec, i, car (x)); |
4407 | |
4475 | |
4408 | s_return (vec); |
4476 | s_return (vec); |
4409 | } |
4477 | } |
4410 | |
4478 | |
4411 | case OP_MKVECTOR: /* make-vector */ |
4479 | case OP_MKVECTOR: /* make-vector */ |
… | |
… | |
4423 | if (SCHEME_V->no_memory) |
4491 | if (SCHEME_V->no_memory) |
4424 | s_return (S_SINK); |
4492 | s_return (S_SINK); |
4425 | #endif |
4493 | #endif |
4426 | |
4494 | |
4427 | if (fill != NIL) |
4495 | if (fill != NIL) |
4428 | fill_vector (vec, fill); |
4496 | fill_vector (vec, 0, fill); |
4429 | |
4497 | |
4430 | s_return (vec); |
4498 | s_return (vec); |
4431 | } |
4499 | } |
4432 | |
4500 | |
4433 | case OP_VECLEN: /* vector-length */ |
4501 | case OP_VECLEN: /* vector-length */ |
4434 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4502 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
|
|
4503 | |
|
|
4504 | case OP_VECRESIZE: |
|
|
4505 | vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args)); |
|
|
4506 | s_return (x); |
4435 | |
4507 | |
4436 | case OP_VECREF: /* vector-ref */ |
4508 | case OP_VECREF: /* vector-ref */ |
4437 | { |
4509 | { |
4438 | int index = ivalue_unchecked (cadr (args)); |
4510 | int index = ivalue_unchecked (cadr (args)); |
4439 | |
4511 | |
4440 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4512 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4441 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4513 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4442 | |
4514 | |
4443 | s_return (vector_elem (x, index)); |
4515 | s_return (vector_get (x, index)); |
4444 | } |
4516 | } |
4445 | |
4517 | |
4446 | case OP_VECSET: /* vector-set! */ |
4518 | case OP_VECSET: /* vector-set! */ |
4447 | { |
4519 | { |
4448 | int index = ivalue_unchecked (cadr (args)); |
4520 | int index = ivalue_unchecked (cadr (args)); |
… | |
… | |
4451 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4523 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4452 | |
4524 | |
4453 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4525 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4454 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4526 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4455 | |
4527 | |
4456 | set_vector_elem (x, index, caddr (args)); |
4528 | vector_set (x, index, caddr (args)); |
4457 | s_return (x); |
4529 | s_return (x); |
4458 | } |
4530 | } |
4459 | } |
4531 | } |
4460 | |
4532 | |
4461 | if (USE_ERROR_CHECKING) abort (); |
4533 | if (USE_ERROR_CHECKING) abort (); |
… | |
… | |
4500 | pointer d = cdr (args); |
4572 | pointer d = cdr (args); |
4501 | int r; |
4573 | int r; |
4502 | |
4574 | |
4503 | switch (op) |
4575 | switch (op) |
4504 | { |
4576 | { |
4505 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4577 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4506 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; |
4578 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break; |
4507 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4579 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4508 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4580 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4509 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
4581 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
|
|
4582 | case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break; |
4510 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4583 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4511 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4584 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4512 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4585 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4513 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4586 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4514 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4587 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4515 | |
4588 | |
4516 | #if USE_CHAR_CLASSIFIERS |
4589 | #if USE_CHAR_CLASSIFIERS |
4517 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4590 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4518 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4591 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4519 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4592 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
… | |
… | |
4995 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5068 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
4996 | SCHEME_V->tok = token (SCHEME_A); |
5069 | SCHEME_V->tok = token (SCHEME_A); |
4997 | s_goto (OP_RDSEXPR); |
5070 | s_goto (OP_RDSEXPR); |
4998 | |
5071 | |
4999 | case TOK_ATOM: |
5072 | case TOK_ATOM: |
5000 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); |
5073 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); |
|
|
5074 | |
|
|
5075 | case TOK_DOTATOM: |
|
|
5076 | SCHEME_V->strbuff[0] = '.'; |
|
|
5077 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
|
|
5078 | |
|
|
5079 | case TOK_STRATOM: |
|
|
5080 | x = readstrexp (SCHEME_A_ '|'); |
|
|
5081 | //TODO: haven't checked whether the garbage collector could interfere |
|
|
5082 | s_return (mk_atom (SCHEME_A_ strvalue (x))); |
5001 | |
5083 | |
5002 | case TOK_DQUOTE: |
5084 | case TOK_DQUOTE: |
5003 | x = readstrexp (SCHEME_A); |
5085 | x = readstrexp (SCHEME_A_ '"'); |
5004 | |
5086 | |
5005 | if (x == S_F) |
5087 | if (x == S_F) |
5006 | Error_0 ("Error reading string"); |
5088 | Error_0 ("Error reading string"); |
5007 | |
5089 | |
5008 | setimmutable (x); |
5090 | setimmutable (x); |
… | |
… | |
5020 | s_goto (OP_EVAL); |
5102 | s_goto (OP_EVAL); |
5021 | } |
5103 | } |
5022 | } |
5104 | } |
5023 | |
5105 | |
5024 | case TOK_SHARP_CONST: |
5106 | case TOK_SHARP_CONST: |
5025 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) |
5107 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) |
5026 | Error_0 ("undefined sharp expression"); |
5108 | Error_0 ("undefined sharp expression"); |
5027 | else |
5109 | else |
5028 | s_return (x); |
5110 | s_return (x); |
5029 | |
5111 | |
5030 | default: |
5112 | default: |
… | |
… | |
5182 | putstr (SCHEME_A_ ")"); |
5264 | putstr (SCHEME_A_ ")"); |
5183 | s_return (S_T); |
5265 | s_return (S_T); |
5184 | } |
5266 | } |
5185 | else |
5267 | else |
5186 | { |
5268 | { |
5187 | pointer elem = vector_elem (vec, i); |
5269 | pointer elem = vector_get (vec, i); |
5188 | |
5270 | |
5189 | ivalue_unchecked (cdr (args)) = i + 1; |
5271 | ivalue_unchecked (cdr (args)) = i + 1; |
5190 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5272 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5191 | SCHEME_V->args = elem; |
5273 | SCHEME_V->args = elem; |
5192 | |
5274 | |
… | |
… | |
5252 | |
5334 | |
5253 | case OP_CLOSUREP: /* closure? */ |
5335 | case OP_CLOSUREP: /* closure? */ |
5254 | /* |
5336 | /* |
5255 | * Note, macro object is also a closure. |
5337 | * Note, macro object is also a closure. |
5256 | * Therefore, (closure? <#MACRO>) ==> #t |
5338 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5339 | * (schmorp) well, obviously not, fix? TODO |
5257 | */ |
5340 | */ |
5258 | s_retbool (is_closure (a)); |
5341 | s_retbool (is_closure (a)); |
5259 | |
5342 | |
5260 | case OP_MACROP: /* macro? */ |
5343 | case OP_MACROP: /* macro? */ |
5261 | s_retbool (is_macro (a)); |
5344 | s_retbool (is_macro (a)); |
… | |
… | |
5430 | { |
5513 | { |
5431 | if (!tests[j - 1].fct (arg)) |
5514 | if (!tests[j - 1].fct (arg)) |
5432 | break; |
5515 | break; |
5433 | } |
5516 | } |
5434 | |
5517 | |
5435 | if (t[1]) /* last test is replicated as necessary */ |
5518 | if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */ |
5436 | t++; |
5519 | t++; |
5437 | |
5520 | |
5438 | arglist = cdr (arglist); |
5521 | arglist = cdr (arglist); |
5439 | i++; |
5522 | i++; |
5440 | } |
5523 | } |
… | |
… | |
5502 | |
5585 | |
5503 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5586 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5504 | static int |
5587 | static int |
5505 | syntaxnum (pointer p) |
5588 | syntaxnum (pointer p) |
5506 | { |
5589 | { |
5507 | const char *s = strvalue (car (p)); |
5590 | const char *s = strvalue (p); |
5508 | |
5591 | |
5509 | switch (strlength (car (p))) |
5592 | switch (strlength (p)) |
5510 | { |
5593 | { |
5511 | case 2: |
5594 | case 2: |
5512 | if (s[0] == 'i') |
5595 | if (s[0] == 'i') |
5513 | return OP_IF0; /* if */ |
5596 | return OP_IF0; /* if */ |
5514 | else |
5597 | else |
… | |
… | |
5589 | ecb_cold int |
5672 | ecb_cold int |
5590 | scheme_init (SCHEME_P) |
5673 | scheme_init (SCHEME_P) |
5591 | { |
5674 | { |
5592 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5675 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5593 | pointer x; |
5676 | pointer x; |
|
|
5677 | |
|
|
5678 | memset (SCHEME_V, 0, sizeof (*SCHEME_V));//TODO !iso c |
5594 | |
5679 | |
5595 | num_set_fixnum (num_zero, 1); |
5680 | num_set_fixnum (num_zero, 1); |
5596 | num_set_ivalue (num_zero, 0); |
5681 | num_set_ivalue (num_zero, 0); |
5597 | num_set_fixnum (num_one, 1); |
5682 | num_set_fixnum (num_one, 1); |
5598 | num_set_ivalue (num_one, 1); |
5683 | num_set_ivalue (num_one, 1); |
… | |
… | |
5950 | # endif |
6035 | # endif |
5951 | int fin; |
6036 | int fin; |
5952 | char *file_name = InitFile; |
6037 | char *file_name = InitFile; |
5953 | int retcode; |
6038 | int retcode; |
5954 | int isfile = 1; |
6039 | int isfile = 1; |
|
|
6040 | system ("ps v $PPID");//D |
5955 | |
6041 | |
5956 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6042 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5957 | { |
6043 | { |
5958 | xwrstr ("Usage: tinyscheme -?\n"); |
6044 | xwrstr ("Usage: tinyscheme -?\n"); |
5959 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6045 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |