… | |
… | |
74 | TOK_SHARP_CONST, |
74 | TOK_SHARP_CONST, |
75 | TOK_VEC |
75 | TOK_VEC |
76 | }; |
76 | }; |
77 | |
77 | |
78 | #define BACKQUOTE '`' |
78 | #define BACKQUOTE '`' |
79 | #define DELIMITERS "()\";\f\t\v\n\r " |
79 | #define WHITESPACE " \t\r\n\v\f" |
|
|
80 | #define DELIMITERS "()\";" WHITESPACE |
80 | |
81 | |
81 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
82 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
82 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
83 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
83 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
84 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
84 | #define S_SINK (&SCHEME_V->xsink) |
85 | #define S_SINK (&SCHEME_V->xsink) |
… | |
… | |
406 | } |
407 | } |
407 | |
408 | |
408 | INTERFACE char * |
409 | INTERFACE char * |
409 | symname (pointer p) |
410 | symname (pointer p) |
410 | { |
411 | { |
411 | return strvalue (car (p)); |
412 | return strvalue (p); |
412 | } |
413 | } |
413 | |
414 | |
414 | #if USE_PLIST |
415 | #if USE_PLIST |
415 | SCHEME_EXPORT int |
416 | SCHEME_EXPORT int |
416 | hasprop (pointer p) |
417 | hasprop (pointer p) |
… | |
… | |
440 | } |
441 | } |
441 | |
442 | |
442 | INTERFACE char * |
443 | INTERFACE char * |
443 | syntaxname (pointer p) |
444 | syntaxname (pointer p) |
444 | { |
445 | { |
445 | return strvalue (car (p)); |
446 | return strvalue (p); |
446 | } |
447 | } |
447 | |
448 | |
448 | #define procnum(p) ivalue_unchecked (p) |
449 | #define procnum(p) ivalue_unchecked (p) |
449 | static const char *procname (pointer x); |
450 | static const char *procname (pointer x); |
450 | |
451 | |
… | |
… | |
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 | |
1108 | |
1109 | #ifndef USE_OBJECT_LIST |
1109 | #ifndef USE_OBJECT_LIST |
1110 | |
1110 | |
… | |
… | |
1352 | { |
1352 | { |
1353 | int i; |
1353 | int i; |
1354 | |
1354 | |
1355 | for (i = start; i < veclength (vec); i++) |
1355 | for (i = start; i < veclength (vec); i++) |
1356 | vecvalue (vec)[i] = obj; |
1356 | vecvalue (vec)[i] = obj; |
|
|
1357 | } |
|
|
1358 | |
|
|
1359 | INTERFACE void |
|
|
1360 | vector_resize (pointer vec, uint32_t newsize, pointer fill) |
|
|
1361 | { |
|
|
1362 | uint32_t oldsize = veclength (vec); |
|
|
1363 | vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer)); |
|
|
1364 | veclength (vec) = newsize; |
|
|
1365 | fill_vector (vec, oldsize, fill); |
1357 | } |
1366 | } |
1358 | |
1367 | |
1359 | INTERFACE pointer |
1368 | INTERFACE pointer |
1360 | vector_get (pointer vec, uint32_t ielem) |
1369 | vector_get (pointer vec, uint32_t ielem) |
1361 | { |
1370 | { |
… | |
… | |
1680 | |
1689 | |
1681 | static void |
1690 | static void |
1682 | finalize_cell (SCHEME_P_ pointer a) |
1691 | finalize_cell (SCHEME_P_ pointer a) |
1683 | { |
1692 | { |
1684 | /* TODO, fast bitmap check? */ |
1693 | /* TODO, fast bitmap check? */ |
1685 | if (is_string (a)) |
1694 | if (is_string (a) || is_symbol (a)) |
1686 | free (strvalue (a)); |
1695 | free (strvalue (a)); |
1687 | else if (is_vector (a)) |
1696 | else if (is_vector (a)) |
1688 | free (vecvalue (a)); |
1697 | free (vecvalue (a)); |
1689 | #if USE_PORTS |
1698 | #if USE_PORTS |
1690 | else if (is_port (a)) |
1699 | else if (is_port (a)) |
… | |
… | |
2256 | |
2265 | |
2257 | /* check c is in chars */ |
2266 | /* check c is in chars */ |
2258 | ecb_inline int |
2267 | ecb_inline int |
2259 | is_one_of (const char *s, int c) |
2268 | is_one_of (const char *s, int c) |
2260 | { |
2269 | { |
2261 | if (c == EOF) |
|
|
2262 | return 1; |
|
|
2263 | |
|
|
2264 | return !!strchr (s, c); |
2270 | return c == EOF || !!strchr (s, c); |
2265 | } |
2271 | } |
2266 | |
2272 | |
2267 | /* skip white characters */ |
2273 | /* skip white characters */ |
2268 | ecb_inline int |
2274 | ecb_inline int |
2269 | skipspace (SCHEME_P) |
2275 | skipspace (SCHEME_P) |
… | |
… | |
2271 | int c, curr_line = 0; |
2277 | int c, curr_line = 0; |
2272 | |
2278 | |
2273 | do |
2279 | do |
2274 | { |
2280 | { |
2275 | c = inchar (SCHEME_A); |
2281 | c = inchar (SCHEME_A); |
|
|
2282 | |
2276 | #if SHOW_ERROR_LINE |
2283 | #if SHOW_ERROR_LINE |
2277 | if (c == '\n') |
2284 | if (ecb_expect_false (c == '\n')) |
2278 | curr_line++; |
2285 | curr_line++; |
2279 | #endif |
2286 | #endif |
|
|
2287 | |
|
|
2288 | if (ecb_expect_false (c == EOF)) |
|
|
2289 | return c; |
2280 | } |
2290 | } |
2281 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2291 | while (is_one_of (WHITESPACE, c)); |
2282 | |
2292 | |
2283 | /* record it */ |
2293 | /* record it */ |
2284 | #if SHOW_ERROR_LINE |
2294 | #if SHOW_ERROR_LINE |
2285 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2295 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2286 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2296 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2287 | #endif |
2297 | #endif |
2288 | |
2298 | |
2289 | if (c != EOF) |
|
|
2290 | { |
|
|
2291 | backchar (SCHEME_A_ c); |
2299 | backchar (SCHEME_A_ c); |
2292 | return 1; |
2300 | return 1; |
2293 | } |
|
|
2294 | else |
|
|
2295 | return EOF; |
|
|
2296 | } |
2301 | } |
2297 | |
2302 | |
2298 | /* get token */ |
2303 | /* get token */ |
2299 | static int |
2304 | static int |
2300 | token (SCHEME_P) |
2305 | token (SCHEME_P) |
… | |
… | |
2316 | return TOK_RPAREN; |
2321 | return TOK_RPAREN; |
2317 | |
2322 | |
2318 | case '.': |
2323 | case '.': |
2319 | c = inchar (SCHEME_A); |
2324 | c = inchar (SCHEME_A); |
2320 | |
2325 | |
2321 | if (is_one_of (" \n\t", c)) |
2326 | if (is_one_of (WHITESPACE, c)) |
2322 | return TOK_DOT; |
2327 | return TOK_DOT; |
2323 | else |
2328 | else |
2324 | { |
2329 | { |
2325 | backchar (SCHEME_A_ c); |
2330 | backchar (SCHEME_A_ c); |
2326 | return TOK_DOTATOM; |
2331 | return TOK_DOTATOM; |
… | |
… | |
2467 | } |
2472 | } |
2468 | |
2473 | |
2469 | putcharacter (SCHEME_A_ '"'); |
2474 | putcharacter (SCHEME_A_ '"'); |
2470 | } |
2475 | } |
2471 | |
2476 | |
2472 | |
|
|
2473 | /* print atoms */ |
2477 | /* print atoms */ |
2474 | static void |
2478 | static void |
2475 | printatom (SCHEME_P_ pointer l, int f) |
2479 | printatom (SCHEME_P_ pointer l, int f) |
2476 | { |
2480 | { |
2477 | char *p; |
2481 | char *p; |
2478 | int len; |
2482 | int len; |
2479 | |
2483 | |
2480 | atom2str (SCHEME_A_ l, f, &p, &len); |
2484 | atom2str (SCHEME_A_ l, f, &p, &len); |
2481 | putchars (SCHEME_A_ p, len); |
2485 | putchars (SCHEME_A_ p, len); |
2482 | } |
2486 | } |
2483 | |
|
|
2484 | |
2487 | |
2485 | /* Uses internal buffer unless string pointer is already available */ |
2488 | /* Uses internal buffer unless string pointer is already available */ |
2486 | static void |
2489 | static void |
2487 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2490 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2488 | { |
2491 | { |
… | |
… | |
2649 | #endif |
2652 | #endif |
2650 | } |
2653 | } |
2651 | else if (is_continuation (l)) |
2654 | else if (is_continuation (l)) |
2652 | p = "#<CONTINUATION>"; |
2655 | p = "#<CONTINUATION>"; |
2653 | else |
2656 | else |
|
|
2657 | { |
|
|
2658 | #if USE_PRINTF |
|
|
2659 | p = SCHEME_V->strbuff; |
|
|
2660 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2661 | #else |
2654 | p = "#<ERROR>"; |
2662 | p = "#<ERROR>"; |
|
|
2663 | #endif |
|
|
2664 | } |
2655 | |
2665 | |
2656 | *pp = p; |
2666 | *pp = p; |
2657 | *plen = strlen (p); |
2667 | *plen = strlen (p); |
2658 | } |
2668 | } |
2659 | |
2669 | |
… | |
… | |
2929 | #endif /* USE_ALIST_ENV else */ |
2939 | #endif /* USE_ALIST_ENV else */ |
2930 | |
2940 | |
2931 | ecb_inline void |
2941 | ecb_inline void |
2932 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2942 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2933 | { |
2943 | { |
|
|
2944 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
2934 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2945 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2935 | } |
2946 | } |
2936 | |
2947 | |
2937 | ecb_inline void |
2948 | ecb_inline void |
2938 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2949 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
… | |
… | |
3230 | |
3241 | |
3231 | #endif |
3242 | #endif |
3232 | |
3243 | |
3233 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3244 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3234 | |
3245 | |
|
|
3246 | #if 1 |
|
|
3247 | static int |
|
|
3248 | debug (SCHEME_P_ int indent, pointer x) |
|
|
3249 | { |
|
|
3250 | int c; |
|
|
3251 | |
|
|
3252 | if (is_syntax (x)) |
|
|
3253 | { |
|
|
3254 | printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x)); |
|
|
3255 | return 8 + 8; |
|
|
3256 | } |
|
|
3257 | |
|
|
3258 | if (x == NIL) |
|
|
3259 | { |
|
|
3260 | printf ("%*sNIL\n", indent, ""); |
|
|
3261 | return 3; |
|
|
3262 | } |
|
|
3263 | |
|
|
3264 | switch (type (x)) |
|
|
3265 | { |
|
|
3266 | case T_INTEGER: |
|
|
3267 | printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x); |
|
|
3268 | return 32+8; |
|
|
3269 | |
|
|
3270 | case T_SYMBOL: |
|
|
3271 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
|
|
3272 | return 24+8; |
|
|
3273 | |
|
|
3274 | case T_CLOSURE: |
|
|
3275 | printf ("%*sS<%s>\n", indent, "", "closure"); |
|
|
3276 | debug (SCHEME_A_ indent + 3, cdr(x)); |
|
|
3277 | return 32 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3278 | |
|
|
3279 | case T_PAIR: |
|
|
3280 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
|
|
3281 | c = debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3282 | c += debug (SCHEME_A_ indent + 3, cdr (x)); |
|
|
3283 | return c + 1; |
|
|
3284 | |
|
|
3285 | case T_PORT: |
|
|
3286 | printf ("%*sS<%s>\n", indent, "", "port"); |
|
|
3287 | return 24+8; |
|
|
3288 | |
|
|
3289 | case T_VECTOR: |
|
|
3290 | printf ("%*sS<%s>\n", indent, "", "vector"); |
|
|
3291 | return 24+8; |
|
|
3292 | |
|
|
3293 | case T_ENVIRONMENT: |
|
|
3294 | printf ("%*sS<%s>\n", indent, "", "environment"); |
|
|
3295 | return 0 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3296 | |
|
|
3297 | default: |
|
|
3298 | printf ("unhandled type %d\n", type (x)); |
|
|
3299 | break; |
|
|
3300 | } |
|
|
3301 | } |
|
|
3302 | #endif |
|
|
3303 | |
3235 | static int |
3304 | static int |
3236 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3305 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3237 | { |
3306 | { |
3238 | pointer args = SCHEME_V->args; |
3307 | pointer args = SCHEME_V->args; |
3239 | pointer x, y; |
3308 | pointer x, y; |
3240 | |
3309 | |
3241 | switch (op) |
3310 | switch (op) |
3242 | { |
3311 | { |
|
|
3312 | #if 1 //D |
|
|
3313 | case OP_DEBUG: |
|
|
3314 | printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); |
|
|
3315 | printf ("\n"); |
|
|
3316 | s_return (S_T); |
|
|
3317 | #endif |
3243 | case OP_LOAD: /* load */ |
3318 | case OP_LOAD: /* load */ |
3244 | if (file_interactive (SCHEME_A)) |
3319 | if (file_interactive (SCHEME_A)) |
3245 | { |
3320 | { |
3246 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3321 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3247 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3322 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
… | |
… | |
3370 | } |
3445 | } |
3371 | else |
3446 | else |
3372 | s_return (SCHEME_V->code); |
3447 | s_return (SCHEME_V->code); |
3373 | |
3448 | |
3374 | case OP_E0ARGS: /* eval arguments */ |
3449 | case OP_E0ARGS: /* eval arguments */ |
3375 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3450 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3376 | { |
3451 | { |
3377 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3452 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3378 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3453 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3379 | SCHEME_V->code = SCHEME_V->value; |
3454 | SCHEME_V->code = SCHEME_V->value; |
3380 | s_goto (OP_APPLY); |
3455 | s_goto (OP_APPLY); |
… | |
… | |
4378 | } |
4453 | } |
4379 | |
4454 | |
4380 | case OP_VECLEN: /* vector-length */ |
4455 | case OP_VECLEN: /* vector-length */ |
4381 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4456 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4382 | |
4457 | |
|
|
4458 | case OP_VECRESIZE: |
|
|
4459 | vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args)); |
|
|
4460 | s_return (x); |
|
|
4461 | |
4383 | case OP_VECREF: /* vector-ref */ |
4462 | case OP_VECREF: /* vector-ref */ |
4384 | { |
4463 | { |
4385 | int index = ivalue_unchecked (cadr (args)); |
4464 | int index = ivalue_unchecked (cadr (args)); |
4386 | |
4465 | |
4387 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4466 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
… | |
… | |
4947 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); |
5026 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); |
4948 | |
5027 | |
4949 | case TOK_DOTATOM: |
5028 | case TOK_DOTATOM: |
4950 | SCHEME_V->strbuff[0] = '.'; |
5029 | SCHEME_V->strbuff[0] = '.'; |
4951 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
5030 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
|
|
5031 | |
|
|
5032 | case TOK_STRATOM: |
|
|
5033 | x = readstrexp (SCHEME_A_ '|'); |
|
|
5034 | //TODO: haven't checked whether the garbage collector could interfere |
|
|
5035 | s_return (mk_atom (SCHEME_A_ strvalue (x))); |
4952 | |
5036 | |
4953 | case TOK_DQUOTE: |
5037 | case TOK_DQUOTE: |
4954 | x = readstrexp (SCHEME_A_ '"'); |
5038 | x = readstrexp (SCHEME_A_ '"'); |
4955 | |
5039 | |
4956 | if (x == S_F) |
5040 | if (x == S_F) |
… | |
… | |
5203 | |
5287 | |
5204 | case OP_CLOSUREP: /* closure? */ |
5288 | case OP_CLOSUREP: /* closure? */ |
5205 | /* |
5289 | /* |
5206 | * Note, macro object is also a closure. |
5290 | * Note, macro object is also a closure. |
5207 | * Therefore, (closure? <#MACRO>) ==> #t |
5291 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5292 | * (schmorp) well, obviously not, fix? TODO |
5208 | */ |
5293 | */ |
5209 | s_retbool (is_closure (a)); |
5294 | s_retbool (is_closure (a)); |
5210 | |
5295 | |
5211 | case OP_MACROP: /* macro? */ |
5296 | case OP_MACROP: /* macro? */ |
5212 | s_retbool (is_macro (a)); |
5297 | s_retbool (is_macro (a)); |
… | |
… | |
5453 | |
5538 | |
5454 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5539 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5455 | static int |
5540 | static int |
5456 | syntaxnum (pointer p) |
5541 | syntaxnum (pointer p) |
5457 | { |
5542 | { |
5458 | const char *s = strvalue (car (p)); |
5543 | const char *s = strvalue (p); |
5459 | |
5544 | |
5460 | switch (strlength (car (p))) |
5545 | switch (strlength (p)) |
5461 | { |
5546 | { |
5462 | case 2: |
5547 | case 2: |
5463 | if (s[0] == 'i') |
5548 | if (s[0] == 'i') |
5464 | return OP_IF0; /* if */ |
5549 | return OP_IF0; /* if */ |
5465 | else |
5550 | else |
… | |
… | |
5901 | # endif |
5986 | # endif |
5902 | int fin; |
5987 | int fin; |
5903 | char *file_name = InitFile; |
5988 | char *file_name = InitFile; |
5904 | int retcode; |
5989 | int retcode; |
5905 | int isfile = 1; |
5990 | int isfile = 1; |
|
|
5991 | system ("ps v $PPID");//D |
5906 | |
5992 | |
5907 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5993 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
5908 | { |
5994 | { |
5909 | xwrstr ("Usage: tinyscheme -?\n"); |
5995 | xwrstr ("Usage: tinyscheme -?\n"); |
5910 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
5996 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |