… | |
… | |
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) |
… | |
… | |
391 | } |
409 | } |
392 | |
410 | |
393 | INTERFACE char * |
411 | INTERFACE char * |
394 | symname (pointer p) |
412 | symname (pointer p) |
395 | { |
413 | { |
396 | return strvalue (car (p)); |
414 | return strvalue (p); |
397 | } |
415 | } |
398 | |
416 | |
399 | #if USE_PLIST |
417 | #if USE_PLIST |
|
|
418 | #define symprop(p) cdr(p) |
400 | SCHEME_EXPORT int |
419 | SCHEME_EXPORT int |
401 | hasprop (pointer p) |
420 | hasprop (pointer p) |
402 | { |
421 | { |
403 | return typeflag (p) & T_SYMBOL; |
422 | return typeflag (p) & T_SYMBOL; |
404 | } |
423 | } |
405 | |
|
|
406 | # define symprop(p) cdr(p) |
|
|
407 | #endif |
424 | #endif |
408 | |
425 | |
409 | INTERFACE int |
426 | INTERFACE int |
410 | is_syntax (pointer p) |
427 | is_syntax (pointer p) |
411 | { |
428 | { |
… | |
… | |
425 | } |
442 | } |
426 | |
443 | |
427 | INTERFACE char * |
444 | INTERFACE char * |
428 | syntaxname (pointer p) |
445 | syntaxname (pointer p) |
429 | { |
446 | { |
430 | return strvalue (car (p)); |
447 | return strvalue (p); |
431 | } |
448 | } |
432 | |
449 | |
433 | #define procnum(p) ivalue_unchecked (p) |
450 | #define procnum(p) ivalue_unchecked (p) |
434 | static const char *procname (pointer x); |
451 | static const char *procname (pointer x); |
435 | |
452 | |
… | |
… | |
651 | #endif |
668 | #endif |
652 | |
669 | |
653 | static int file_push (SCHEME_P_ const char *fname); |
670 | static int file_push (SCHEME_P_ const char *fname); |
654 | static void file_pop (SCHEME_P); |
671 | static void file_pop (SCHEME_P); |
655 | static int file_interactive (SCHEME_P); |
672 | static int file_interactive (SCHEME_P); |
656 | ecb_inline int is_one_of (char *s, int c); |
673 | ecb_inline int is_one_of (const char *s, int c); |
657 | static int alloc_cellseg (SCHEME_P_ int n); |
674 | static int alloc_cellseg (SCHEME_P_ int n); |
658 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
675 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
659 | static void finalize_cell (SCHEME_P_ pointer a); |
676 | static void finalize_cell (SCHEME_P_ pointer a); |
660 | static int count_consecutive_cells (pointer x, int needed); |
677 | static int count_consecutive_cells (pointer x, int needed); |
661 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
678 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
… | |
… | |
678 | static void mark (pointer a); |
695 | static void mark (pointer a); |
679 | static void gc (SCHEME_P_ pointer a, pointer b); |
696 | static void gc (SCHEME_P_ pointer a, pointer b); |
680 | static int basic_inchar (port *pt); |
697 | static int basic_inchar (port *pt); |
681 | static int inchar (SCHEME_P); |
698 | static int inchar (SCHEME_P); |
682 | static void backchar (SCHEME_P_ int c); |
699 | static void backchar (SCHEME_P_ int c); |
683 | static char *readstr_upto (SCHEME_P_ char *delim); |
700 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
684 | static pointer readstrexp (SCHEME_P); |
701 | static pointer readstrexp (SCHEME_P_ char delim); |
685 | ecb_inline int skipspace (SCHEME_P); |
702 | ecb_inline int skipspace (SCHEME_P); |
686 | static int token (SCHEME_P); |
703 | static int token (SCHEME_P); |
687 | static void printslashstring (SCHEME_P_ char *s, int len); |
704 | static void printslashstring (SCHEME_P_ char *s, int len); |
688 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
705 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
689 | static void printatom (SCHEME_P_ pointer l, int f); |
706 | static void printatom (SCHEME_P_ pointer l, int f); |
… | |
… | |
952 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
969 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
953 | return S_SINK; |
970 | return S_SINK; |
954 | |
971 | |
955 | if (SCHEME_V->free_cell == NIL) |
972 | if (SCHEME_V->free_cell == NIL) |
956 | { |
973 | { |
957 | const int min_to_be_recovered = SCHEME_V->last_cell_seg < 128 ? 128 * 8 : SCHEME_V->last_cell_seg * 8; |
974 | const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1; |
958 | |
975 | |
959 | gc (SCHEME_A_ a, b); |
976 | gc (SCHEME_A_ a, b); |
960 | |
977 | |
961 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
978 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
962 | { |
979 | { |
… | |
… | |
1013 | } |
1030 | } |
1014 | |
1031 | |
1015 | static pointer |
1032 | static pointer |
1016 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1033 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1017 | { |
1034 | { |
1018 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
1035 | pointer v = get_cell_x (SCHEME_A_ NIL, NIL); |
1019 | pointer *e = malloc (len * sizeof (pointer)); |
1036 | pointer *e = malloc (len * sizeof (pointer)); |
1020 | |
1037 | |
1021 | if (!e && USE_ERROR_CHECKING) |
1038 | if (!e && USE_ERROR_CHECKING) |
1022 | return S_SINK; |
1039 | return S_SINK; |
1023 | |
1040 | |
… | |
… | |
1077 | set_cdr (x, b); |
1094 | set_cdr (x, b); |
1078 | |
1095 | |
1079 | return x; |
1096 | return x; |
1080 | } |
1097 | } |
1081 | |
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 | set_typeflag (x, T_SYMBOL | T_ATOM); |
|
|
1105 | return x; |
|
|
1106 | } |
|
|
1107 | |
1082 | /* ========== oblist implementation ========== */ |
1108 | /* ========== oblist implementation ========== */ |
1083 | |
1109 | |
1084 | #ifndef USE_OBJECT_LIST |
1110 | #ifndef USE_OBJECT_LIST |
1085 | |
1111 | |
|
|
1112 | static int |
1086 | static int hash_fn (const char *key, int table_size); |
1113 | hash_fn (const char *key, int table_size) |
|
|
1114 | { |
|
|
1115 | const unsigned char *p = key; |
|
|
1116 | uint32_t hash = 2166136261; |
|
|
1117 | |
|
|
1118 | while (*p) |
|
|
1119 | hash = (hash ^ *p++) * 16777619; |
|
|
1120 | |
|
|
1121 | return hash % table_size; |
|
|
1122 | } |
1087 | |
1123 | |
1088 | static pointer |
1124 | static pointer |
1089 | oblist_initial_value (SCHEME_P) |
1125 | oblist_initial_value (SCHEME_P) |
1090 | { |
1126 | { |
1091 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1127 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
… | |
… | |
1093 | |
1129 | |
1094 | /* returns the new symbol */ |
1130 | /* returns the new symbol */ |
1095 | static pointer |
1131 | static pointer |
1096 | oblist_add_by_name (SCHEME_P_ const char *name) |
1132 | oblist_add_by_name (SCHEME_P_ const char *name) |
1097 | { |
1133 | { |
1098 | int location; |
1134 | pointer x = generate_symbol (SCHEME_A_ name); |
1099 | |
|
|
1100 | pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1101 | set_typeflag (x, T_SYMBOL); |
|
|
1102 | setimmutable (car (x)); |
|
|
1103 | |
|
|
1104 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1135 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1105 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1136 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1106 | return x; |
1137 | return x; |
1107 | } |
1138 | } |
1108 | |
1139 | |
1109 | ecb_inline pointer |
1140 | ecb_inline pointer |
… | |
… | |
1169 | |
1200 | |
1170 | /* returns the new symbol */ |
1201 | /* returns the new symbol */ |
1171 | static pointer |
1202 | static pointer |
1172 | oblist_add_by_name (SCHEME_P_ const char *name) |
1203 | oblist_add_by_name (SCHEME_P_ const char *name) |
1173 | { |
1204 | { |
1174 | pointer x; |
1205 | pointer x = mk_string (SCHEME_A_ name); |
1175 | |
|
|
1176 | x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1177 | set_typeflag (x, T_SYMBOL); |
1206 | set_typeflag (x, T_SYMBOL); |
1178 | setimmutable (car (x)); |
1207 | setimmutable (x); |
1179 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1208 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1180 | return x; |
1209 | return x; |
1181 | } |
1210 | } |
1182 | |
1211 | |
1183 | static pointer |
1212 | static pointer |
… | |
… | |
1273 | SCHEME_V->no_memory = 1; |
1302 | SCHEME_V->no_memory = 1; |
1274 | return SCHEME_V->strbuff; |
1303 | return SCHEME_V->strbuff; |
1275 | } |
1304 | } |
1276 | |
1305 | |
1277 | if (str) |
1306 | if (str) |
1278 | { |
1307 | memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */ |
1279 | int l = strlen (str); |
|
|
1280 | |
|
|
1281 | if (l > len_str) |
|
|
1282 | l = len_str; |
|
|
1283 | |
|
|
1284 | memcpy (q, str, l); |
|
|
1285 | q[l] = 0; |
|
|
1286 | } |
|
|
1287 | else |
1308 | else |
1288 | { |
|
|
1289 | memset (q, fill, len_str); |
1309 | memset (q, fill, len_str); |
|
|
1310 | |
1290 | q[len_str] = 0; |
1311 | q[len_str] = 0; |
1291 | } |
|
|
1292 | |
1312 | |
1293 | return q; |
1313 | return q; |
1294 | } |
1314 | } |
1295 | |
1315 | |
1296 | INTERFACE pointer |
1316 | INTERFACE pointer |
… | |
… | |
1310 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1330 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1311 | |
1331 | |
1312 | set_typeflag (x, T_STRING | T_ATOM); |
1332 | set_typeflag (x, T_STRING | T_ATOM); |
1313 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1333 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1314 | strlength (x) = len; |
1334 | strlength (x) = len; |
|
|
1335 | |
1315 | return x; |
1336 | return x; |
1316 | } |
1337 | } |
1317 | |
1338 | |
1318 | INTERFACE pointer |
1339 | INTERFACE pointer |
1319 | mk_string (SCHEME_P_ const char *str) |
1340 | mk_string (SCHEME_P_ const char *str) |
… | |
… | |
1332 | { |
1353 | { |
1333 | int i; |
1354 | int i; |
1334 | |
1355 | |
1335 | for (i = start; i < veclength (vec); i++) |
1356 | for (i = start; i < veclength (vec); i++) |
1336 | vecvalue (vec)[i] = obj; |
1357 | vecvalue (vec)[i] = obj; |
|
|
1358 | } |
|
|
1359 | |
|
|
1360 | INTERFACE void |
|
|
1361 | vector_resize (pointer vec, uint32_t newsize, pointer fill) |
|
|
1362 | { |
|
|
1363 | uint32_t oldsize = veclength (vec); |
|
|
1364 | vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer)); |
|
|
1365 | veclength (vec) = newsize; |
|
|
1366 | fill_vector (vec, oldsize, fill); |
1337 | } |
1367 | } |
1338 | |
1368 | |
1339 | INTERFACE pointer |
1369 | INTERFACE pointer |
1340 | vector_get (pointer vec, uint32_t ielem) |
1370 | vector_get (pointer vec, uint32_t ielem) |
1341 | { |
1371 | { |
… | |
… | |
1363 | |
1393 | |
1364 | INTERFACE pointer |
1394 | INTERFACE pointer |
1365 | gensym (SCHEME_P) |
1395 | gensym (SCHEME_P) |
1366 | { |
1396 | { |
1367 | pointer x; |
1397 | pointer x; |
1368 | |
|
|
1369 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
|
|
1370 | { |
|
|
1371 | char name[40] = "gensym-"; |
1398 | char name[40] = "gensym-"; |
1372 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1399 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1373 | |
1400 | |
1374 | /* first check oblist */ |
1401 | return generate_symbol (SCHEME_A_ name); |
1375 | x = oblist_find_by_name (SCHEME_A_ name); |
1402 | } |
1376 | |
1403 | |
1377 | if (x == NIL) |
1404 | static int |
1378 | { |
1405 | is_gensym (SCHEME_P_ pointer x) |
1379 | x = oblist_add_by_name (SCHEME_A_ name); |
1406 | { |
1380 | return x; |
1407 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1381 | } |
|
|
1382 | } |
|
|
1383 | |
|
|
1384 | return NIL; |
|
|
1385 | } |
1408 | } |
1386 | |
1409 | |
1387 | /* make symbol or number atom from string */ |
1410 | /* make symbol or number atom from string */ |
1388 | static pointer |
1411 | static pointer |
1389 | mk_atom (SCHEME_P_ char *q) |
1412 | mk_atom (SCHEME_P_ char *q) |
… | |
… | |
1633 | /* garbage collect */ |
1656 | /* garbage collect */ |
1634 | clrmark (NIL); |
1657 | clrmark (NIL); |
1635 | SCHEME_V->fcells = 0; |
1658 | SCHEME_V->fcells = 0; |
1636 | SCHEME_V->free_cell = NIL; |
1659 | SCHEME_V->free_cell = NIL; |
1637 | |
1660 | |
1638 | /* free-list is kept sorted by address so as to maintain consecutive |
1661 | if (SCHEME_V->gc_verbose) |
1639 | ranges, if possible, for use with vectors. Here we scan the cells |
1662 | xwrstr ("freeing..."); |
1640 | (which are also kept sorted by address) downwards to build the |
1663 | |
1641 | free-list in sorted order. |
1664 | uint32_t total = 0; |
1642 | */ |
1665 | |
|
|
1666 | /* Here we scan the cells to build the free-list. */ |
1643 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1667 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1644 | { |
1668 | { |
1645 | p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
1669 | pointer end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1670 | total += SCHEME_V->cell_segsize [i]; |
1646 | |
1671 | |
1647 | while (--p >= SCHEME_V->cell_seg[i]) |
1672 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
1648 | { |
1673 | { |
1649 | if (is_mark (p)) |
1674 | if (is_mark (p)) |
1650 | clrmark (p); |
1675 | clrmark (p); |
1651 | else |
1676 | else |
1652 | { |
1677 | { |
… | |
… | |
1665 | } |
1690 | } |
1666 | } |
1691 | } |
1667 | |
1692 | |
1668 | if (SCHEME_V->gc_verbose) |
1693 | if (SCHEME_V->gc_verbose) |
1669 | { |
1694 | { |
1670 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); |
1695 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" out of "); xwrnum (total); xwrstr (" cells were recovered.\n"); |
1671 | } |
1696 | } |
1672 | } |
1697 | } |
1673 | |
1698 | |
1674 | static void |
1699 | static void |
1675 | finalize_cell (SCHEME_P_ pointer a) |
1700 | finalize_cell (SCHEME_P_ pointer a) |
1676 | { |
1701 | { |
1677 | /* TODO, fast bitmap check? */ |
1702 | /* TODO, fast bitmap check? */ |
1678 | if (is_string (a)) |
1703 | if (is_string (a) || is_symbol (a)) |
1679 | free (strvalue (a)); |
1704 | free (strvalue (a)); |
1680 | else if (is_vector (a)) |
1705 | else if (is_vector (a)) |
1681 | free (vecvalue (a)); |
1706 | free (vecvalue (a)); |
1682 | #if USE_PORTS |
1707 | #if USE_PORTS |
1683 | else if (is_port (a)) |
1708 | else if (is_port (a)) |
… | |
… | |
2105 | #endif |
2130 | #endif |
2106 | } |
2131 | } |
2107 | |
2132 | |
2108 | /* read characters up to delimiter, but cater to character constants */ |
2133 | /* read characters up to delimiter, but cater to character constants */ |
2109 | static char * |
2134 | static char * |
2110 | readstr_upto (SCHEME_P_ char *delim) |
2135 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2111 | { |
2136 | { |
2112 | char *p = SCHEME_V->strbuff; |
2137 | char *p = SCHEME_V->strbuff + skip; |
2113 | |
2138 | |
2114 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2139 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2115 | |
2140 | |
2116 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2141 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2117 | *p = 0; |
2142 | *p = 0; |
… | |
… | |
2124 | return SCHEME_V->strbuff; |
2149 | return SCHEME_V->strbuff; |
2125 | } |
2150 | } |
2126 | |
2151 | |
2127 | /* read string expression "xxx...xxx" */ |
2152 | /* read string expression "xxx...xxx" */ |
2128 | static pointer |
2153 | static pointer |
2129 | readstrexp (SCHEME_P) |
2154 | readstrexp (SCHEME_P_ char delim) |
2130 | { |
2155 | { |
2131 | char *p = SCHEME_V->strbuff; |
2156 | char *p = SCHEME_V->strbuff; |
2132 | int c; |
2157 | int c; |
2133 | int c1 = 0; |
2158 | int c1 = 0; |
2134 | enum |
|
|
2135 | { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2159 | enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2136 | |
2160 | |
2137 | for (;;) |
2161 | for (;;) |
2138 | { |
2162 | { |
2139 | c = inchar (SCHEME_A); |
2163 | c = inchar (SCHEME_A); |
2140 | |
2164 | |
… | |
… | |
2142 | return S_F; |
2166 | return S_F; |
2143 | |
2167 | |
2144 | switch (state) |
2168 | switch (state) |
2145 | { |
2169 | { |
2146 | case st_ok: |
2170 | case st_ok: |
2147 | switch (c) |
2171 | if (ecb_expect_false (c == delim)) |
2148 | { |
|
|
2149 | case '\\': |
|
|
2150 | state = st_bsl; |
|
|
2151 | break; |
|
|
2152 | |
|
|
2153 | case '"': |
|
|
2154 | *p = 0; |
|
|
2155 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2172 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2156 | |
2173 | |
2157 | default: |
2174 | if (ecb_expect_false (c == '\\')) |
|
|
2175 | state = st_bsl; |
|
|
2176 | else |
2158 | *p++ = c; |
2177 | *p++ = c; |
2159 | break; |
|
|
2160 | } |
|
|
2161 | |
2178 | |
2162 | break; |
2179 | break; |
2163 | |
2180 | |
2164 | case st_bsl: |
2181 | case st_bsl: |
2165 | switch (c) |
2182 | switch (c) |
… | |
… | |
2195 | case 'r': |
2212 | case 'r': |
2196 | *p++ = '\r'; |
2213 | *p++ = '\r'; |
2197 | state = st_ok; |
2214 | state = st_ok; |
2198 | break; |
2215 | break; |
2199 | |
2216 | |
2200 | case '"': |
|
|
2201 | *p++ = '"'; |
|
|
2202 | state = st_ok; |
|
|
2203 | break; |
|
|
2204 | |
|
|
2205 | default: |
2217 | default: |
2206 | *p++ = c; |
2218 | *p++ = c; |
2207 | state = st_ok; |
2219 | state = st_ok; |
2208 | break; |
2220 | break; |
2209 | } |
2221 | } |
2210 | |
2222 | |
2211 | break; |
2223 | break; |
2212 | |
2224 | |
2213 | case st_x1: |
2225 | case st_x1: |
2214 | case st_x2: |
2226 | case st_x2: |
2215 | c = toupper (c); |
2227 | c = tolower (c); |
2216 | |
2228 | |
2217 | if (c >= '0' && c <= 'F') |
2229 | if (c >= '0' && c <= '9') |
2218 | { |
|
|
2219 | if (c <= '9') |
|
|
2220 | c1 = (c1 << 4) + c - '0'; |
2230 | c1 = (c1 << 4) + c - '0'; |
2221 | else |
2231 | else if (c >= 'a' && c <= 'f') |
2222 | c1 = (c1 << 4) + c - 'A' + 10; |
2232 | c1 = (c1 << 4) + c - 'a' + 10; |
2223 | |
|
|
2224 | if (state == st_x1) |
|
|
2225 | state = st_x2; |
|
|
2226 | else |
|
|
2227 | { |
|
|
2228 | *p++ = c1; |
|
|
2229 | state = st_ok; |
|
|
2230 | } |
|
|
2231 | } |
|
|
2232 | else |
2233 | else |
2233 | return S_F; |
2234 | return S_F; |
|
|
2235 | |
|
|
2236 | if (state == st_x1) |
|
|
2237 | state = st_x2; |
|
|
2238 | else |
|
|
2239 | { |
|
|
2240 | *p++ = c1; |
|
|
2241 | state = st_ok; |
|
|
2242 | } |
2234 | |
2243 | |
2235 | break; |
2244 | break; |
2236 | |
2245 | |
2237 | case st_oct1: |
2246 | case st_oct1: |
2238 | case st_oct2: |
2247 | case st_oct2: |
… | |
… | |
2242 | backchar (SCHEME_A_ c); |
2251 | backchar (SCHEME_A_ c); |
2243 | state = st_ok; |
2252 | state = st_ok; |
2244 | } |
2253 | } |
2245 | else |
2254 | else |
2246 | { |
2255 | { |
2247 | if (state == st_oct2 && c1 >= 32) |
2256 | if (state == st_oct2 && c1 >= ' ') |
2248 | return S_F; |
2257 | return S_F; |
2249 | |
2258 | |
2250 | c1 = (c1 << 3) + (c - '0'); |
2259 | c1 = (c1 << 3) + (c - '0'); |
2251 | |
2260 | |
2252 | if (state == st_oct1) |
2261 | if (state == st_oct1) |
… | |
… | |
2257 | state = st_ok; |
2266 | state = st_ok; |
2258 | } |
2267 | } |
2259 | } |
2268 | } |
2260 | |
2269 | |
2261 | break; |
2270 | break; |
2262 | |
|
|
2263 | } |
2271 | } |
2264 | } |
2272 | } |
2265 | } |
2273 | } |
2266 | |
2274 | |
2267 | /* check c is in chars */ |
2275 | /* check c is in chars */ |
2268 | ecb_inline int |
2276 | ecb_inline int |
2269 | is_one_of (char *s, int c) |
2277 | is_one_of (const char *s, int c) |
2270 | { |
2278 | { |
2271 | if (c == EOF) |
|
|
2272 | return 1; |
|
|
2273 | |
|
|
2274 | return !!strchr (s, c); |
2279 | return c == EOF || !!strchr (s, c); |
2275 | } |
2280 | } |
2276 | |
2281 | |
2277 | /* skip white characters */ |
2282 | /* skip white characters */ |
2278 | ecb_inline int |
2283 | ecb_inline int |
2279 | skipspace (SCHEME_P) |
2284 | skipspace (SCHEME_P) |
… | |
… | |
2281 | int c, curr_line = 0; |
2286 | int c, curr_line = 0; |
2282 | |
2287 | |
2283 | do |
2288 | do |
2284 | { |
2289 | { |
2285 | c = inchar (SCHEME_A); |
2290 | c = inchar (SCHEME_A); |
|
|
2291 | |
2286 | #if SHOW_ERROR_LINE |
2292 | #if SHOW_ERROR_LINE |
2287 | if (c == '\n') |
2293 | if (ecb_expect_false (c == '\n')) |
2288 | curr_line++; |
2294 | curr_line++; |
2289 | #endif |
2295 | #endif |
|
|
2296 | |
|
|
2297 | if (ecb_expect_false (c == EOF)) |
|
|
2298 | return c; |
2290 | } |
2299 | } |
2291 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2300 | while (is_one_of (WHITESPACE, c)); |
2292 | |
2301 | |
2293 | /* record it */ |
2302 | /* record it */ |
2294 | #if SHOW_ERROR_LINE |
2303 | #if SHOW_ERROR_LINE |
2295 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2304 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2296 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2305 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2297 | #endif |
2306 | #endif |
2298 | |
2307 | |
2299 | if (c != EOF) |
|
|
2300 | { |
|
|
2301 | backchar (SCHEME_A_ c); |
2308 | backchar (SCHEME_A_ c); |
2302 | return 1; |
2309 | return 1; |
2303 | } |
|
|
2304 | else |
|
|
2305 | return EOF; |
|
|
2306 | } |
2310 | } |
2307 | |
2311 | |
2308 | /* get token */ |
2312 | /* get token */ |
2309 | static int |
2313 | static int |
2310 | token (SCHEME_P) |
2314 | token (SCHEME_P) |
… | |
… | |
2326 | return TOK_RPAREN; |
2330 | return TOK_RPAREN; |
2327 | |
2331 | |
2328 | case '.': |
2332 | case '.': |
2329 | c = inchar (SCHEME_A); |
2333 | c = inchar (SCHEME_A); |
2330 | |
2334 | |
2331 | if (is_one_of (" \n\t", c)) |
2335 | if (is_one_of (WHITESPACE, c)) |
2332 | return TOK_DOT; |
2336 | return TOK_DOT; |
2333 | else |
2337 | else |
2334 | { |
2338 | { |
2335 | //TODO: ungetc twice in a row is not supported in C |
|
|
2336 | backchar (SCHEME_A_ c); |
2339 | backchar (SCHEME_A_ c); |
2337 | backchar (SCHEME_A_ '.'); |
|
|
2338 | return TOK_ATOM; |
2340 | return TOK_DOTATOM; |
2339 | } |
2341 | } |
|
|
2342 | |
|
|
2343 | case '|': |
|
|
2344 | return TOK_STRATOM; |
2340 | |
2345 | |
2341 | case '\'': |
2346 | case '\'': |
2342 | return TOK_QUOTE; |
2347 | return TOK_QUOTE; |
2343 | |
2348 | |
2344 | case ';': |
2349 | case ';': |
… | |
… | |
2476 | } |
2481 | } |
2477 | |
2482 | |
2478 | putcharacter (SCHEME_A_ '"'); |
2483 | putcharacter (SCHEME_A_ '"'); |
2479 | } |
2484 | } |
2480 | |
2485 | |
2481 | |
|
|
2482 | /* print atoms */ |
2486 | /* print atoms */ |
2483 | static void |
2487 | static void |
2484 | printatom (SCHEME_P_ pointer l, int f) |
2488 | printatom (SCHEME_P_ pointer l, int f) |
2485 | { |
2489 | { |
2486 | char *p; |
2490 | char *p; |
2487 | int len; |
2491 | int len; |
2488 | |
2492 | |
2489 | atom2str (SCHEME_A_ l, f, &p, &len); |
2493 | atom2str (SCHEME_A_ l, f, &p, &len); |
2490 | putchars (SCHEME_A_ p, len); |
2494 | putchars (SCHEME_A_ p, len); |
2491 | } |
2495 | } |
2492 | |
|
|
2493 | |
2496 | |
2494 | /* Uses internal buffer unless string pointer is already available */ |
2497 | /* Uses internal buffer unless string pointer is already available */ |
2495 | static void |
2498 | static void |
2496 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2499 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2497 | { |
2500 | { |
… | |
… | |
2658 | #endif |
2661 | #endif |
2659 | } |
2662 | } |
2660 | else if (is_continuation (l)) |
2663 | else if (is_continuation (l)) |
2661 | p = "#<CONTINUATION>"; |
2664 | p = "#<CONTINUATION>"; |
2662 | else |
2665 | else |
|
|
2666 | { |
|
|
2667 | #if USE_PRINTF |
|
|
2668 | p = SCHEME_V->strbuff; |
|
|
2669 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2670 | #else |
2663 | p = "#<ERROR>"; |
2671 | p = "#<ERROR>"; |
|
|
2672 | #endif |
|
|
2673 | } |
2664 | |
2674 | |
2665 | *pp = p; |
2675 | *pp = p; |
2666 | *plen = strlen (p); |
2676 | *plen = strlen (p); |
2667 | } |
2677 | } |
2668 | |
2678 | |
… | |
… | |
2812 | /* () is #t in R5RS */ |
2822 | /* () is #t in R5RS */ |
2813 | #define is_true(p) ((p) != S_F) |
2823 | #define is_true(p) ((p) != S_F) |
2814 | #define is_false(p) ((p) == S_F) |
2824 | #define is_false(p) ((p) == S_F) |
2815 | |
2825 | |
2816 | /* ========== Environment implementation ========== */ |
2826 | /* ========== Environment implementation ========== */ |
2817 | |
|
|
2818 | #if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) |
|
|
2819 | |
|
|
2820 | static int |
|
|
2821 | hash_fn (const char *key, int table_size) |
|
|
2822 | { |
|
|
2823 | const unsigned char *p = key; |
|
|
2824 | uint32_t hash = 2166136261; |
|
|
2825 | |
|
|
2826 | while (*p) |
|
|
2827 | hash = (hash ^ *p++) * 16777619; |
|
|
2828 | |
|
|
2829 | return hash % table_size; |
|
|
2830 | } |
|
|
2831 | #endif |
|
|
2832 | |
2827 | |
2833 | #ifndef USE_ALIST_ENV |
2828 | #ifndef USE_ALIST_ENV |
2834 | |
2829 | |
2835 | /* |
2830 | /* |
2836 | * In this implementation, each frame of the environment may be |
2831 | * In this implementation, each frame of the environment may be |
… | |
… | |
2853 | |
2848 | |
2854 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2849 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2855 | setenvironment (SCHEME_V->envir); |
2850 | setenvironment (SCHEME_V->envir); |
2856 | } |
2851 | } |
2857 | |
2852 | |
|
|
2853 | static uint32_t |
|
|
2854 | sym_hash (pointer sym, uint32_t size) |
|
|
2855 | { |
|
|
2856 | uintptr_t ptr = (uintptr_t)sym; |
|
|
2857 | |
|
|
2858 | #if 0 |
|
|
2859 | /* table size is prime, so why mix */ |
|
|
2860 | ptr += ptr >> 32; |
|
|
2861 | ptr += ptr >> 16; |
|
|
2862 | ptr += ptr >> 8; |
|
|
2863 | #endif |
|
|
2864 | |
|
|
2865 | return ptr % size; |
|
|
2866 | } |
|
|
2867 | |
2858 | ecb_inline void |
2868 | ecb_inline void |
2859 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2869 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2860 | { |
2870 | { |
2861 | pointer slot = immutable_cons (variable, value); |
2871 | pointer slot = immutable_cons (variable, value); |
2862 | |
2872 | |
2863 | if (is_vector (car (env))) |
2873 | if (is_vector (car (env))) |
2864 | { |
2874 | { |
2865 | int location = hash_fn (symname (variable), veclength (car (env))); |
2875 | int location = sym_hash (variable, veclength (car (env))); |
2866 | |
|
|
2867 | vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); |
2876 | vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); |
2868 | } |
2877 | } |
2869 | else |
2878 | else |
2870 | set_car (env, immutable_cons (slot, car (env))); |
2879 | set_car (env, immutable_cons (slot, car (env))); |
2871 | } |
2880 | } |
2872 | |
2881 | |
2873 | static pointer |
2882 | static pointer |
2874 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2883 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2875 | { |
2884 | { |
2876 | pointer x, y; |
2885 | pointer x, y; |
2877 | int location; |
|
|
2878 | |
2886 | |
2879 | for (x = env; x != NIL; x = cdr (x)) |
2887 | for (x = env; x != NIL; x = cdr (x)) |
2880 | { |
2888 | { |
2881 | if (is_vector (car (x))) |
2889 | if (is_vector (car (x))) |
2882 | { |
2890 | { |
2883 | location = hash_fn (symname (hdl), veclength (car (x))); |
2891 | int location = sym_hash (hdl, veclength (car (x))); |
2884 | y = vector_get (car (x), location); |
2892 | y = vector_get (car (x), location); |
2885 | } |
2893 | } |
2886 | else |
2894 | else |
2887 | y = car (x); |
2895 | y = car (x); |
2888 | |
2896 | |
… | |
… | |
2925 | for (y = car (x); y != NIL; y = cdr (y)) |
2933 | for (y = car (x); y != NIL; y = cdr (y)) |
2926 | if (caar (y) == hdl) |
2934 | if (caar (y) == hdl) |
2927 | break; |
2935 | break; |
2928 | |
2936 | |
2929 | if (y != NIL) |
2937 | if (y != NIL) |
|
|
2938 | return car (y); |
2930 | break; |
2939 | break; |
2931 | |
2940 | |
2932 | if (!all) |
2941 | if (!all) |
2933 | return NIL; |
2942 | break; |
2934 | } |
2943 | } |
2935 | |
|
|
2936 | if (x != NIL) |
|
|
2937 | return car (y); |
|
|
2938 | |
2944 | |
2939 | return NIL; |
2945 | return NIL; |
2940 | } |
2946 | } |
2941 | |
2947 | |
2942 | #endif /* USE_ALIST_ENV else */ |
2948 | #endif /* USE_ALIST_ENV else */ |
2943 | |
2949 | |
2944 | ecb_inline void |
2950 | ecb_inline void |
2945 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2951 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2946 | { |
2952 | { |
|
|
2953 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2947 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2954 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2948 | } |
2955 | } |
2949 | |
2956 | |
2950 | ecb_inline void |
2957 | ecb_inline void |
2951 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2958 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
… | |
… | |
3243 | |
3250 | |
3244 | #endif |
3251 | #endif |
3245 | |
3252 | |
3246 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3253 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3247 | |
3254 | |
|
|
3255 | #if EXPERIMENT |
|
|
3256 | static int |
|
|
3257 | debug (SCHEME_P_ int indent, pointer x) |
|
|
3258 | { |
|
|
3259 | int c; |
|
|
3260 | |
|
|
3261 | if (is_syntax (x)) |
|
|
3262 | { |
|
|
3263 | printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x)); |
|
|
3264 | return 8 + 8; |
|
|
3265 | } |
|
|
3266 | |
|
|
3267 | if (x == NIL) |
|
|
3268 | { |
|
|
3269 | printf ("%*sNIL\n", indent, ""); |
|
|
3270 | return 3; |
|
|
3271 | } |
|
|
3272 | |
|
|
3273 | switch (type (x)) |
|
|
3274 | { |
|
|
3275 | case T_INTEGER: |
|
|
3276 | printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x); |
|
|
3277 | return 32+8; |
|
|
3278 | |
|
|
3279 | case T_SYMBOL: |
|
|
3280 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
|
|
3281 | return 24+8; |
|
|
3282 | |
|
|
3283 | case T_CLOSURE: |
|
|
3284 | printf ("%*sS<%s>\n", indent, "", "closure"); |
|
|
3285 | debug (SCHEME_A_ indent + 3, cdr(x)); |
|
|
3286 | return 32 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3287 | |
|
|
3288 | case T_PAIR: |
|
|
3289 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
|
|
3290 | c = debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3291 | c += debug (SCHEME_A_ indent + 3, cdr (x)); |
|
|
3292 | return c + 1; |
|
|
3293 | |
|
|
3294 | case T_PORT: |
|
|
3295 | printf ("%*sS<%s>\n", indent, "", "port"); |
|
|
3296 | return 24+8; |
|
|
3297 | |
|
|
3298 | case T_VECTOR: |
|
|
3299 | printf ("%*sS<%s>\n", indent, "", "vector"); |
|
|
3300 | return 24+8; |
|
|
3301 | |
|
|
3302 | case T_ENVIRONMENT: |
|
|
3303 | printf ("%*sS<%s>\n", indent, "", "environment"); |
|
|
3304 | return 0 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3305 | |
|
|
3306 | default: |
|
|
3307 | printf ("unhandled type %d\n", type (x)); |
|
|
3308 | break; |
|
|
3309 | } |
|
|
3310 | } |
|
|
3311 | #endif |
|
|
3312 | |
3248 | static int |
3313 | static int |
3249 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3314 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3250 | { |
3315 | { |
3251 | pointer args = SCHEME_V->args; |
3316 | pointer args = SCHEME_V->args; |
3252 | pointer x, y; |
3317 | pointer x, y; |
3253 | |
3318 | |
3254 | switch (op) |
3319 | switch (op) |
3255 | { |
3320 | { |
|
|
3321 | #if EXPERIMENT //D |
|
|
3322 | case OP_DEBUG: |
|
|
3323 | printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); |
|
|
3324 | printf ("\n"); |
|
|
3325 | s_return (S_T); |
|
|
3326 | #endif |
3256 | case OP_LOAD: /* load */ |
3327 | case OP_LOAD: /* load */ |
3257 | if (file_interactive (SCHEME_A)) |
3328 | if (file_interactive (SCHEME_A)) |
3258 | { |
3329 | { |
3259 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3330 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3260 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3331 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
… | |
… | |
3383 | } |
3454 | } |
3384 | else |
3455 | else |
3385 | s_return (SCHEME_V->code); |
3456 | s_return (SCHEME_V->code); |
3386 | |
3457 | |
3387 | case OP_E0ARGS: /* eval arguments */ |
3458 | case OP_E0ARGS: /* eval arguments */ |
3388 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3459 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3389 | { |
3460 | { |
3390 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3461 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3391 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3462 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3392 | SCHEME_V->code = SCHEME_V->value; |
3463 | SCHEME_V->code = SCHEME_V->value; |
3393 | s_goto (OP_APPLY); |
3464 | s_goto (OP_APPLY); |
… | |
… | |
4128 | else |
4199 | else |
4129 | Error_0 ("modulo: division by zero"); |
4200 | Error_0 ("modulo: division by zero"); |
4130 | |
4201 | |
4131 | s_return (mk_number (SCHEME_A_ v)); |
4202 | s_return (mk_number (SCHEME_A_ v)); |
4132 | |
4203 | |
4133 | case OP_CAR: /* car */ |
4204 | /* the compiler will optimize this mess... */ |
4134 | s_return (caar (args)); |
4205 | case OP_CAR: op_car: s_return (car (x)); |
4135 | |
4206 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4136 | case OP_CDR: /* cdr */ |
4207 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4137 | s_return (cdar (args)); |
4208 | case OP_CADR: op_cadr: x = cdr (x); goto op_car; |
|
|
4209 | case OP_CDAR: op_cdar: x = car (x); goto op_cdr; |
|
|
4210 | case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr; |
|
|
4211 | case OP_CAAAR: op_caaar: x = car (x); goto op_caar; |
|
|
4212 | case OP_CAADR: op_caadr: x = cdr (x); goto op_caar; |
|
|
4213 | case OP_CADAR: op_cadar: x = car (x); goto op_cadr; |
|
|
4214 | case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr; |
|
|
4215 | case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar; |
|
|
4216 | case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar; |
|
|
4217 | case OP_CDDAR: op_cddar: x = car (x); goto op_cddr; |
|
|
4218 | case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr; |
|
|
4219 | case OP_CAAAAR: x = car (x); goto op_caaar; |
|
|
4220 | case OP_CAAADR: x = cdr (x); goto op_caaar; |
|
|
4221 | case OP_CAADAR: x = car (x); goto op_caadr; |
|
|
4222 | case OP_CAADDR: x = cdr (x); goto op_caadr; |
|
|
4223 | case OP_CADAAR: x = car (x); goto op_cadar; |
|
|
4224 | case OP_CADADR: x = cdr (x); goto op_cadar; |
|
|
4225 | case OP_CADDAR: x = car (x); goto op_caddr; |
|
|
4226 | case OP_CADDDR: x = cdr (x); goto op_caddr; |
|
|
4227 | case OP_CDAAAR: x = car (x); goto op_cdaar; |
|
|
4228 | case OP_CDAADR: x = cdr (x); goto op_cdaar; |
|
|
4229 | case OP_CDADAR: x = car (x); goto op_cdadr; |
|
|
4230 | case OP_CDADDR: x = cdr (x); goto op_cdadr; |
|
|
4231 | case OP_CDDAAR: x = car (x); goto op_cddar; |
|
|
4232 | case OP_CDDADR: x = cdr (x); goto op_cddar; |
|
|
4233 | case OP_CDDDAR: x = car (x); goto op_cdddr; |
|
|
4234 | case OP_CDDDDR: x = cdr (x); goto op_cdddr; |
4138 | |
4235 | |
4139 | case OP_CONS: /* cons */ |
4236 | case OP_CONS: /* cons */ |
4140 | set_cdr (args, cadr (args)); |
4237 | set_cdr (args, cadr (args)); |
4141 | s_return (args); |
4238 | s_return (args); |
4142 | |
4239 | |
… | |
… | |
4391 | } |
4488 | } |
4392 | |
4489 | |
4393 | case OP_VECLEN: /* vector-length */ |
4490 | case OP_VECLEN: /* vector-length */ |
4394 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4491 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4395 | |
4492 | |
|
|
4493 | case OP_VECRESIZE: |
|
|
4494 | vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args)); |
|
|
4495 | s_return (x); |
|
|
4496 | |
4396 | case OP_VECREF: /* vector-ref */ |
4497 | case OP_VECREF: /* vector-ref */ |
4397 | { |
4498 | { |
4398 | int index = ivalue_unchecked (cadr (args)); |
4499 | int index = ivalue_unchecked (cadr (args)); |
4399 | |
4500 | |
4400 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4501 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
… | |
… | |
4460 | pointer d = cdr (args); |
4561 | pointer d = cdr (args); |
4461 | int r; |
4562 | int r; |
4462 | |
4563 | |
4463 | switch (op) |
4564 | switch (op) |
4464 | { |
4565 | { |
4465 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4566 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4466 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; |
4567 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break; |
4467 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4568 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
4468 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4569 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
4469 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
4570 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
|
|
4571 | case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break; |
4470 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4572 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
4471 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4573 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
4472 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4574 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
4473 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4575 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
4474 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4576 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4475 | |
4577 | |
4476 | #if USE_CHAR_CLASSIFIERS |
4578 | #if USE_CHAR_CLASSIFIERS |
4477 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4579 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4478 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4580 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4479 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4581 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
… | |
… | |
4955 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5057 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
4956 | SCHEME_V->tok = token (SCHEME_A); |
5058 | SCHEME_V->tok = token (SCHEME_A); |
4957 | s_goto (OP_RDSEXPR); |
5059 | s_goto (OP_RDSEXPR); |
4958 | |
5060 | |
4959 | case TOK_ATOM: |
5061 | case TOK_ATOM: |
4960 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); |
5062 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); |
|
|
5063 | |
|
|
5064 | case TOK_DOTATOM: |
|
|
5065 | SCHEME_V->strbuff[0] = '.'; |
|
|
5066 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
|
|
5067 | |
|
|
5068 | case TOK_STRATOM: |
|
|
5069 | x = readstrexp (SCHEME_A_ '|'); |
|
|
5070 | //TODO: haven't checked whether the garbage collector could interfere |
|
|
5071 | s_return (mk_atom (SCHEME_A_ strvalue (x))); |
4961 | |
5072 | |
4962 | case TOK_DQUOTE: |
5073 | case TOK_DQUOTE: |
4963 | x = readstrexp (SCHEME_A); |
5074 | x = readstrexp (SCHEME_A_ '"'); |
4964 | |
5075 | |
4965 | if (x == S_F) |
5076 | if (x == S_F) |
4966 | Error_0 ("Error reading string"); |
5077 | Error_0 ("Error reading string"); |
4967 | |
5078 | |
4968 | setimmutable (x); |
5079 | setimmutable (x); |
… | |
… | |
4980 | s_goto (OP_EVAL); |
5091 | s_goto (OP_EVAL); |
4981 | } |
5092 | } |
4982 | } |
5093 | } |
4983 | |
5094 | |
4984 | case TOK_SHARP_CONST: |
5095 | case TOK_SHARP_CONST: |
4985 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) |
5096 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) |
4986 | Error_0 ("undefined sharp expression"); |
5097 | Error_0 ("undefined sharp expression"); |
4987 | else |
5098 | else |
4988 | s_return (x); |
5099 | s_return (x); |
4989 | |
5100 | |
4990 | default: |
5101 | default: |
… | |
… | |
5212 | |
5323 | |
5213 | case OP_CLOSUREP: /* closure? */ |
5324 | case OP_CLOSUREP: /* closure? */ |
5214 | /* |
5325 | /* |
5215 | * Note, macro object is also a closure. |
5326 | * Note, macro object is also a closure. |
5216 | * Therefore, (closure? <#MACRO>) ==> #t |
5327 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5328 | * (schmorp) well, obviously not, fix? TODO |
5217 | */ |
5329 | */ |
5218 | s_retbool (is_closure (a)); |
5330 | s_retbool (is_closure (a)); |
5219 | |
5331 | |
5220 | case OP_MACROP: /* macro? */ |
5332 | case OP_MACROP: /* macro? */ |
5221 | s_retbool (is_macro (a)); |
5333 | s_retbool (is_macro (a)); |
… | |
… | |
5462 | |
5574 | |
5463 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5575 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5464 | static int |
5576 | static int |
5465 | syntaxnum (pointer p) |
5577 | syntaxnum (pointer p) |
5466 | { |
5578 | { |
5467 | const char *s = strvalue (car (p)); |
5579 | const char *s = strvalue (p); |
5468 | |
5580 | |
5469 | switch (strlength (car (p))) |
5581 | switch (strlength (p)) |
5470 | { |
5582 | { |
5471 | case 2: |
5583 | case 2: |
5472 | if (s[0] == 'i') |
5584 | if (s[0] == 'i') |
5473 | return OP_IF0; /* if */ |
5585 | return OP_IF0; /* if */ |
5474 | else |
5586 | else |
… | |
… | |
5910 | # endif |
6022 | # endif |
5911 | int fin; |
6023 | int fin; |
5912 | char *file_name = InitFile; |
6024 | char *file_name = InitFile; |
5913 | int retcode; |
6025 | int retcode; |
5914 | int isfile = 1; |
6026 | int isfile = 1; |
|
|
6027 | system ("ps v $PPID");//D |
5915 | |
6028 | |
5916 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6029 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5917 | { |
6030 | { |
5918 | xwrstr ("Usage: tinyscheme -?\n"); |
6031 | xwrstr ("Usage: tinyscheme -?\n"); |
5919 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6032 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |