Changeset 281 for trunk/thune/rune
- Timestamp:
- 09/23/06 18:16:16 (2 years ago)
- Location:
- trunk/thune/rune
- Files:
-
- 4 modified
-
rune.c (modified) (21 diffs)
-
tests/eval.good (modified) (1 diff)
-
tests/eval.t (modified) (2 diffs)
-
tests/mandel.ru (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/thune/rune/rune.c
r280 r281 31 31 32 32 33 #define CELL_TRUE(val) \ 34 ((ur_type(val) != UT_NONE) && \ 35 ((ur_type(val) != UT_LOGIC) || (ur_logic(val) != 0))) 36 37 #define CELL_FALSE(val) \ 38 (ur_is(val, UT_NONE) || (ur_is(val, UT_LOGIC) && (ur_logic(val) == 0))) 39 40 33 41 #define OR_TOC ur_thread->toc 34 42 #define OR_BOC ur_thread->cstack 35 43 36 44 #define ur_funcFetch(cell) cell->func.closureN 45 #define ur_opFetch(cell) cell->word.sel 37 46 #define ur_argc(cell) cell->func.localArgs 38 47 #define ur_varc(cell) cell->func.localVars … … 95 104 ROP_DOES, 96 105 ROP_REDUCE, 106 ROP_IF, 107 ROP_IF_ELSE, 97 108 ROP_ADD, 98 109 ROP_SUB, … … 158 169 159 170 160 #define PUSH_VAL if(val != UR_TOS) { UR_S_PUSH( *val ); } 171 #define PUSH_VAL \ 172 UR_S_GROW; \ 173 if(val != UR_TOS) \ 174 *UR_TOS = *val; 175 176 //#define PUSH_VAL if(val != UR_TOS) { UR_S_PUSH( *val ); } 177 //#define PLACE_VAL if(val != UR_TOS) { ur_copyCell(UR_TOS, *val); } 161 178 162 179 // Look ahead for infix operator. … … 221 238 } 222 239 UR_C_DEC; 223 goto control _end;240 goto control; 224 241 #endif 225 242 … … 227 244 if( ur_evalLocal(OR_TOC) ) 228 245 { 229 val = UR_TOS;230 UR_TOS= UR_LF_BEG->localFrame.cell;246 // Return from function. 247 cval = UR_LF_BEG->localFrame.cell; 231 248 UR_LF_POP; 232 ur_copyCell(UR_TOS, *val); 249 ur_copyCell(cval, *val); 250 val = cval; 251 UR_TOS = ur_s_prev(cval); 233 252 } 234 253 UR_C_DEC; 235 goto control _end;254 goto control; 236 255 } 237 256 goto unexpected_end; … … 257 276 goto fetch_pc; 258 277 259 //case UT_GETWORD: 278 case UT_GETWORD: 279 val = ur_wordCell( ur_thread, val ); 280 break; 260 281 261 282 case UT_LITWORD: 262 ur_copyCell(UR_TOS, *val); 263 val = UR_TOS; 264 ur_type(val) = UT_WORD; 283 cval = ur_s_next(UR_TOS); 284 ur_copyCell(cval, *val); 285 ur_type(cval) = UT_WORD; 286 val = cval; 265 287 break; 266 288 … … 278 300 case ROP_REDUCE: 279 301 PUSHC_CELL( val ); 302 goto fetch_pc; 303 304 case ROP_IF: 305 PUSHC_CELL( val ); 306 ur_opFetch( OR_TOC ) = 2; 307 goto fetch_pc; 308 309 case ROP_IF_ELSE: 310 PUSHC_CELL( val ); 311 ur_opFetch( OR_TOC ) = 3; 280 312 goto fetch_pc; 281 313 … … 300 332 case UT_CALL: 301 333 if( ur_argc(val) == 0 ) 302 goto call; 334 { 335 //UR_S_GROW; // Calls without arguments must ( -- val). 336 goto do_call; 337 } 303 338 PUSHC_CELL( val ); 304 339 goto fetch_pc; … … 306 341 307 342 control: 308 control_end:309 343 310 344 // Here val points to current value (most often contents at pc). … … 348 382 goto control; 349 383 384 case ROP_IF: 385 LOOK_AHEAD_FOR_INFIX 386 si = ur_opFetch(cval) - 1; 387 if( si == 0 ) 388 { 389 UR_C_DEC; 390 391 cval = UR_TOS; 392 UR_S_DROP; 393 if( CELL_FALSE( cval ) ) 394 goto control; 395 if( ur_is(val, UT_BLOCK) || ur_is(val, UT_PAREN) ) 396 goto do_block; 397 ur_copyCell( cval, *val ); 398 val = cval; 399 goto control; 400 } 401 ur_opFetch(cval) = si; 402 PUSH_VAL 403 goto fetch_pc; 404 405 case ROP_IF_ELSE: 406 LOOK_AHEAD_FOR_INFIX 407 si = ur_opFetch(cval) - 1; 408 if( si == 0 ) 409 { 410 UR_C_DEC; 411 412 cval = ur_s_prev(UR_TOS); 413 if( CELL_TRUE( cval ) ) 414 val = UR_TOS; 415 UR_S_DROPN( 2 ); 416 if( ur_is(val, UT_BLOCK) || ur_is(val, UT_PAREN) ) 417 goto do_block; 418 ur_copyCell( cval, *val ); 419 val = cval; 420 goto control; 421 } 422 ur_opFetch(cval) = si; 423 PUSH_VAL 424 goto fetch_pc; 425 350 426 default: 351 427 // Infix operator. … … 353 429 val = INFIX_CALL( ur_opcode(cval) ); 354 430 UR_C_DEC; 355 goto call;431 goto do_call; 356 432 } 357 433 goto bad_control; … … 366 442 ur_copyCell( cval, *val /*UR_TOS*/ ); 367 443 UR_C_DEC; 368 goto control _end;444 goto control; 369 445 370 446 case UT_BLOCK: … … 373 449 374 450 case UT_PAREN: 451 // paren! type denotes block evaluation should resume. 375 452 SET_BLK_PC( OR_TOC->series.n, OR_TOC->series.it ); 376 453 UR_C_DEC; 377 goto control_end; 378 379 case UT_CALL: 380 LOOK_AHEAD_FOR_INFIX 381 PUSH_VAL 382 //printf( "KR f %d\n", OR_TOC->call.fetch ); 383 if( --OR_TOC->call.fetch == 0 ) 384 { 385 val = OR_TOC; 386 UR_C_DEC; 387 goto call; 388 } 389 goto fetch_pc; 454 goto control; 390 455 391 456 case UT_FUNCTION: 392 457 LOOK_AHEAD_FOR_INFIX 393 458 PUSH_VAL 394 if( --ur_funcFetch(OR_TOC) == 0 ) 459 si = ur_funcFetch(OR_TOC) - 1; 460 if( si == 0 ) 395 461 { 396 462 val = OR_TOC; … … 398 464 goto call_func; 399 465 } 466 ur_funcFetch(OR_TOC) = si; 467 goto fetch_pc; 468 469 case UT_CALL: 470 LOOK_AHEAD_FOR_INFIX 471 //printf( "KR f %d\n", OR_TOC->call.fetch ); 472 PUSH_VAL 473 si = OR_TOC->call.fetch - 1; 474 if( si == 0 ) 475 { 476 val = OR_TOC; 477 UR_C_DEC; 478 goto do_call; 479 } 480 OR_TOC->call.fetch = si; 400 481 goto fetch_pc; 401 482 } … … 416 497 if( pc >= end ) 417 498 { 418 UR_C_DEC; 499 UR_C_DEC; // TODO: Don't put on stack if empty. 419 500 val = UR_TOS; 420 501 ur_initType( val, UT_UNSET ); 421 goto control _end;502 goto control; 422 503 } 423 504 goto fetch_pc_ok; 424 505 425 call: 506 do_call: 507 508 //printf( "KR call %ld\n", UR_TOS - ur_thread->dstack ); 426 509 427 510 val->call.addr( ur_thread, UR_TOS ); 511 val = UR_TOS; 512 UR_S_DROP; 428 513 429 514 switch( UR_CALL_OP ) … … 449 534 goto fetch_pc; 450 535 } 451 val = UR_TOS;452 536 goto control; 453 537 … … 496 580 SET_BLK_PC( bodyN, 0 ); 497 581 } 498 582 if( pc >= end ) 583 { 584 UR_C_DEC; // TODO: Don't put on stack if empty. 585 val = ur_s_next(UR_TOS); 586 ur_initType( val, UT_UNSET ); 587 goto control; 588 } 589 ur_initType(UR_TOS, UT_UNSET); 590 goto fetch_pc_ok; 591 592 /* 499 593 ur_initType(UR_TOS, UT_UNSET); 500 594 val = UR_TOS; 501 595 goto fetch_pc; 596 */ 502 597 503 598 op_throw: … … 573 668 574 669 670 /** 671 (val -- val) 672 673 Print value to stdout (without linefeed). 674 */ 675 UR_CALL( rc_cout ) 676 { 677 char* it; 678 char* end; 679 680 UR_CALL_UNUSED_TH 681 682 if( ur_is(tos, UT_CHAR) ) 683 { 684 putchar( ur_char(tos) ); 685 } 686 else if( ur_stringSlice(tos, &it, &end) && it ) 687 { 688 if( ur_encoding(tos) == UR_ENC_UTF16 ) 689 { 690 uint16_t* uit = (uint16_t*) it; 691 uint16_t* uend = (uint16_t*) end; 692 693 while( uit != uend ) 694 putchar( *uit++ ); 695 } 696 else 697 { 698 fwrite( it, 1, end - it, stdout ); 699 } 700 } 701 } 702 703 704 // (val -- val) 705 UR_CALL( rc_probe ) 706 { 707 UString* str; 708 str = ur_binPtr( ur_thread->callTempBinN ); 709 str->used = 0; 710 ur_toStr( tos, str, 0 ); 711 ur_arrayReserve( str, sizeof(char), str->used + 1 ); 712 str->ptr.c[ str->used++ ] = '\n'; 713 714 UR_S_GROW; 715 tos = UR_TOS; 716 ur_initType( tos, UT_STRING ); 717 ur_setSeries( tos, ur_thread->callTempBinN, 0 ); 718 rc_cout( ur_thread, tos ); 719 UR_S_DROP; 720 } 721 722 575 723 extern UIndex _funcSignature( const UCell* scell, int* pArgc, int* pVarc ); 576 577 724 578 725 // (block -- ) … … 714 861 " 2 does\n" 715 862 " 3 reduce\n" 716 " 4 +\n" 717 " 5 -\n" 718 " 6 *\n" 719 " 7 /\n" 863 " 4 r.if\n" 864 " 5 r.if.else\n" 865 " 6 +\n" 866 " 7 -\n" 867 " 8 *\n" 868 " 9 /\n" 720 869 "] make-opcodes :rune-ops\n" 721 870 "[block! verify/1 rune-ops infuse code! swap 'rune make] proc :runec\n" … … 726 875 " mul [a int!/decimal! b int!/decimal!]\n" 727 876 " div [a int!/decimal! b int!/decimal!]\n" 728 " console.out [val]\n" 729 " func [sig block! body block!]\n" 730 " type? [val]\n" 877 " func [sig block! body block!]\n" 878 " cout [val]\n" 879 " probe [val]\n" 880 " type? [val]\n" 731 881 " to-text [val]\n" 732 " reduce [val]\n" 733 " . [val]\n" 882 " reduce [val]\n" 734 883 "] define-natives\n" 735 "console.out: :cout\n"736 884 "\n" 737 885 ; … … 741 889 static UCallDef rune_calls[] = 742 890 { 891 { rc_cout, "cout" }, 892 { rc_probe, "probe" }, 743 893 { rc_defNatives, "define-natives" }, 744 894 }; -
trunk/thune/rune/tests/eval.good
r280 r281 20 20 --- Print --- 21 21 hi 22 unset 22 "hi^/" 23 --- Does 1 --- 24 inside-does 23 25 --- Func 1 --- 24 26 16 25 27 --- Func 2 --- 26 28 unset 29 --- Func Local --- 30 none 31 none 32 --- If --- 33 <if true> 34 <if 1> 35 --- If Else --- 36 no 37 no 38 yes 39 yes 40 --- Now --- 41 Timing print string. 42 time! -
trunk/thune/rune/tests/eval.t
r280 r281 1 1 [ 2 2 3 r.prin: func [val] [co nsole.out to-text val]4 r.print: func [val] [co nsole.out to-text val console.out eol]3 r.prin: func [val] [cout to-text val] 4 r.print: func [val] [cout to-text val cout eol] 5 5 time-blk: func [blk | s] [s: now r.do blk now - s] 6 6 7 7 tst: func [msg blk] [ 8 8 r.print msg 9 .r.do blk9 probe r.do blk 10 10 ] 11 11 … … 28 28 tst "--- Infix 3 ---" 29 29 [add 1 2 * 3] ;= 7 30 .1 + 2 * 3 ;= 931 .mul add 1 2 3 ;= 932 ; .+ 1 * 2 3 ;= 5 (Rune expects first value)33 ; .1 + * 2 3 ;= 7 (Rune expects first value)30 probe 1 + 2 * 3 ;= 9 31 probe mul add 1 2 3 ;= 9 32 ;probe + 1 * 2 3 ;= 5 (Rune expects first value) 33 ;probe 1 + * 2 3 ;= 7 (Rune expects first value) 34 34 35 35 tst "--- Paren ---" [8 + (5 * 2)] 36 36 37 37 tst "--- Print ---" 38 [console.out "hi^/"] 38 [cout "hi^/"] ; Rune returns value passed to cout. 39 40 tst "--- Does 1 ---" 41 [x: does ['inside-does] x] 39 42 40 43 tst "--- Func 1 ---" 41 [p2: func [x] [x * x] nopp2 4]44 [p2: func [x] [x * x] p2 4] 42 45 43 46 tst "--- Func 2 ---" 44 47 [p2: func [] [] p2] 45 48 49 tst "--- Func Local ---" 50 [p2: func [| a] [probe a] p2] 51 52 r.print "--- If ---" 53 r.if none [r.print "<if none>"] 54 r.if false [r.print "<if false>"] 55 r.if true [r.print "<if true>"] 56 r.if 1 [r.print "<if 1>"] 57 58 r.print "--- If Else ---" 59 probe r.if.else none ['yes]['no] 60 probe r.if.else false 'yes 'no 61 probe r.if.else true ['yes]['no] 62 probe r.if.else 1 'yes 'no 63 64 tst "--- Now ---" 65 [type? time-blk [r.print "Timing print string."]] 66 46 67 ] rune -
trunk/thune/rune/tests/mandel.ru
r280 r281 3 3 /* 4 4 TODO 5 [ ] either6 5 [ ] loop 7 6 [ ] loop.to 8 7 [ ] if 8 [ ] if.else 9 9 [ ] return 10 10 */ … … 43 43 loop.to 'x -39 39 44 44 [ 45 prin eithermandelbrot x / 40.0 y / 40.045 prin if.else mandelbrot x / 40.0 y / 40.0 46 46 ['*'][' '] 47 47 ]
