… | |
… | |
34 | |
34 | |
35 | #include <sys/types.h> |
35 | #include <sys/types.h> |
36 | #include <sys/stat.h> |
36 | #include <sys/stat.h> |
37 | #include <fcntl.h> |
37 | #include <fcntl.h> |
38 | |
38 | |
|
|
39 | #if !USE_ERROR_CHECKING |
|
|
40 | # define NDEBUG |
|
|
41 | #endif |
|
|
42 | |
|
|
43 | #include <assert.h> |
|
|
44 | #include <stdlib.h> |
39 | #include <string.h> |
45 | #include <string.h> |
40 | #include <stdlib.h> |
|
|
41 | |
46 | |
42 | #include <limits.h> |
47 | #include <limits.h> |
43 | #include <inttypes.h> |
48 | #include <inttypes.h> |
44 | #include <float.h> |
49 | #include <float.h> |
45 | //#include <ctype.h> |
50 | //#include <ctype.h> |
|
|
51 | |
|
|
52 | #if '1' != '0' + 1 \ |
|
|
53 | || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \ |
|
|
54 | || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \ |
|
|
55 | || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \ |
|
|
56 | || 'f' != 'a' + 5 |
|
|
57 | # error "execution character set digits not consecutive" |
|
|
58 | #endif |
46 | |
59 | |
47 | enum { |
60 | enum { |
48 | TOK_EOF, |
61 | TOK_EOF, |
49 | TOK_LPAREN, |
62 | TOK_LPAREN, |
50 | TOK_RPAREN, |
63 | TOK_RPAREN, |
51 | TOK_DOT, |
64 | TOK_DOT, |
52 | TOK_ATOM, |
65 | TOK_ATOM, |
|
|
66 | TOK_DOTATOM, /* atom name starting with '.' */ |
|
|
67 | TOK_STRATOM, /* atom name enclosed in | */ |
53 | TOK_QUOTE, |
68 | TOK_QUOTE, |
54 | TOK_DQUOTE, |
69 | TOK_DQUOTE, |
55 | TOK_BQUOTE, |
70 | TOK_BQUOTE, |
56 | TOK_COMMA, |
71 | TOK_COMMA, |
57 | TOK_ATMARK, |
72 | TOK_ATMARK, |
… | |
… | |
651 | #endif |
666 | #endif |
652 | |
667 | |
653 | static int file_push (SCHEME_P_ const char *fname); |
668 | static int file_push (SCHEME_P_ const char *fname); |
654 | static void file_pop (SCHEME_P); |
669 | static void file_pop (SCHEME_P); |
655 | static int file_interactive (SCHEME_P); |
670 | static int file_interactive (SCHEME_P); |
656 | ecb_inline int is_one_of (char *s, int c); |
671 | ecb_inline int is_one_of (const char *s, int c); |
657 | static int alloc_cellseg (SCHEME_P_ int n); |
672 | static int alloc_cellseg (SCHEME_P_ int n); |
658 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
673 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
659 | static void finalize_cell (SCHEME_P_ pointer a); |
674 | static void finalize_cell (SCHEME_P_ pointer a); |
660 | static int count_consecutive_cells (pointer x, int needed); |
675 | static int count_consecutive_cells (pointer x, int needed); |
661 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
676 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
… | |
… | |
678 | static void mark (pointer a); |
693 | static void mark (pointer a); |
679 | static void gc (SCHEME_P_ pointer a, pointer b); |
694 | static void gc (SCHEME_P_ pointer a, pointer b); |
680 | static int basic_inchar (port *pt); |
695 | static int basic_inchar (port *pt); |
681 | static int inchar (SCHEME_P); |
696 | static int inchar (SCHEME_P); |
682 | static void backchar (SCHEME_P_ int c); |
697 | static void backchar (SCHEME_P_ int c); |
683 | static char *readstr_upto (SCHEME_P_ char *delim); |
698 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
684 | static pointer readstrexp (SCHEME_P); |
699 | static pointer readstrexp (SCHEME_P_ char delim); |
685 | ecb_inline int skipspace (SCHEME_P); |
700 | ecb_inline int skipspace (SCHEME_P); |
686 | static int token (SCHEME_P); |
701 | static int token (SCHEME_P); |
687 | static void printslashstring (SCHEME_P_ char *s, int len); |
702 | static void printslashstring (SCHEME_P_ char *s, int len); |
688 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
703 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
689 | static void printatom (SCHEME_P_ pointer l, int f); |
704 | static void printatom (SCHEME_P_ pointer l, int f); |
… | |
… | |
1079 | return x; |
1094 | return x; |
1080 | } |
1095 | } |
1081 | |
1096 | |
1082 | /* ========== oblist implementation ========== */ |
1097 | /* ========== oblist implementation ========== */ |
1083 | |
1098 | |
|
|
1099 | static pointer |
|
|
1100 | generate_symbol (SCHEME_P_ const char *name) |
|
|
1101 | { |
|
|
1102 | pointer x = mk_string (SCHEME_A_ name); |
|
|
1103 | setimmutable (x); |
|
|
1104 | x = immutable_cons (x, NIL); |
|
|
1105 | set_typeflag (x, T_SYMBOL); |
|
|
1106 | return x; |
|
|
1107 | } |
|
|
1108 | |
1084 | #ifndef USE_OBJECT_LIST |
1109 | #ifndef USE_OBJECT_LIST |
1085 | |
1110 | |
1086 | static int |
1111 | static int |
1087 | hash_fn (const char *key, int table_size) |
1112 | hash_fn (const char *key, int table_size) |
1088 | { |
1113 | { |
… | |
… | |
1103 | |
1128 | |
1104 | /* returns the new symbol */ |
1129 | /* returns the new symbol */ |
1105 | static pointer |
1130 | static pointer |
1106 | oblist_add_by_name (SCHEME_P_ const char *name) |
1131 | oblist_add_by_name (SCHEME_P_ const char *name) |
1107 | { |
1132 | { |
1108 | int location; |
1133 | pointer x = generate_symbol (SCHEME_A_ name); |
1109 | |
|
|
1110 | pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1111 | set_typeflag (x, T_SYMBOL); |
|
|
1112 | setimmutable (car (x)); |
|
|
1113 | |
|
|
1114 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1134 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1115 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1135 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1116 | return x; |
1136 | return x; |
1117 | } |
1137 | } |
1118 | |
1138 | |
1119 | ecb_inline pointer |
1139 | ecb_inline pointer |
… | |
… | |
1281 | SCHEME_V->no_memory = 1; |
1301 | SCHEME_V->no_memory = 1; |
1282 | return SCHEME_V->strbuff; |
1302 | return SCHEME_V->strbuff; |
1283 | } |
1303 | } |
1284 | |
1304 | |
1285 | if (str) |
1305 | if (str) |
1286 | { |
1306 | memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */ |
1287 | int l = strlen (str); |
|
|
1288 | |
|
|
1289 | if (l > len_str) |
|
|
1290 | l = len_str; |
|
|
1291 | |
|
|
1292 | memcpy (q, str, l); |
|
|
1293 | q[l] = 0; |
|
|
1294 | } |
|
|
1295 | else |
1307 | else |
1296 | { |
|
|
1297 | memset (q, fill, len_str); |
1308 | memset (q, fill, len_str); |
|
|
1309 | |
1298 | q[len_str] = 0; |
1310 | q[len_str] = 0; |
1299 | } |
|
|
1300 | |
1311 | |
1301 | return q; |
1312 | return q; |
1302 | } |
1313 | } |
1303 | |
1314 | |
1304 | INTERFACE pointer |
1315 | INTERFACE pointer |
… | |
… | |
1318 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1329 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1319 | |
1330 | |
1320 | set_typeflag (x, T_STRING | T_ATOM); |
1331 | set_typeflag (x, T_STRING | T_ATOM); |
1321 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1332 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1322 | strlength (x) = len; |
1333 | strlength (x) = len; |
|
|
1334 | |
1323 | return x; |
1335 | return x; |
1324 | } |
1336 | } |
1325 | |
1337 | |
1326 | INTERFACE pointer |
1338 | INTERFACE pointer |
1327 | mk_string (SCHEME_P_ const char *str) |
1339 | mk_string (SCHEME_P_ const char *str) |
… | |
… | |
1371 | |
1383 | |
1372 | INTERFACE pointer |
1384 | INTERFACE pointer |
1373 | gensym (SCHEME_P) |
1385 | gensym (SCHEME_P) |
1374 | { |
1386 | { |
1375 | pointer x; |
1387 | pointer x; |
1376 | |
|
|
1377 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
|
|
1378 | { |
|
|
1379 | char name[40] = "gensym-"; |
1388 | char name[40] = "gensym-"; |
1380 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1389 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1381 | |
1390 | |
1382 | /* first check oblist */ |
1391 | return generate_symbol (SCHEME_A_ name); |
1383 | x = oblist_find_by_name (SCHEME_A_ name); |
|
|
1384 | |
|
|
1385 | if (x == NIL) |
|
|
1386 | { |
|
|
1387 | x = oblist_add_by_name (SCHEME_A_ name); |
|
|
1388 | return x; |
|
|
1389 | } |
|
|
1390 | } |
|
|
1391 | |
|
|
1392 | return NIL; |
|
|
1393 | } |
1392 | } |
1394 | |
1393 | |
1395 | /* make symbol or number atom from string */ |
1394 | /* make symbol or number atom from string */ |
1396 | static pointer |
1395 | static pointer |
1397 | mk_atom (SCHEME_P_ char *q) |
1396 | mk_atom (SCHEME_P_ char *q) |
… | |
… | |
2113 | #endif |
2112 | #endif |
2114 | } |
2113 | } |
2115 | |
2114 | |
2116 | /* read characters up to delimiter, but cater to character constants */ |
2115 | /* read characters up to delimiter, but cater to character constants */ |
2117 | static char * |
2116 | static char * |
2118 | readstr_upto (SCHEME_P_ char *delim) |
2117 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2119 | { |
2118 | { |
2120 | char *p = SCHEME_V->strbuff; |
2119 | char *p = SCHEME_V->strbuff + skip; |
2121 | |
2120 | |
2122 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2121 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2123 | |
2122 | |
2124 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2123 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2125 | *p = 0; |
2124 | *p = 0; |
… | |
… | |
2132 | return SCHEME_V->strbuff; |
2131 | return SCHEME_V->strbuff; |
2133 | } |
2132 | } |
2134 | |
2133 | |
2135 | /* read string expression "xxx...xxx" */ |
2134 | /* read string expression "xxx...xxx" */ |
2136 | static pointer |
2135 | static pointer |
2137 | readstrexp (SCHEME_P) |
2136 | readstrexp (SCHEME_P_ char delim) |
2138 | { |
2137 | { |
2139 | char *p = SCHEME_V->strbuff; |
2138 | char *p = SCHEME_V->strbuff; |
2140 | int c; |
2139 | int c; |
2141 | int c1 = 0; |
2140 | int c1 = 0; |
2142 | enum |
|
|
2143 | { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2141 | enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2144 | |
2142 | |
2145 | for (;;) |
2143 | for (;;) |
2146 | { |
2144 | { |
2147 | c = inchar (SCHEME_A); |
2145 | c = inchar (SCHEME_A); |
2148 | |
2146 | |
… | |
… | |
2150 | return S_F; |
2148 | return S_F; |
2151 | |
2149 | |
2152 | switch (state) |
2150 | switch (state) |
2153 | { |
2151 | { |
2154 | case st_ok: |
2152 | case st_ok: |
2155 | switch (c) |
2153 | if (ecb_expect_false (c == delim)) |
2156 | { |
|
|
2157 | case '\\': |
|
|
2158 | state = st_bsl; |
|
|
2159 | break; |
|
|
2160 | |
|
|
2161 | case '"': |
|
|
2162 | *p = 0; |
|
|
2163 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2154 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2164 | |
2155 | |
2165 | default: |
2156 | if (ecb_expect_false (c == '\\')) |
|
|
2157 | state = st_bsl; |
|
|
2158 | else |
2166 | *p++ = c; |
2159 | *p++ = c; |
2167 | break; |
|
|
2168 | } |
|
|
2169 | |
2160 | |
2170 | break; |
2161 | break; |
2171 | |
2162 | |
2172 | case st_bsl: |
2163 | case st_bsl: |
2173 | switch (c) |
2164 | switch (c) |
… | |
… | |
2203 | case 'r': |
2194 | case 'r': |
2204 | *p++ = '\r'; |
2195 | *p++ = '\r'; |
2205 | state = st_ok; |
2196 | state = st_ok; |
2206 | break; |
2197 | break; |
2207 | |
2198 | |
2208 | case '"': |
|
|
2209 | *p++ = '"'; |
|
|
2210 | state = st_ok; |
|
|
2211 | break; |
|
|
2212 | |
|
|
2213 | default: |
2199 | default: |
2214 | *p++ = c; |
2200 | *p++ = c; |
2215 | state = st_ok; |
2201 | state = st_ok; |
2216 | break; |
2202 | break; |
2217 | } |
2203 | } |
2218 | |
2204 | |
2219 | break; |
2205 | break; |
2220 | |
2206 | |
2221 | case st_x1: |
2207 | case st_x1: |
2222 | case st_x2: |
2208 | case st_x2: |
2223 | c = toupper (c); |
2209 | c = tolower (c); |
2224 | |
2210 | |
2225 | if (c >= '0' && c <= 'F') |
2211 | if (c >= '0' && c <= '9') |
2226 | { |
|
|
2227 | if (c <= '9') |
|
|
2228 | c1 = (c1 << 4) + c - '0'; |
2212 | c1 = (c1 << 4) + c - '0'; |
2229 | else |
2213 | else if (c >= 'a' && c <= 'f') |
2230 | c1 = (c1 << 4) + c - 'A' + 10; |
2214 | c1 = (c1 << 4) + c - 'a' + 10; |
2231 | |
|
|
2232 | if (state == st_x1) |
|
|
2233 | state = st_x2; |
|
|
2234 | else |
|
|
2235 | { |
|
|
2236 | *p++ = c1; |
|
|
2237 | state = st_ok; |
|
|
2238 | } |
|
|
2239 | } |
|
|
2240 | else |
2215 | else |
2241 | return S_F; |
2216 | return S_F; |
|
|
2217 | |
|
|
2218 | if (state == st_x1) |
|
|
2219 | state = st_x2; |
|
|
2220 | else |
|
|
2221 | { |
|
|
2222 | *p++ = c1; |
|
|
2223 | state = st_ok; |
|
|
2224 | } |
2242 | |
2225 | |
2243 | break; |
2226 | break; |
2244 | |
2227 | |
2245 | case st_oct1: |
2228 | case st_oct1: |
2246 | case st_oct2: |
2229 | case st_oct2: |
… | |
… | |
2250 | backchar (SCHEME_A_ c); |
2233 | backchar (SCHEME_A_ c); |
2251 | state = st_ok; |
2234 | state = st_ok; |
2252 | } |
2235 | } |
2253 | else |
2236 | else |
2254 | { |
2237 | { |
2255 | if (state == st_oct2 && c1 >= 32) |
2238 | if (state == st_oct2 && c1 >= ' ') |
2256 | return S_F; |
2239 | return S_F; |
2257 | |
2240 | |
2258 | c1 = (c1 << 3) + (c - '0'); |
2241 | c1 = (c1 << 3) + (c - '0'); |
2259 | |
2242 | |
2260 | if (state == st_oct1) |
2243 | if (state == st_oct1) |
… | |
… | |
2265 | state = st_ok; |
2248 | state = st_ok; |
2266 | } |
2249 | } |
2267 | } |
2250 | } |
2268 | |
2251 | |
2269 | break; |
2252 | break; |
2270 | |
|
|
2271 | } |
2253 | } |
2272 | } |
2254 | } |
2273 | } |
2255 | } |
2274 | |
2256 | |
2275 | /* check c is in chars */ |
2257 | /* check c is in chars */ |
2276 | ecb_inline int |
2258 | ecb_inline int |
2277 | is_one_of (char *s, int c) |
2259 | is_one_of (const char *s, int c) |
2278 | { |
2260 | { |
2279 | if (c == EOF) |
2261 | if (c == EOF) |
2280 | return 1; |
2262 | return 1; |
2281 | |
2263 | |
2282 | return !!strchr (s, c); |
2264 | return !!strchr (s, c); |
… | |
… | |
2338 | |
2320 | |
2339 | if (is_one_of (" \n\t", c)) |
2321 | if (is_one_of (" \n\t", c)) |
2340 | return TOK_DOT; |
2322 | return TOK_DOT; |
2341 | else |
2323 | else |
2342 | { |
2324 | { |
2343 | //TODO: ungetc twice in a row is not supported in C |
|
|
2344 | backchar (SCHEME_A_ c); |
2325 | backchar (SCHEME_A_ c); |
2345 | backchar (SCHEME_A_ '.'); |
|
|
2346 | return TOK_ATOM; |
2326 | return TOK_DOTATOM; |
2347 | } |
2327 | } |
|
|
2328 | |
|
|
2329 | case '|': |
|
|
2330 | return TOK_STRATOM; |
2348 | |
2331 | |
2349 | case '\'': |
2332 | case '\'': |
2350 | return TOK_QUOTE; |
2333 | return TOK_QUOTE; |
2351 | |
2334 | |
2352 | case ';': |
2335 | case ';': |
… | |
… | |
4959 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
4942 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
4960 | SCHEME_V->tok = token (SCHEME_A); |
4943 | SCHEME_V->tok = token (SCHEME_A); |
4961 | s_goto (OP_RDSEXPR); |
4944 | s_goto (OP_RDSEXPR); |
4962 | |
4945 | |
4963 | case TOK_ATOM: |
4946 | case TOK_ATOM: |
4964 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); |
4947 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); |
|
|
4948 | |
|
|
4949 | case TOK_DOTATOM: |
|
|
4950 | SCHEME_V->strbuff[0] = '.'; |
|
|
4951 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
4965 | |
4952 | |
4966 | case TOK_DQUOTE: |
4953 | case TOK_DQUOTE: |
4967 | x = readstrexp (SCHEME_A); |
4954 | x = readstrexp (SCHEME_A_ '"'); |
4968 | |
4955 | |
4969 | if (x == S_F) |
4956 | if (x == S_F) |
4970 | Error_0 ("Error reading string"); |
4957 | Error_0 ("Error reading string"); |
4971 | |
4958 | |
4972 | setimmutable (x); |
4959 | setimmutable (x); |
… | |
… | |
4984 | s_goto (OP_EVAL); |
4971 | s_goto (OP_EVAL); |
4985 | } |
4972 | } |
4986 | } |
4973 | } |
4987 | |
4974 | |
4988 | case TOK_SHARP_CONST: |
4975 | case TOK_SHARP_CONST: |
4989 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) |
4976 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) |
4990 | Error_0 ("undefined sharp expression"); |
4977 | Error_0 ("undefined sharp expression"); |
4991 | else |
4978 | else |
4992 | s_return (x); |
4979 | s_return (x); |
4993 | |
4980 | |
4994 | default: |
4981 | default: |