Changeset 281 for trunk/thune/rune

Show
Ignore:
Timestamp:
09/23/06 18:16:16 (2 years ago)
Author:
krobillard
Message:

Thune - Tokenize now counts eol in line comments.
Rune - Func/call no longer eats stack. Added if, if.else.

Location:
trunk/thune/rune
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/thune/rune/rune.c

    r280 r281  
    3131 
    3232 
     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 
    3341#define OR_TOC      ur_thread->toc 
    3442#define OR_BOC      ur_thread->cstack 
    3543 
    3644#define ur_funcFetch(cell)  cell->func.closureN 
     45#define ur_opFetch(cell)    cell->word.sel 
    3746#define ur_argc(cell)       cell->func.localArgs 
    3847#define ur_varc(cell)       cell->func.localVars 
     
    95104    ROP_DOES, 
    96105    ROP_REDUCE, 
     106    ROP_IF, 
     107    ROP_IF_ELSE, 
    97108    ROP_ADD, 
    98109    ROP_SUB, 
     
    158169 
    159170 
    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); } 
    161178 
    162179// Look ahead for infix operator. 
     
    221238            } 
    222239                UR_C_DEC; 
    223                 goto control_end; 
     240                goto control; 
    224241#endif 
    225242 
     
    227244                if( ur_evalLocal(OR_TOC) ) 
    228245                { 
    229                     val = UR_TOS; 
    230                     UR_TOS = UR_LF_BEG->localFrame.cell; 
     246                    // Return from function. 
     247                    cval = UR_LF_BEG->localFrame.cell; 
    231248                    UR_LF_POP; 
    232                     ur_copyCell(UR_TOS, *val); 
     249                    ur_copyCell(cval, *val); 
     250                    val = cval; 
     251                    UR_TOS = ur_s_prev(cval); 
    233252                } 
    234253                UR_C_DEC; 
    235                 goto control_end; 
     254                goto control; 
    236255        } 
    237256        goto unexpected_end; 
     
    257276            goto fetch_pc; 
    258277 
    259         //case UT_GETWORD: 
     278        case UT_GETWORD: 
     279            val = ur_wordCell( ur_thread, val ); 
     280            break; 
    260281 
    261282        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; 
    265287            break; 
    266288 
     
    278300                case ROP_REDUCE: 
    279301                    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; 
    280312                    goto fetch_pc; 
    281313 
     
    300332        case UT_CALL: 
    301333            if( ur_argc(val) == 0 ) 
    302                 goto call; 
     334            { 
     335                //UR_S_GROW;    // Calls without arguments must ( -- val). 
     336                goto do_call; 
     337            } 
    303338            PUSHC_CELL( val ); 
    304339            goto fetch_pc; 
     
    306341 
    307342control: 
    308 control_end: 
    309343 
    310344    // Here val points to current value (most often contents at pc). 
     
    348382                    goto control; 
    349383 
     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 
    350426                default: 
    351427                    // Infix operator. 
     
    353429                    val = INFIX_CALL( ur_opcode(cval) ); 
    354430                    UR_C_DEC; 
    355                     goto call; 
     431                    goto do_call; 
    356432            } 
    357433            goto bad_control; 
     
    366442            ur_copyCell( cval, *val /*UR_TOS*/ ); 
    367443            UR_C_DEC; 
    368             goto control_end; 
     444            goto control; 
    369445 
    370446        case UT_BLOCK: 
     
    373449 
    374450        case UT_PAREN: 
     451            // paren! type denotes block evaluation should resume. 
    375452            SET_BLK_PC( OR_TOC->series.n, OR_TOC->series.it ); 
    376453            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; 
    390455 
    391456        case UT_FUNCTION: 
    392457            LOOK_AHEAD_FOR_INFIX 
    393458            PUSH_VAL 
    394             if( --ur_funcFetch(OR_TOC) == 0 ) 
     459            si = ur_funcFetch(OR_TOC) - 1; 
     460            if( si == 0 ) 
    395461            { 
    396462                val = OR_TOC; 
     
    398464                goto call_func; 
    399465            } 
     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; 
    400481            goto fetch_pc; 
    401482    } 
     
    416497    if( pc >= end ) 
    417498    { 
    418         UR_C_DEC; 
     499        UR_C_DEC;   // TODO: Don't put on stack if empty. 
    419500        val = UR_TOS; 
    420501        ur_initType( val, UT_UNSET ); 
    421         goto control_end; 
     502        goto control; 
    422503    } 
    423504    goto fetch_pc_ok; 
    424505 
    425 call: 
     506do_call: 
     507 
     508    //printf( "KR call %ld\n", UR_TOS - ur_thread->dstack ); 
    426509 
    427510    val->call.addr( ur_thread, UR_TOS ); 
     511    val = UR_TOS; 
     512    UR_S_DROP; 
    428513 
    429514    switch( UR_CALL_OP ) 
     
    449534            goto fetch_pc; 
    450535    } 
    451     val = UR_TOS; 
    452536    goto control; 
    453537 
     
    496580    SET_BLK_PC( bodyN, 0 ); 
    497581    } 
    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    /* 
    499593    ur_initType(UR_TOS, UT_UNSET); 
    500594    val = UR_TOS; 
    501595    goto fetch_pc; 
     596    */ 
    502597 
    503598op_throw: 
     
    573668 
    574669 
     670/** 
     671   (val -- val) 
     672 
     673   Print value to stdout (without linefeed). 
     674*/ 
     675UR_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) 
     705UR_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 
    575723extern UIndex _funcSignature( const UCell* scell, int* pArgc, int* pVarc ); 
    576  
    577724 
    578725// (block -- ) 
     
    714861  "  2 does\n" 
    715862  "  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" 
    720869  "] make-opcodes :rune-ops\n" 
    721870  "[block! verify/1 rune-ops infuse code! swap 'rune make] proc :runec\n" 
     
    726875  "  mul    [a int!/decimal! b int!/decimal!]\n" 
    727876  "  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" 
    731881  "  to-text [val]\n" 
    732   "  reduce [val]\n" 
    733   "  .      [val]\n" 
     882  "  reduce  [val]\n" 
    734883  "] define-natives\n" 
    735   "console.out: :cout\n" 
    736884  "\n" 
    737885  ; 
     
    741889static UCallDef rune_calls[] = 
    742890{ 
     891    { rc_cout,         "cout" }, 
     892    { rc_probe,        "probe" }, 
    743893    { rc_defNatives,   "define-natives" }, 
    744894}; 
  • trunk/thune/rune/tests/eval.good

    r280 r281  
    2020--- Print --- 
    2121hi 
    22 unset 
     22"hi^/" 
     23--- Does 1 --- 
     24inside-does 
    2325--- Func 1 --- 
    242616 
    2527--- Func 2 --- 
    2628unset 
     29--- Func Local --- 
     30none 
     31none 
     32--- If --- 
     33<if true> 
     34<if 1> 
     35--- If Else --- 
     36no 
     37no 
     38yes 
     39yes 
     40--- Now --- 
     41Timing print string. 
     42time! 
  • trunk/thune/rune/tests/eval.t

    r280 r281  
    11[ 
    22 
    3 r.prin:  func [val] [console.out to-text val] 
    4 r.print: func [val] [console.out to-text val console.out eol] 
     3r.prin:  func [val] [cout to-text val] 
     4r.print: func [val] [cout to-text val cout eol] 
    55time-blk: func [blk | s] [s: now  r.do blk  now - s] 
    66 
    77tst: func [msg blk] [ 
    88    r.print msg 
    9     . r.do blk 
     9    probe r.do blk 
    1010] 
    1111 
     
    2828tst "--- Infix 3 ---" 
    2929    [add 1 2 * 3]       ;= 7 
    30 . 1 + 2 * 3             ;= 9 
    31 . mul add 1 2 3         ;= 9 
    32 ;. + 1 * 2 3             ;= 5 (Rune expects first value) 
    33 ;. 1 + * 2 3             ;= 7 (Rune expects first value) 
     30probe 1 + 2 * 3             ;= 9 
     31probe 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) 
    3434 
    3535tst "--- Paren ---" [8 + (5 * 2)] 
    3636 
    3737tst "--- Print ---" 
    38     [console.out "hi^/"] 
     38    [cout "hi^/"]           ; Rune returns value passed to cout. 
     39 
     40tst "--- Does 1 ---" 
     41    [x: does ['inside-does] x] 
    3942 
    4043tst "--- Func 1 ---" 
    41     [p2: func [x] [x * x]  nop p2 4] 
     44    [p2: func [x] [x * x]  p2 4] 
    4245 
    4346tst "--- Func 2 ---" 
    4447    [p2: func [] [] p2] 
    4548 
     49tst "--- Func Local ---" 
     50    [p2: func [| a] [probe a] p2] 
     51 
     52r.print "--- If ---" 
     53r.if none  [r.print "<if none>"] 
     54r.if false [r.print "<if false>"] 
     55r.if true  [r.print "<if true>"] 
     56r.if 1     [r.print "<if 1>"] 
     57 
     58r.print "--- If Else ---" 
     59probe r.if.else none  ['yes]['no] 
     60probe r.if.else false  'yes  'no  
     61probe r.if.else true  ['yes]['no] 
     62probe r.if.else 1      'yes  'no 
     63 
     64tst "--- Now ---" 
     65    [type? time-blk [r.print "Timing print string."]] 
     66 
    4667] rune 
  • trunk/thune/rune/tests/mandel.ru

    r280 r281  
    33/* 
    44  TODO 
    5    [ ] either 
    65   [ ] loop 
    76   [ ] loop.to 
    87   [ ] if 
     8   [ ] if.else 
    99   [ ] return 
    1010*/ 
     
    4343                loop.to 'x -39 39 
    4444        [ 
    45             prin either mandelbrot x / 40.0 y / 40.0 
     45            prin if.else mandelbrot x / 40.0 y / 40.0 
    4646                                ['*'][' '] 
    4747        ]