… | |
… | |
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" |
… | |
… | |
74 | TOK_SHARP_CONST, |
76 | TOK_SHARP_CONST, |
75 | TOK_VEC |
77 | TOK_VEC |
76 | }; |
78 | }; |
77 | |
79 | |
78 | #define BACKQUOTE '`' |
80 | #define BACKQUOTE '`' |
79 | #define DELIMITERS "()\";\f\t\v\n\r " |
81 | #define WHITESPACE " \t\r\n\v\f" |
|
|
82 | #define DELIMITERS "()\";" WHITESPACE |
80 | |
83 | |
81 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
84 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
82 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
85 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
83 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
86 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
84 | #define S_SINK (&SCHEME_V->xsink) |
87 | #define S_SINK (&SCHEME_V->xsink) |
… | |
… | |
406 | } |
409 | } |
407 | |
410 | |
408 | INTERFACE char * |
411 | INTERFACE char * |
409 | symname (pointer p) |
412 | symname (pointer p) |
410 | { |
413 | { |
411 | return strvalue (car (p)); |
414 | return strvalue (p); |
412 | } |
415 | } |
413 | |
416 | |
414 | #if USE_PLIST |
417 | #if USE_PLIST |
|
|
418 | #error plists are broken because symbols are no longer pairs |
|
|
419 | #define symprop(p) cdr(p) |
415 | SCHEME_EXPORT int |
420 | SCHEME_EXPORT int |
416 | hasprop (pointer p) |
421 | hasprop (pointer p) |
417 | { |
422 | { |
418 | return typeflag (p) & T_SYMBOL; |
423 | return typeflag (p) & T_SYMBOL; |
419 | } |
424 | } |
420 | |
|
|
421 | # define symprop(p) cdr(p) |
|
|
422 | #endif |
425 | #endif |
423 | |
426 | |
424 | INTERFACE int |
427 | INTERFACE int |
425 | is_syntax (pointer p) |
428 | is_syntax (pointer p) |
426 | { |
429 | { |
… | |
… | |
440 | } |
443 | } |
441 | |
444 | |
442 | INTERFACE char * |
445 | INTERFACE char * |
443 | syntaxname (pointer p) |
446 | syntaxname (pointer p) |
444 | { |
447 | { |
445 | return strvalue (car (p)); |
448 | return strvalue (p); |
446 | } |
449 | } |
447 | |
450 | |
448 | #define procnum(p) ivalue_unchecked (p) |
451 | #define procnum(p) ivalue_unchecked (p) |
449 | static const char *procname (pointer x); |
452 | static const char *procname (pointer x); |
450 | |
453 | |
… | |
… | |
967 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
970 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
968 | return S_SINK; |
971 | return S_SINK; |
969 | |
972 | |
970 | if (SCHEME_V->free_cell == NIL) |
973 | if (SCHEME_V->free_cell == NIL) |
971 | { |
974 | { |
972 | 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; |
973 | |
976 | |
974 | gc (SCHEME_A_ a, b); |
977 | gc (SCHEME_A_ a, b); |
975 | |
978 | |
976 | 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) |
977 | { |
980 | { |
… | |
… | |
1028 | } |
1031 | } |
1029 | |
1032 | |
1030 | static pointer |
1033 | static pointer |
1031 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1034 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1032 | { |
1035 | { |
1033 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
1036 | pointer v = get_cell_x (SCHEME_A_ NIL, NIL); |
1034 | pointer *e = malloc (len * sizeof (pointer)); |
1037 | pointer *e = malloc (len * sizeof (pointer)); |
1035 | |
1038 | |
1036 | if (!e && USE_ERROR_CHECKING) |
1039 | if (!e && USE_ERROR_CHECKING) |
1037 | return S_SINK; |
1040 | return S_SINK; |
1038 | |
1041 | |
… | |
… | |
1092 | set_cdr (x, b); |
1095 | set_cdr (x, b); |
1093 | |
1096 | |
1094 | return x; |
1097 | return x; |
1095 | } |
1098 | } |
1096 | |
1099 | |
1097 | /* ========== oblist implementation ========== */ |
|
|
1098 | |
|
|
1099 | static pointer |
1100 | static pointer |
1100 | generate_symbol (SCHEME_P_ const char *name) |
1101 | generate_symbol (SCHEME_P_ const char *name) |
1101 | { |
1102 | { |
1102 | pointer x = mk_string (SCHEME_A_ name); |
1103 | pointer x = mk_string (SCHEME_A_ name); |
1103 | setimmutable (x); |
1104 | setimmutable (x); |
1104 | x = immutable_cons (x, NIL); |
|
|
1105 | set_typeflag (x, T_SYMBOL); |
1105 | set_typeflag (x, T_SYMBOL | T_ATOM); |
1106 | return x; |
1106 | return x; |
1107 | } |
1107 | } |
|
|
1108 | |
|
|
1109 | /* ========== oblist implementation ========== */ |
1108 | |
1110 | |
1109 | #ifndef USE_OBJECT_LIST |
1111 | #ifndef USE_OBJECT_LIST |
1110 | |
1112 | |
1111 | static int |
1113 | static int |
1112 | hash_fn (const char *key, int table_size) |
1114 | hash_fn (const char *key, int table_size) |
… | |
… | |
1199 | |
1201 | |
1200 | /* returns the new symbol */ |
1202 | /* returns the new symbol */ |
1201 | static pointer |
1203 | static pointer |
1202 | oblist_add_by_name (SCHEME_P_ const char *name) |
1204 | oblist_add_by_name (SCHEME_P_ const char *name) |
1203 | { |
1205 | { |
1204 | pointer x = mk_string (SCHEME_A_ name); |
1206 | pointer x = generate_symbol (SCHEME_A_ name); |
1205 | set_typeflag (x, T_SYMBOL); |
|
|
1206 | setimmutable (x); |
|
|
1207 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1207 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1208 | return x; |
1208 | return x; |
1209 | } |
1209 | } |
1210 | |
1210 | |
1211 | static pointer |
1211 | static pointer |
… | |
… | |
1232 | pointer |
1232 | pointer |
1233 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1233 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1234 | { |
1234 | { |
1235 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1235 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1236 | |
1236 | |
1237 | set_typeflag (x, (T_FOREIGN | T_ATOM)); |
1237 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1238 | x->object.ff = f; |
1238 | x->object.ff = f; |
1239 | |
1239 | |
1240 | return x; |
1240 | return x; |
1241 | } |
1241 | } |
1242 | |
1242 | |
1243 | INTERFACE pointer |
1243 | INTERFACE pointer |
1244 | mk_character (SCHEME_P_ int c) |
1244 | mk_character (SCHEME_P_ int c) |
1245 | { |
1245 | { |
1246 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1246 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1247 | |
1247 | |
1248 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1248 | set_typeflag (x, T_CHARACTER | T_ATOM); |
1249 | set_ivalue (x, c & 0xff); |
1249 | set_ivalue (x, c & 0xff); |
1250 | |
1250 | |
1251 | return x; |
1251 | return x; |
1252 | } |
1252 | } |
1253 | |
1253 | |
1254 | /* get number atom (integer) */ |
1254 | /* get number atom (integer) */ |
1255 | INTERFACE pointer |
1255 | INTERFACE pointer |
1256 | mk_integer (SCHEME_P_ long n) |
1256 | mk_integer (SCHEME_P_ long n) |
1257 | { |
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 | { |
1258 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1268 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1259 | |
1269 | |
1260 | 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 */ |
1261 | set_ivalue (x, n); |
1272 | set_ivalue (x, n); |
1262 | |
1273 | |
|
|
1274 | *pp = x; |
|
|
1275 | } |
|
|
1276 | |
1263 | return x; |
1277 | return *pp; |
1264 | } |
1278 | } |
1265 | |
1279 | |
1266 | INTERFACE pointer |
1280 | INTERFACE pointer |
1267 | mk_real (SCHEME_P_ RVALUE n) |
1281 | mk_real (SCHEME_P_ RVALUE n) |
1268 | { |
1282 | { |
1269 | #if USE_REAL |
1283 | #if USE_REAL |
1270 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1284 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1271 | |
1285 | |
1272 | set_typeflag (x, (T_REAL | T_ATOM)); |
1286 | set_typeflag (x, T_REAL | T_ATOM); |
1273 | set_rvalue (x, n); |
1287 | set_rvalue (x, n); |
1274 | |
1288 | |
1275 | return x; |
1289 | return x; |
1276 | #else |
1290 | #else |
1277 | return mk_integer (SCHEME_A_ n); |
1291 | return mk_integer (SCHEME_A_ n); |
… | |
… | |
1393 | INTERFACE pointer |
1407 | INTERFACE pointer |
1394 | gensym (SCHEME_P) |
1408 | gensym (SCHEME_P) |
1395 | { |
1409 | { |
1396 | pointer x; |
1410 | pointer x; |
1397 | char name[40] = "gensym-"; |
1411 | char name[40] = "gensym-"; |
1398 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1412 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1399 | |
1413 | |
1400 | return generate_symbol (SCHEME_A_ name); |
1414 | return generate_symbol (SCHEME_A_ name); |
|
|
1415 | } |
|
|
1416 | |
|
|
1417 | static int |
|
|
1418 | is_gensym (SCHEME_P_ pointer x) |
|
|
1419 | { |
|
|
1420 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1401 | } |
1421 | } |
1402 | |
1422 | |
1403 | /* make symbol or number atom from string */ |
1423 | /* make symbol or number atom from string */ |
1404 | static pointer |
1424 | static pointer |
1405 | mk_atom (SCHEME_P_ char *q) |
1425 | mk_atom (SCHEME_P_ char *q) |
… | |
… | |
1640 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1660 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1641 | mark (car (S_SINK)); |
1661 | mark (car (S_SINK)); |
1642 | /* Mark any older stuff above nested C calls */ |
1662 | /* Mark any older stuff above nested C calls */ |
1643 | mark (SCHEME_V->c_nest); |
1663 | mark (SCHEME_V->c_nest); |
1644 | |
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 | |
1645 | /* mark variables a, b */ |
1672 | /* mark variables a, b */ |
1646 | mark (a); |
1673 | mark (a); |
1647 | mark (b); |
1674 | mark (b); |
1648 | |
1675 | |
1649 | /* garbage collect */ |
1676 | /* garbage collect */ |
1650 | clrmark (NIL); |
1677 | clrmark (NIL); |
1651 | SCHEME_V->fcells = 0; |
1678 | SCHEME_V->fcells = 0; |
1652 | SCHEME_V->free_cell = NIL; |
1679 | SCHEME_V->free_cell = NIL; |
1653 | |
1680 | |
1654 | /* free-list is kept sorted by address so as to maintain consecutive |
1681 | if (SCHEME_V->gc_verbose) |
1655 | ranges, if possible, for use with vectors. Here we scan the cells |
1682 | xwrstr ("freeing..."); |
1656 | (which are also kept sorted by address) downwards to build the |
1683 | |
1657 | free-list in sorted order. |
1684 | uint32_t total = 0; |
1658 | */ |
1685 | |
|
|
1686 | /* Here we scan the cells to build the free-list. */ |
1659 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1687 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1660 | { |
1688 | { |
1661 | 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]; |
1662 | |
1691 | |
1663 | while (--p >= SCHEME_V->cell_seg[i]) |
1692 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
1664 | { |
1693 | { |
1665 | if (is_mark (p)) |
1694 | if (is_mark (p)) |
1666 | clrmark (p); |
1695 | clrmark (p); |
1667 | else |
1696 | else |
1668 | { |
1697 | { |
… | |
… | |
1681 | } |
1710 | } |
1682 | } |
1711 | } |
1683 | |
1712 | |
1684 | if (SCHEME_V->gc_verbose) |
1713 | if (SCHEME_V->gc_verbose) |
1685 | { |
1714 | { |
1686 | 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"); |
1687 | } |
1716 | } |
1688 | } |
1717 | } |
1689 | |
1718 | |
1690 | static void |
1719 | static void |
1691 | finalize_cell (SCHEME_P_ pointer a) |
1720 | finalize_cell (SCHEME_P_ pointer a) |
1692 | { |
1721 | { |
1693 | /* TODO, fast bitmap check? */ |
1722 | /* TODO, fast bitmap check? */ |
1694 | if (is_string (a)) |
1723 | if (is_string (a) || is_symbol (a)) |
1695 | free (strvalue (a)); |
1724 | free (strvalue (a)); |
1696 | else if (is_vector (a)) |
1725 | else if (is_vector (a)) |
1697 | free (vecvalue (a)); |
1726 | free (vecvalue (a)); |
1698 | #if USE_PORTS |
1727 | #if USE_PORTS |
1699 | else if (is_port (a)) |
1728 | else if (is_port (a)) |
… | |
… | |
2265 | |
2294 | |
2266 | /* check c is in chars */ |
2295 | /* check c is in chars */ |
2267 | ecb_inline int |
2296 | ecb_inline int |
2268 | is_one_of (const char *s, int c) |
2297 | is_one_of (const char *s, int c) |
2269 | { |
2298 | { |
2270 | if (c == EOF) |
|
|
2271 | return 1; |
|
|
2272 | |
|
|
2273 | return !!strchr (s, c); |
2299 | return c == EOF || !!strchr (s, c); |
2274 | } |
2300 | } |
2275 | |
2301 | |
2276 | /* skip white characters */ |
2302 | /* skip white characters */ |
2277 | ecb_inline int |
2303 | ecb_inline int |
2278 | skipspace (SCHEME_P) |
2304 | skipspace (SCHEME_P) |
… | |
… | |
2280 | int c, curr_line = 0; |
2306 | int c, curr_line = 0; |
2281 | |
2307 | |
2282 | do |
2308 | do |
2283 | { |
2309 | { |
2284 | c = inchar (SCHEME_A); |
2310 | c = inchar (SCHEME_A); |
|
|
2311 | |
2285 | #if SHOW_ERROR_LINE |
2312 | #if SHOW_ERROR_LINE |
2286 | if (c == '\n') |
2313 | if (ecb_expect_false (c == '\n')) |
2287 | curr_line++; |
2314 | curr_line++; |
2288 | #endif |
2315 | #endif |
|
|
2316 | |
|
|
2317 | if (ecb_expect_false (c == EOF)) |
|
|
2318 | return c; |
2289 | } |
2319 | } |
2290 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2320 | while (is_one_of (WHITESPACE, c)); |
2291 | |
2321 | |
2292 | /* record it */ |
2322 | /* record it */ |
2293 | #if SHOW_ERROR_LINE |
2323 | #if SHOW_ERROR_LINE |
2294 | 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) |
2295 | 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; |
2296 | #endif |
2326 | #endif |
2297 | |
2327 | |
2298 | if (c != EOF) |
|
|
2299 | { |
|
|
2300 | backchar (SCHEME_A_ c); |
2328 | backchar (SCHEME_A_ c); |
2301 | return 1; |
2329 | return 1; |
2302 | } |
|
|
2303 | else |
|
|
2304 | return EOF; |
|
|
2305 | } |
2330 | } |
2306 | |
2331 | |
2307 | /* get token */ |
2332 | /* get token */ |
2308 | static int |
2333 | static int |
2309 | token (SCHEME_P) |
2334 | token (SCHEME_P) |
… | |
… | |
2325 | return TOK_RPAREN; |
2350 | return TOK_RPAREN; |
2326 | |
2351 | |
2327 | case '.': |
2352 | case '.': |
2328 | c = inchar (SCHEME_A); |
2353 | c = inchar (SCHEME_A); |
2329 | |
2354 | |
2330 | if (is_one_of (" \n\t", c)) |
2355 | if (is_one_of (WHITESPACE, c)) |
2331 | return TOK_DOT; |
2356 | return TOK_DOT; |
2332 | else |
2357 | else |
2333 | { |
2358 | { |
2334 | backchar (SCHEME_A_ c); |
2359 | backchar (SCHEME_A_ c); |
2335 | return TOK_DOTATOM; |
2360 | return TOK_DOTATOM; |
… | |
… | |
2476 | } |
2501 | } |
2477 | |
2502 | |
2478 | putcharacter (SCHEME_A_ '"'); |
2503 | putcharacter (SCHEME_A_ '"'); |
2479 | } |
2504 | } |
2480 | |
2505 | |
2481 | |
|
|
2482 | /* print atoms */ |
2506 | /* print atoms */ |
2483 | static void |
2507 | static void |
2484 | printatom (SCHEME_P_ pointer l, int f) |
2508 | printatom (SCHEME_P_ pointer l, int f) |
2485 | { |
2509 | { |
2486 | char *p; |
2510 | char *p; |
2487 | int len; |
2511 | int len; |
2488 | |
2512 | |
2489 | atom2str (SCHEME_A_ l, f, &p, &len); |
2513 | atom2str (SCHEME_A_ l, f, &p, &len); |
2490 | putchars (SCHEME_A_ p, len); |
2514 | putchars (SCHEME_A_ p, len); |
2491 | } |
2515 | } |
2492 | |
|
|
2493 | |
2516 | |
2494 | /* Uses internal buffer unless string pointer is already available */ |
2517 | /* Uses internal buffer unless string pointer is already available */ |
2495 | static void |
2518 | static void |
2496 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2519 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2497 | { |
2520 | { |
… | |
… | |
2658 | #endif |
2681 | #endif |
2659 | } |
2682 | } |
2660 | else if (is_continuation (l)) |
2683 | else if (is_continuation (l)) |
2661 | p = "#<CONTINUATION>"; |
2684 | p = "#<CONTINUATION>"; |
2662 | else |
2685 | else |
|
|
2686 | { |
|
|
2687 | #if USE_PRINTF |
|
|
2688 | p = SCHEME_V->strbuff; |
|
|
2689 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2690 | #else |
2663 | p = "#<ERROR>"; |
2691 | p = "#<ERROR>"; |
|
|
2692 | #endif |
|
|
2693 | } |
2664 | |
2694 | |
2665 | *pp = p; |
2695 | *pp = p; |
2666 | *plen = strlen (p); |
2696 | *plen = strlen (p); |
2667 | } |
2697 | } |
2668 | |
2698 | |
… | |
… | |
2938 | #endif /* USE_ALIST_ENV else */ |
2968 | #endif /* USE_ALIST_ENV else */ |
2939 | |
2969 | |
2940 | ecb_inline void |
2970 | ecb_inline void |
2941 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2971 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2942 | { |
2972 | { |
|
|
2973 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2943 | 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); |
2944 | } |
2975 | } |
2945 | |
2976 | |
2946 | ecb_inline void |
2977 | ecb_inline void |
2947 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2978 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
… | |
… | |
3239 | |
3270 | |
3240 | #endif |
3271 | #endif |
3241 | |
3272 | |
3242 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3273 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3243 | |
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 | |
3244 | static int |
3333 | static int |
3245 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3334 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3246 | { |
3335 | { |
3247 | pointer args = SCHEME_V->args; |
3336 | pointer args = SCHEME_V->args; |
3248 | pointer x, y; |
3337 | pointer x, y; |
3249 | |
3338 | |
3250 | switch (op) |
3339 | switch (op) |
3251 | { |
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 |
3252 | case OP_LOAD: /* load */ |
3347 | case OP_LOAD: /* load */ |
3253 | if (file_interactive (SCHEME_A)) |
3348 | if (file_interactive (SCHEME_A)) |
3254 | { |
3349 | { |
3255 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3350 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3256 | //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))); |
… | |
… | |
3379 | } |
3474 | } |
3380 | else |
3475 | else |
3381 | s_return (SCHEME_V->code); |
3476 | s_return (SCHEME_V->code); |
3382 | |
3477 | |
3383 | case OP_E0ARGS: /* eval arguments */ |
3478 | case OP_E0ARGS: /* eval arguments */ |
3384 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3479 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3385 | { |
3480 | { |
3386 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3481 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3387 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3482 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3388 | SCHEME_V->code = SCHEME_V->value; |
3483 | SCHEME_V->code = SCHEME_V->value; |
3389 | s_goto (OP_APPLY); |
3484 | s_goto (OP_APPLY); |
… | |
… | |
3490 | |
3585 | |
3491 | case OP_DOMACRO: /* do macro */ |
3586 | case OP_DOMACRO: /* do macro */ |
3492 | SCHEME_V->code = SCHEME_V->value; |
3587 | SCHEME_V->code = SCHEME_V->value; |
3493 | s_goto (OP_EVAL); |
3588 | s_goto (OP_EVAL); |
3494 | |
3589 | |
3495 | #if 1 |
|
|
3496 | |
|
|
3497 | case OP_LAMBDA: /* lambda */ |
3590 | case OP_LAMBDA: /* lambda */ |
3498 | /* 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 |
3499 | set SCHEME_V->value fall thru */ |
3592 | set SCHEME_V->value fall thru */ |
3500 | { |
3593 | { |
3501 | 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); |
… | |
… | |
3507 | SCHEME_V->code = slot_value_in_env (f); |
3600 | SCHEME_V->code = slot_value_in_env (f); |
3508 | s_goto (OP_APPLY); |
3601 | s_goto (OP_APPLY); |
3509 | } |
3602 | } |
3510 | |
3603 | |
3511 | SCHEME_V->value = SCHEME_V->code; |
3604 | SCHEME_V->value = SCHEME_V->code; |
3512 | /* Fallthru */ |
|
|
3513 | } |
3605 | } |
|
|
3606 | /* Fallthru */ |
3514 | |
3607 | |
3515 | case OP_LAMBDA1: |
3608 | case OP_LAMBDA1: |
3516 | 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)); |
3517 | |
|
|
3518 | #else |
|
|
3519 | |
|
|
3520 | case OP_LAMBDA: /* lambda */ |
|
|
3521 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
|
|
3522 | |
|
|
3523 | #endif |
|
|
3524 | |
3610 | |
3525 | case OP_MKCLOSURE: /* make-closure */ |
3611 | case OP_MKCLOSURE: /* make-closure */ |
3526 | x = car (args); |
3612 | x = car (args); |
3527 | |
3613 | |
3528 | if (car (x) == SCHEME_V->LAMBDA) |
3614 | if (car (x) == SCHEME_V->LAMBDA) |
… | |
… | |
4124 | else |
4210 | else |
4125 | Error_0 ("modulo: division by zero"); |
4211 | Error_0 ("modulo: division by zero"); |
4126 | |
4212 | |
4127 | s_return (mk_number (SCHEME_A_ v)); |
4213 | s_return (mk_number (SCHEME_A_ v)); |
4128 | |
4214 | |
4129 | case OP_CAR: /* car */ |
4215 | /* the compiler will optimize this mess... */ |
4130 | s_return (caar (args)); |
4216 | case OP_CAR: op_car: s_return (car (x)); |
4131 | |
4217 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4132 | case OP_CDR: /* cdr */ |
4218 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4133 | 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; |
4134 | |
4246 | |
4135 | case OP_CONS: /* cons */ |
4247 | case OP_CONS: /* cons */ |
4136 | set_cdr (args, cadr (args)); |
4248 | set_cdr (args, cadr (args)); |
4137 | s_return (args); |
4249 | s_return (args); |
4138 | |
4250 | |
… | |
… | |
4460 | pointer d = cdr (args); |
4572 | pointer d = cdr (args); |
4461 | int r; |
4573 | int r; |
4462 | |
4574 | |
4463 | switch (op) |
4575 | switch (op) |
4464 | { |
4576 | { |
4465 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4577 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4466 | 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; |
4467 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4579 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4468 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4580 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4469 | 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; |
4470 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4583 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4471 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4584 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4472 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4585 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4473 | 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 */ |
4474 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4587 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4475 | |
4588 | |
4476 | #if USE_CHAR_CLASSIFIERS |
4589 | #if USE_CHAR_CLASSIFIERS |
4477 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4590 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4478 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4591 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4479 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4592 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
… | |
… | |
5221 | |
5334 | |
5222 | case OP_CLOSUREP: /* closure? */ |
5335 | case OP_CLOSUREP: /* closure? */ |
5223 | /* |
5336 | /* |
5224 | * Note, macro object is also a closure. |
5337 | * Note, macro object is also a closure. |
5225 | * Therefore, (closure? <#MACRO>) ==> #t |
5338 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5339 | * (schmorp) well, obviously not, fix? TODO |
5226 | */ |
5340 | */ |
5227 | s_retbool (is_closure (a)); |
5341 | s_retbool (is_closure (a)); |
5228 | |
5342 | |
5229 | case OP_MACROP: /* macro? */ |
5343 | case OP_MACROP: /* macro? */ |
5230 | s_retbool (is_macro (a)); |
5344 | s_retbool (is_macro (a)); |
… | |
… | |
5471 | |
5585 | |
5472 | /* 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! */ |
5473 | static int |
5587 | static int |
5474 | syntaxnum (pointer p) |
5588 | syntaxnum (pointer p) |
5475 | { |
5589 | { |
5476 | const char *s = strvalue (car (p)); |
5590 | const char *s = strvalue (p); |
5477 | |
5591 | |
5478 | switch (strlength (car (p))) |
5592 | switch (strlength (p)) |
5479 | { |
5593 | { |
5480 | case 2: |
5594 | case 2: |
5481 | if (s[0] == 'i') |
5595 | if (s[0] == 'i') |
5482 | return OP_IF0; /* if */ |
5596 | return OP_IF0; /* if */ |
5483 | else |
5597 | else |
… | |
… | |
5558 | ecb_cold int |
5672 | ecb_cold int |
5559 | scheme_init (SCHEME_P) |
5673 | scheme_init (SCHEME_P) |
5560 | { |
5674 | { |
5561 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5675 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5562 | pointer x; |
5676 | pointer x; |
|
|
5677 | |
|
|
5678 | memset (SCHEME_V, 0, sizeof (*SCHEME_V));//TODO !iso c |
5563 | |
5679 | |
5564 | num_set_fixnum (num_zero, 1); |
5680 | num_set_fixnum (num_zero, 1); |
5565 | num_set_ivalue (num_zero, 0); |
5681 | num_set_ivalue (num_zero, 0); |
5566 | num_set_fixnum (num_one, 1); |
5682 | num_set_fixnum (num_one, 1); |
5567 | num_set_ivalue (num_one, 1); |
5683 | num_set_ivalue (num_one, 1); |
… | |
… | |
5919 | # endif |
6035 | # endif |
5920 | int fin; |
6036 | int fin; |
5921 | char *file_name = InitFile; |
6037 | char *file_name = InitFile; |
5922 | int retcode; |
6038 | int retcode; |
5923 | int isfile = 1; |
6039 | int isfile = 1; |
|
|
6040 | system ("ps v $PPID");//D |
5924 | |
6041 | |
5925 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6042 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5926 | { |
6043 | { |
5927 | xwrstr ("Usage: tinyscheme -?\n"); |
6044 | xwrstr ("Usage: tinyscheme -?\n"); |
5928 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6045 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |