Changeset 282 for trunk/thune/rune
- Timestamp:
- 09/24/06 04:43:27 (2 years ago)
- Location:
- trunk/thune/rune
- Files:
-
- 4 modified
-
project.r (modified) (1 diff)
-
rune.c (modified) (20 diffs)
-
tests/eval.t (modified) (3 diffs)
-
tests/mandel.ru (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/thune/rune/project.r
r275 r282 5 5 default [ 6 6 warn 7 debug8 ;release7 ;debug 8 release 9 9 10 10 cflags {-DLANG_RUNE} -
trunk/thune/rune/rune.c
r281 r282 29 29 extern void uc_mul( void*, void* ); 30 30 extern void uc_div( void*, void* ); 31 extern void uc_gtQ( void*, void* ); 32 extern void uc_ltQ( void*, void* ); 31 33 32 34 … … 48 50 #define ur_evalLocal(cell) cell->series.encoding 49 51 52 #if 0 50 53 #define CC_SETWORD CC_END+1 51 54 #define CC_CALL CC_END+2 … … 56 59 #define CC_LEN_CALL 1 57 60 #define CC_LEN_CALL_FUNC 1 61 #endif 62 58 63 59 64 #define SET_BLK_PC(bn,it) \ … … 64 69 end = start + blk->used; 65 70 66 67 71 #define PUSHC_END \ 68 72 ur_initType(OR_TOC, UT_UNSET) 69 73 70 #define PUSHC_HOLD (blkn) \74 #define PUSHC_HOLD_BLK(blkn) \ 71 75 UR_C_GROW; \ 72 76 ur_initType(OR_TOC, UT_BLOCK); \ 73 77 ur_setSeries(OR_TOC, blkn, 0) 74 78 75 #define PUSHC_HOLD_FUNC(blkn, locals) \ 79 #define FLAG_FUNC_BLOCK 0x40 80 #define FLAG_LOOP 0x20 81 82 #define PUSHC_HOLD_FUNC(blkn, locals, loop) \ 76 83 UR_C_GROW; \ 77 84 ur_initType(OR_TOC, UT_BLOCK); \ 85 OR_TOC->id.flags |= (loop) ? FLAG_FUNC_BLOCK|FLAG_LOOP : FLAG_FUNC_BLOCK; \ 78 86 ur_evalLocal(OR_TOC) = locals; \ 79 87 ur_setSeries(OR_TOC, blkn, 0); … … 106 114 ROP_IF, 107 115 ROP_IF_ELSE, 116 ROP_FOREVER, 117 ROP_BREAK, 118 ROP_RETURN, 108 119 ROP_ADD, 109 120 ROP_SUB, 110 121 ROP_MUL, 111 ROP_DIV 122 ROP_DIV, 123 ROP_GT, 124 ROP_LT 112 125 }; 113 126 … … 120 133 {UT_CALL, 0, 2,0, 1,0, 0, uc_sub}, 121 134 {UT_CALL, 0, 2,0, 1,0, 0, uc_mul}, 122 {UT_CALL, 0, 2,0, 1,0, 0, uc_div} 135 {UT_CALL, 0, 2,0, 1,0, 0, uc_div}, 136 {UT_CALL, 0, 2,0, 1,0, 0, uc_gtQ}, 137 {UT_CALL, 0, 2,0, 1,0, 0, uc_ltQ} 123 138 }; 124 139 … … 203 218 204 219 PUSHC_END; 205 PUSHC_HOLD ( blkN );220 PUSHC_HOLD_BLK( blkN ); 206 221 207 222 fetch_pc: … … 213 228 switch( ur_type(OR_TOC) ) 214 229 { 215 #if 1230 #if 0 216 231 case UT_WORD: // Reduce 217 232 { … … 240 255 goto control; 241 256 #endif 242 243 257 case UT_BLOCK: 258 func_return: 244 259 if( ur_evalLocal(OR_TOC) ) 245 260 { … … 312 327 goto fetch_pc; 313 328 329 case ROP_FOREVER: 330 PUSHC_CELL( val ); 331 OR_TOC->word.valBlk = 0; 332 goto fetch_pc; 333 334 case ROP_BREAK: 335 goto op_break; 336 337 case ROP_RETURN: 338 PUSHC_CELL( val ); 339 goto fetch_pc; 340 314 341 case ROP_ADD: 315 342 case ROP_SUB: … … 348 375 case UT_UNSET: 349 376 goto finish; 350 377 #if 0 351 378 case UT_WORD: // Reduce 352 379 //UR_S_GROW; 353 380 goto fetch_pc; 354 381 #endif 355 382 case UT_OPCODE: 356 383 cval = OR_TOC; … … 424 451 goto fetch_pc; 425 452 453 case ROP_FOREVER: 454 if( OR_TOC->word.valBlk == 0 ) 455 { 456 if( ur_is(val, UT_BLOCK) || ur_is(val, UT_PAREN) ) 457 { 458 OR_TOC->word.valBlk = val->series.n; 459 goto do_block; 460 } 461 } 462 val = ur_s_next(UR_TOS); 463 ur_initType( val, UT_BLOCK ); 464 ur_setSeries( val, OR_TOC->word.valBlk, 0 ); 465 goto do_block; 466 467 case ROP_RETURN: 468 UR_C_DEC; 469 goto op_return; 470 426 471 default: 427 472 // Infix operator. … … 493 538 494 539 PUSHC_EVAL( blkN, pc - start ); 495 PUSHC_HOLD ( val->series.n );540 PUSHC_HOLD_BLK( val->series.n ); 496 541 SET_BLK_PC( val->series.n, val->series.it ); 497 542 if( pc >= end ) … … 540 585 { 541 586 UIndex bodyN; 587 int loop; 542 588 int totc = ur_varc(val); 543 589 if( totc ) … … 575 621 // PUSHC_EVAL can overwrite val here - must grab bodyN first. 576 622 bodyN = val->func.bodyN; 623 loop = val->func.flags & UR_FLAG_FUNC_LOOP; 577 624 578 625 PUSHC_EVAL( blkN, pc - start ); 579 PUSHC_HOLD_FUNC( bodyN, totc );626 PUSHC_HOLD_FUNC( bodyN, totc, loop ); 580 627 SET_BLK_PC( bodyN, 0 ); 581 628 } … … 590 637 goto fetch_pc_ok; 591 638 592 /* 593 ur_initType(UR_TOS, UT_UNSET); 594 val = UR_TOS; 595 goto fetch_pc; 596 */ 597 639 op_return: 640 641 cval = OR_TOC; 642 while( 1 ) 643 { 644 switch( ur_type(cval) ) 645 { 646 case UT_UNSET: 647 ur_throwErr( UR_ERR_SCRIPT, "return used outside of function" ); 648 goto trace_error; 649 650 case UT_BLOCK: 651 if( cval->id.flags & FLAG_FUNC_BLOCK ) 652 { 653 if( cval->id.flags & FLAG_LOOP ) 654 { 655 if( ur_evalLocal(cval) ) 656 UR_LF_POP; 657 break; 658 } 659 OR_TOC = cval; 660 goto func_return; 661 } 662 } 663 --cval; // UR_C_DEC 664 } 665 666 op_break: 598 667 op_throw: 599 668 669 cval = OR_TOC; 600 670 while( 1 ) 601 671 { 602 switch( UR_TOC[-1].id.code)603 { 604 case CC_END:672 switch( ur_type(cval) ) 673 { 674 case UT_UNSET: 605 675 UR_C_DEC; 606 676 // error( "throw not caught" ); 607 677 goto error; 608 678 609 case CC_SETWORD: 610 case CC_CALL: 611 UR_C_DECN( CC_LEN_CALL ); 679 case UT_OPCODE: 680 if( ur_opcode(cval) == ROP_FOREVER ) 681 { 682 ur_copyCell( cval, cval[1] ); 683 goto control; 684 } 612 685 break; 613 686 614 default: 615 goto bad_control; 616 } 617 } 618 687 case UT_BLOCK: 688 if( cval->id.flags & FLAG_FUNC_BLOCK ) 689 { 690 if( ur_evalLocal(OR_TOC) ) 691 UR_LF_POP; 692 } 693 break; 694 695 //default: goto bad_control; 696 } 697 --cval; // UR_C_DEC 698 } 619 699 620 700 finish: … … 721 801 722 802 803 extern void uc_func( UThread*, UCell* ); 804 805 UR_CALL( rc_func_loop ) 806 { 807 uc_func( ur_thread, tos ); 808 UR_TOS->func.flags |= UR_FLAG_FUNC_LOOP; 809 } 810 811 723 812 extern UIndex _funcSignature( const UCell* scell, int* pArgc, int* pVarc ); 724 813 … … 858 947 "[\n" 859 948 " 0 'nop\n" 860 " 1 r.do\n"949 " 1 'do\n" 861 950 " 2 does\n" 862 951 " 3 reduce\n" 863 " 4 r.if\n" 864 " 5 r.if.else\n" 865 " 6 +\n" 866 " 7 -\n" 867 " 8 *\n" 868 " 9 /\n" 952 " 4 'if\n" 953 " 5 'if.else\n" 954 " 6 'forever\n" 955 " 7 'break\n" 956 " 8 'return\n" 957 " 9 +\n" 958 " 10 -\n" 959 " 11 *\n" 960 " 12 /\n" 961 " 13 >\n" 962 " 14 <\n" 869 963 "] make-opcodes :rune-ops\n" 870 "[block! verify/1 rune-ops infuse code! swap 'rune make] proc :runec\n" 964 "[\n" 965 " 0 :prin\n" 966 " 0 :print\n" 967 " 0 :loop\n" 968 " 0 :loop.to\n" 969 "] context :rune-ctx\n" 970 "[\n" 971 " block! verify/1 rune-ops infuse rune-ctx bind\n" 972 " code! swap 'rune make\n" 973 "] proc :runec\n" 871 974 "[runec do] proc :rune\n" 872 975 "[\n" … … 879 982 " probe [val]\n" 880 983 " type? [val]\n" 984 " zero? [val]\n" 881 985 " to-text [val]\n" 882 986 " reduce [val]\n" 987 " func.loop [sig body]\n" 883 988 "] define-natives\n" 989 "[\n" 990 " prin: func [val] [cout to-text val]\n" 991 " print: func [val] [cout to-text val cout eol]\n" 992 " time-blk: func [blk | s] [s: now do blk now - s]\n" 993 "] rune\n" 884 994 "\n" 885 995 ; … … 891 1001 { rc_cout, "cout" }, 892 1002 { rc_probe, "probe" }, 1003 { rc_func_loop, "func.loop" }, 893 1004 { rc_defNatives, "define-natives" }, 894 1005 }; -
trunk/thune/rune/tests/eval.t
r281 r282 1 1 [ 2 3 r.prin: func [val] [cout to-text val]4 r.print: func [val] [cout to-text val cout eol]5 time-blk: func [blk | s] [s: now r.do blk now - s]6 7 2 tst: func [msg blk] [ 8 r.print msg9 probe r.do blk3 print msg 4 probe do blk 10 5 ] 11 6 … … 18 13 19 14 tst "--- Call 1 ---" [sub add 4 5 x: 1] 20 r.print x15 print x 21 16 22 17 tst "--- Infix 1 ---" [sub add 4 5 x: 1 + 2] 23 r.print x18 print x 24 19 25 20 tst "--- Infix 2 ---" [1 + sub add 4 5 x: 1 + 2] 26 r.print x21 print x 27 22 28 23 tst "--- Infix 3 ---" … … 50 45 [p2: func [| a] [probe a] p2] 51 46 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>"]47 print "--- If ---" 48 if none [print "<if none>"] 49 if false [print "<if false>"] 50 if true [print "<if true>"] 51 if 1 [print "<if 1>"] 57 52 58 r.print "--- If Else ---"59 probe r.if.else none ['yes]['no]60 probe r.if.else false 'yes 'no61 probe r.if.else true ['yes]['no]62 probe r.if.else 1 'yes 'no53 print "--- If Else ---" 54 probe if.else none ['yes]['no] 55 probe if.else false 'yes 'no 56 probe if.else true ['yes]['no] 57 probe if.else 1 'yes 'no 63 58 64 59 tst "--- Now ---" 65 [type? time-blk [ r.print "Timing print string."]]60 [type? time-blk [print "Timing print string."]] 66 61 67 62 ] rune 63 -
trunk/thune/rune/tests/mandel.ru
r281 r282 1 1 ; Rune version of http://www.timestretch.com/FractalBenchmark.html 2 [ 2 3 3 /* 4 TODO 5 [ ] loop 6 [ ] loop.to 7 [ ] if 8 [ ] if.else 9 [ ] return 10 */ 4 loop: func.loop [n int! body] [ 5 forever [n: n - 1 if zero? n [break] do body] 6 ] 7 11 8 12 9 BAILOUT: 16.0 … … 20 17 zr: 0.0 21 18 19 ;prin x prin ' ' print y 20 22 21 loop MAX_ITERATIONS 23 22 [ … … 28 27 zi: temp + temp + ci 29 28 30 if zi2 + zr2 > BAILOUT [return false] 29 ;prin zi2 prin ' ' print zr2 30 if zi2 + zr2 > BAILOUT [return nop false] 31 31 ] 32 32 true 33 33 ] 34 34 35 prin "Thune Elapsed: " 36 print time-blk 35 et: time-blk 37 36 [ 38 ;for 'y -39 39 1 39 loop.to 'y -39 39 37 ;loop [y -39 39] 38 y: -39 39 loop 79 40 40 [ 41 41 prin eol 42 ;for 'x -39 39 1 43 loop.to 'x -39 39 42 43 ;loop [x -39 39] 44 x: -39 45 loop 79 44 46 [ 45 prin if.else mandelbrot x / 40.0 y / 40.0 46 ['*'][' '] 47 prin if.else mandelbrot x / 40.0 y / 40.0 '*' ' ' 48 x: x + 1 47 49 ] 50 51 y: y + 1 48 52 ] 49 53 prin eol 50 54 ] 55 56 prin "Rune Elapsed: " print et 57 58 ] 59 rune
