ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/microscheme/scheme.c
(Generate patch)

Comparing cvsroot/microscheme/scheme.c (file contents):
Revision 1.36 by root, Sun Nov 29 05:04:29 2015 UTC vs.
Revision 1.40 by root, Mon Nov 30 05:19:01 2015 UTC

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
408INTERFACE char * 409INTERFACE char *
409symname (pointer p) 410symname (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
415SCHEME_EXPORT int 416SCHEME_EXPORT int
416hasprop (pointer p) 417hasprop (pointer p)
440} 441}
441 442
442INTERFACE char * 443INTERFACE char *
443syntaxname (pointer p) 444syntaxname (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)
449static const char *procname (pointer x); 450static const char *procname (pointer x);
450 451
1099static pointer 1100static pointer
1100generate_symbol (SCHEME_P_ const char *name) 1101generate_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
1359INTERFACE void
1360vector_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
1359INTERFACE pointer 1368INTERFACE pointer
1360vector_get (pointer vec, uint32_t ielem) 1369vector_get (pointer vec, uint32_t ielem)
1361{ 1370{
1680 1689
1681static void 1690static void
1682finalize_cell (SCHEME_P_ pointer a) 1691finalize_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 */
2258ecb_inline int 2267ecb_inline int
2259is_one_of (const char *s, int c) 2268is_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 */
2268ecb_inline int 2274ecb_inline int
2269skipspace (SCHEME_P) 2275skipspace (SCHEME_P)
2276#if SHOW_ERROR_LINE 2282#if SHOW_ERROR_LINE
2277 if (c == '\n') 2283 if (c == '\n')
2278 curr_line++; 2284 curr_line++;
2279#endif 2285#endif
2280 } 2286 }
2281 while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); 2287 while (is_one_of (WHITESPACE, c));
2282 2288
2283 /* record it */ 2289 /* record it */
2284#if SHOW_ERROR_LINE 2290#if SHOW_ERROR_LINE
2285 if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) 2291 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; 2292 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line;
2316 return TOK_RPAREN; 2322 return TOK_RPAREN;
2317 2323
2318 case '.': 2324 case '.':
2319 c = inchar (SCHEME_A); 2325 c = inchar (SCHEME_A);
2320 2326
2321 if (is_one_of (" \n\t", c)) 2327 if (is_one_of (WHITESPACE, c))
2322 return TOK_DOT; 2328 return TOK_DOT;
2323 else 2329 else
2324 { 2330 {
2325 backchar (SCHEME_A_ c); 2331 backchar (SCHEME_A_ c);
2326 return TOK_DOTATOM; 2332 return TOK_DOTATOM;
2467 } 2473 }
2468 2474
2469 putcharacter (SCHEME_A_ '"'); 2475 putcharacter (SCHEME_A_ '"');
2470} 2476}
2471 2477
2472
2473/* print atoms */ 2478/* print atoms */
2474static void 2479static void
2475printatom (SCHEME_P_ pointer l, int f) 2480printatom (SCHEME_P_ pointer l, int f)
2476{ 2481{
2477 char *p; 2482 char *p;
2478 int len; 2483 int len;
2479 2484
2480 atom2str (SCHEME_A_ l, f, &p, &len); 2485 atom2str (SCHEME_A_ l, f, &p, &len);
2481 putchars (SCHEME_A_ p, len); 2486 putchars (SCHEME_A_ p, len);
2482} 2487}
2483
2484 2488
2485/* Uses internal buffer unless string pointer is already available */ 2489/* Uses internal buffer unless string pointer is already available */
2486static void 2490static void
2487atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2491atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2488{ 2492{
2649#endif 2653#endif
2650 } 2654 }
2651 else if (is_continuation (l)) 2655 else if (is_continuation (l))
2652 p = "#<CONTINUATION>"; 2656 p = "#<CONTINUATION>";
2653 else 2657 else
2658 {
2659#if USE_PRINTF
2660 p = SCHEME_V->strbuff;
2661 snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l));
2662#else
2654 p = "#<ERROR>"; 2663 p = "#<ERROR>";
2664#endif
2665 }
2655 2666
2656 *pp = p; 2667 *pp = p;
2657 *plen = strlen (p); 2668 *plen = strlen (p);
2658} 2669}
2659 2670
2929#endif /* USE_ALIST_ENV else */ 2940#endif /* USE_ALIST_ENV else */
2930 2941
2931ecb_inline void 2942ecb_inline void
2932new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2943new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2933{ 2944{
2945 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2934 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2946 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2935} 2947}
2936 2948
2937ecb_inline void 2949ecb_inline void
2938set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2950set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
3230 3242
3231#endif 3243#endif
3232 3244
3233#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3245#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3234 3246
3247#if 1
3248static int
3249debug (SCHEME_P_ int indent, pointer x)
3250{
3251 int c;
3252
3253 if (is_syntax (x))
3254 {
3255 printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x));
3256 return 8 + 8;
3257 }
3258
3259 if (x == NIL)
3260 {
3261 printf ("%*sNIL\n", indent, "");
3262 return 3;
3263 }
3264
3265 switch (type (x))
3266 {
3267 case T_INTEGER:
3268 printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x);
3269 return 32+8;
3270
3271 case T_SYMBOL:
3272 printf ("%*sS<%s>\n", indent, "", symname (x));
3273 return 24+8;
3274
3275 case T_CLOSURE:
3276 printf ("%*sS<%s>\n", indent, "", "closure");
3277 debug (SCHEME_A_ indent + 3, cdr(x));
3278 return 32 + debug (SCHEME_A_ indent + 3, car (x));
3279
3280 case T_PAIR:
3281 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x));
3282 c = debug (SCHEME_A_ indent + 3, car (x));
3283 c += debug (SCHEME_A_ indent + 3, cdr (x));
3284 return c + 1;
3285
3286 case T_PORT:
3287 printf ("%*sS<%s>\n", indent, "", "port");
3288 return 24+8;
3289
3290 case T_VECTOR:
3291 printf ("%*sS<%s>\n", indent, "", "vector");
3292 return 24+8;
3293
3294 case T_ENVIRONMENT:
3295 printf ("%*sS<%s>\n", indent, "", "environment");
3296 return 0 + debug (SCHEME_A_ indent + 3, car (x));
3297
3298 default:
3299 printf ("unhandled type %d\n", type (x));
3300 break;
3301 }
3302}
3303#endif
3304
3235static int 3305static int
3236opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3306opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3237{ 3307{
3238 pointer args = SCHEME_V->args; 3308 pointer args = SCHEME_V->args;
3239 pointer x, y; 3309 pointer x, y;
3240 3310
3241 switch (op) 3311 switch (op)
3242 { 3312 {
3313#if 1 //D
3314 case OP_DEBUG:
3315 printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8);
3316 printf ("\n");
3317 s_return (S_T);
3318#endif
3243 case OP_LOAD: /* load */ 3319 case OP_LOAD: /* load */
3244 if (file_interactive (SCHEME_A)) 3320 if (file_interactive (SCHEME_A))
3245 { 3321 {
3246 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); 3322 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))); 3323 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args)));
3370 } 3446 }
3371 else 3447 else
3372 s_return (SCHEME_V->code); 3448 s_return (SCHEME_V->code);
3373 3449
3374 case OP_E0ARGS: /* eval arguments */ 3450 case OP_E0ARGS: /* eval arguments */
3375 if (is_macro (SCHEME_V->value)) /* macro expansion */ 3451 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3376 { 3452 {
3377 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3453 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3378 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3454 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3379 SCHEME_V->code = SCHEME_V->value; 3455 SCHEME_V->code = SCHEME_V->value;
3380 s_goto (OP_APPLY); 3456 s_goto (OP_APPLY);
4377 s_return (vec); 4453 s_return (vec);
4378 } 4454 }
4379 4455
4380 case OP_VECLEN: /* vector-length */ 4456 case OP_VECLEN: /* vector-length */
4381 s_return (mk_integer (SCHEME_A_ veclength (x))); 4457 s_return (mk_integer (SCHEME_A_ veclength (x)));
4458
4459 case OP_VECRESIZE:
4460 vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args));
4461 s_return (x);
4382 4462
4383 case OP_VECREF: /* vector-ref */ 4463 case OP_VECREF: /* vector-ref */
4384 { 4464 {
4385 int index = ivalue_unchecked (cadr (args)); 4465 int index = ivalue_unchecked (cadr (args));
4386 4466
5208 5288
5209 case OP_CLOSUREP: /* closure? */ 5289 case OP_CLOSUREP: /* closure? */
5210 /* 5290 /*
5211 * Note, macro object is also a closure. 5291 * Note, macro object is also a closure.
5212 * Therefore, (closure? <#MACRO>) ==> #t 5292 * Therefore, (closure? <#MACRO>) ==> #t
5293 * (schmorp) well, obviously not, fix? TODO
5213 */ 5294 */
5214 s_retbool (is_closure (a)); 5295 s_retbool (is_closure (a));
5215 5296
5216 case OP_MACROP: /* macro? */ 5297 case OP_MACROP: /* macro? */
5217 s_retbool (is_macro (a)); 5298 s_retbool (is_macro (a));
5458 5539
5459/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5540/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5460static int 5541static int
5461syntaxnum (pointer p) 5542syntaxnum (pointer p)
5462{ 5543{
5463 const char *s = strvalue (car (p)); 5544 const char *s = strvalue (p);
5464 5545
5465 switch (strlength (car (p))) 5546 switch (strlength (p))
5466 { 5547 {
5467 case 2: 5548 case 2:
5468 if (s[0] == 'i') 5549 if (s[0] == 'i')
5469 return OP_IF0; /* if */ 5550 return OP_IF0; /* if */
5470 else 5551 else
5906# endif 5987# endif
5907 int fin; 5988 int fin;
5908 char *file_name = InitFile; 5989 char *file_name = InitFile;
5909 int retcode; 5990 int retcode;
5910 int isfile = 1; 5991 int isfile = 1;
5992 system ("ps v $PPID");//D
5911 5993
5912 if (argc == 2 && strcmp (argv[1], "-?") == 0) 5994 if (argc == 2 && strcmp (argv[1], "-?") == 0)
5913 { 5995 {
5914 xwrstr ("Usage: tinyscheme -?\n"); 5996 xwrstr ("Usage: tinyscheme -?\n");
5915 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 5997 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines