Changeset 152

Show
Ignore:
Timestamp:
05/14/06 02:10:45 (3 years ago)
Author:
krobillard
Message:

Thune -
'do now dispatches function!, call!, & code!
'as handles all word types.
'index? returns atom if TOS is a word.
Started implementing 'foreach.
true & false are now infused.
Fixed unitialized word.wordBlk and leak in ur_tokenize().

Location:
trunk/thune
Files:
12 modified

Legend:

Unmodified
Added
Removed
  • trunk/thune/boot.c

    r132 r152  
    22static char _bootScript[] = 
    33  "context! [\n" 
     4  "  logic! 1 make :true\n" 
     5  "  logic! 0 make :false\n" 
    46  "  opcode!  0 'nop     make :nop\n" 
    57  "  opcode!  3 'drop    make :drop\n" 
     
    2931  "  opcode! 27 'verify.2  make :verify.2\n" 
    3032  "  opcode! 28 'verify.3  make :verify.3\n" 
     33  "  opcode! 30 'foreach   make :foreach\n" 
    3134  "] make :urlan-ops\n" 
    3235; 
    3336 
    3437static char _envScript[] = 
    35   "logic! 1 make dup dup :true :yes :on\n" 
    36   "logic! 0 make dup dup :false :no :off\n" 
     38  "logic! 1 make dup :yes :on\n" 
     39  "logic! 0 make dup :no :off\n" 
    3740  "char! 10 make :eol\n" 
    3841  "[quit] proc :q\n" 
     
    4245  "[read to-block urlan-ops infuse] proc :load\n" 
    4346  "[select do] proc :case\n" 
    44   ";(block cond -- )\n" 
    4547  "[\n" 
    4648  "  dup do iff (drop drop return)\n" 
    4749  "  over do recurse\n" 
    4850  "]\n" 
    49   "proc :while\n" 
     51  "proc :while  ;(block cond -- )\n" 
     52  "[\n" 
     53  "  dup zero? ift (drop drop return)\n" 
     54  "  over do 1 sub recurse\n" 
     55  "]\n" 
     56  "proc :loop   ;(block count -- )\n" 
    5057  "[error! swap make throw] proc :error\n" 
    51   "; (env body -- proc)\n" 
    52   "[swap context bind proc] proc :proc.env\n" 
    53   "; (data rules -- pos)\n" 
    54   "[[some none] 2 poke parse] proc :parse.some\n" 
     58  "[swap context bind proc] proc :proc.env      ;(env body -- proc)\n" 
     59  "[[some none] 2 poke parse] proc :parse.some  ;(data rules -- pos)\n" 
    5560  "3.14159265358979323846 :pi\n" 
    5661  "[\n" 
  • trunk/thune/context.c

    r129 r152  
    168168 
    169169    ur_setType( it, UT_WORD ); 
    170     //it->word.wordBlk = ctx->ctx.wordBlk; 
     170    it->word.wordBlk = 0;       //ctx->ctx.wordBlk; 
    171171    it->word.valBlk  = ctx->ctx.valBlk; 
    172172    it->word.index   = wrdN; 
     
    495495    while( it != end ) 
    496496    { 
    497         if( ur_is(it, UT_WORD) || 
    498             ur_is(it, UT_SETWORD) || 
    499             ur_is(it, UT_GETWORD) || 
    500             ur_is(it, UT_LITWORD) || 
    501             ur_is(it, UT_OPCODE) ) 
     497        if( ur_isAWord(it) || ur_is(it, UT_OPCODE) || ur_is(it, UT_SELECTOR) ) 
    502498        { 
    503499            _bindWord( it ); 
     
    506502        { 
    507503            UCell* path1 = ur_blockPtr( it->series.n )->ptr.cells; 
    508             if( ur_is(path1, UT_WORD) || 
    509                 ur_is(path1, UT_SETWORD) || 
    510                 ur_is(path1, UT_GETWORD) ) 
     504            if( ur_isAWord(path1) ) 
    511505            { 
    512506                _bindWord( path1 ); 
  • trunk/thune/debugger/ThuneDebugger.cpp

    r114 r152  
    3131 
    3232    _editor = new QTextEdit; 
    33     _editor->setTabStopWidth( 4 ); 
     33    _editor->setTabStopWidth( 8 * 4 /*_editor->fontMetrics().height() * 3*/ ); 
    3434    new ThuneHighlight(_editor->document()); 
    3535    setCentralWidget(_editor); 
  • trunk/thune/doc/UserManual

    r132 r152  
    1717 
    1818   * Small binary & run-time enviroment. 
    19    * Written in C to work well with existing systems. 
     19   * Written in C to work well as an embedded scripting language. 
    2020   * Stack based evaluator. 
    2121   * Garbage collected datatype system with prototype based objects. 
     
    6969string!     "hello"  {hello} 
    7070binary!     #{01afed}  #{00 33 ff a0} 
     71selector!   obj/x my-array/2 
    7172slice! 
    7273context! 
     
    212213========  =========================  ============================ 
    213214proc.env  (env body -- proc)         Create procedure with context. 
     215========  =========================  ============================ 
     216 
     217 
     218Contexts 
     219-------- 
     220 
     221==========  ========================  ================= 
     222Word        Stack Usage               Function 
     223==========  ========================  ================= 
     224set         (val word -- )            Assign value to word. 
     225get         (word -- val)             Retrieve value referenced by word. 
     226==========  ========================  ================= 
     227 
     228 
     229Context Helpers 
     230~~~~~~~~~~~~~~~ 
     231 
     232========  =========================  ============================ 
     233Word      Stack Usage                Function 
     234========  =========================  ============================ 
     235context   (def -- context)           Create context. 
    214236========  =========================  ============================ 
    215237 
  • trunk/thune/eval.c

    r141 r152  
    8686    UR_C_GROW 
    8787 
    88 #define POPC_ITER(a,b) \ 
     88#define RESET_ITER(a,b) \ 
    8989    b = UR_TOC->cp.cell; \ 
    90     UR_C_DEC; \ 
    91     a = UR_TOC->cp.cell 
    92  
    93 #define RESET_ITER(a,b) \ 
     90    a = UR_TOC[-1].cp.cell; \ 
     91    UR_C_GROW 
     92 
     93 
     94#define PUSHC_FOREACH(si,a,b) \ 
     95    UR_TOC->cp.cell = a; \ 
     96    UR_C_GROW; \ 
     97    UR_TOC->cp.code = CC_FOREACH; \ 
     98    UR_TOC->cp.n    = si; \ 
     99    UR_TOC->cp.cell = b; \ 
     100    UR_C_GROW 
     101 
     102#define RESET_FOREACH(a,b) \ 
    94103    b = UR_TOC->cp.cell; \ 
    95104    a = UR_TOC[-1].cp.cell; \ 
     
    247256        case UT_VEC3: 
    248257        { 
    249             int n; 
    250  
     258            unsigned int n; 
    251259            n = ur_selector(sel) - (ur_selIsAtom(sel) ? UR_ATOM_X : 1); 
    252  
    253             ur_setType( res, UT_DECIMAL ); 
    254             ur_decimal(res) = (double) val->vec3.xyz[ n & 3 ]; 
     260            if( n < 3 ) 
     261            { 
     262                ur_setType( res, UT_DECIMAL ); 
     263                ur_decimal(res) = (double) val->vec3.xyz[ n ]; 
     264            } 
     265            else 
     266            { 
     267                ur_setNone( res ); 
     268            } 
    255269            return 1; 
    256270        } 
     
    283297 
    284298            ur_wordCell( tos, blk, val ); 
    285             ur_copyCell(tos, *val ); 
     299 
     300            switch( ur_type(val) ) 
     301            { 
     302                case UT_WORD: 
     303                case UT_LITWORD: 
     304                    ur_wordCell( val, blk, val ); 
     305                    ur_copyCell( tos, *val ); 
     306                    break; 
     307 
     308                case UT_FUNCTION: 
     309                    ur_copyCell( tos, *val ); 
     310                    UR_CALL_OP = OP_DO_FUNC; 
     311                    break; 
     312 
     313                case UT_CALL: 
     314                    UR_S_DROP; 
     315                    val->call.addr( ur_thread, UR_TOS ); 
     316                    break; 
     317 
     318                case UT_CODE: 
     319                    ur_copyCell( tos, *val ); 
     320                    ur_language(val)->run( ur_thread, tos ); 
     321                    break; 
     322 
     323                default: 
     324                    ur_copyCell( tos, *val ); 
     325                    break; 
     326            } 
    286327        } 
    287328            break; 
     
    321362            break; 
    322363 
     364        case UT_CALL: 
     365            UR_S_DROP; 
     366            tos->call.addr( ur_thread, UR_TOS ); 
     367            break; 
     368 
    323369        case UT_CODE: 
    324370            ur_language(tos)->run( ur_thread, tos ); 
    325371            break; 
    326     } 
    327     // else leave tos unchanged... 
     372 
     373        // default: leave tos unchanged... 
     374    } 
    328375} 
    329376 
     
    349396 
    350397        ur_wordCell( tos, blk, val ); 
    351         ur_copyCell(tos, *val ); 
     398        ur_copyCell( tos, *val ); 
    352399    } 
    353400    // else leave tos unchanged... 
     
    555602                        } 
    556603                        break; 
     604 
     605                    case OP_FOREACH:        // (blk series ['words] -- ) 
     606                        val = UR_TOS; 
     607                        while( ur_is(val, UT_LITWORD) ) 
     608                            ++val; 
     609                        PUSHC_EVAL( blkN, start, pc + 1 ); 
     610                        SET_BLK_PC( val[1].series.n, 
     611                                    val[1].series.it ); 
     612                        PUSHC_FOREACH( UR_BOS - val, pc, end ); 
     613                        goto control; 
    557614 
    558615                    case OP_RECURSE: 
     
    851908                goto execute; 
    852909            } 
    853             POPC_ITER( pc, end ); 
     910            UR_C_DEC; 
    854911            UR_S_DROP;           // Pop iterator value. 
     912            goto control; 
     913 
     914        case CC_FOREACH: 
     915            val = UR_BOS - UR_TOC->cp.n; 
     916            if( ! ur_atTail( val ) ) 
     917            { 
     918                UR_TOS->series.it++; 
     919                RESET_FOREACH( pc, end ); 
     920                UR_S_DUP; 
     921                goto execute; 
     922            } 
     923            UR_C_DEC; 
     924            UR_S_DROPN( UR_TOC->cp.n );     // Pop iterator values. 
    855925            goto control; 
    856926 
     
    911981 
    912982            case CC_ITER: 
     983            case CC_FOREACH: 
    913984                UR_C_DECN( CC_LEN_ITER); 
    914985                break; 
     
    9501021            case CC_EVAL_RUNNING: 
    9511022                            UR_C_DECN( CC_LEN_EVAL );  break; 
    952             case CC_ITER:   UR_C_DECN( CC_LEN_ITER );  break; 
     1023            case CC_ITER: 
     1024            case CC_FOREACH: 
     1025                            UR_C_DECN( CC_LEN_ITER );  break; 
    9531026            case CC_CATCH:  UR_C_DECN( CC_LEN_CATCH ); break; 
    9541027 
     
    9731046            case CC_EVAL_RUNNING: 
    9741047                            UR_C_DECN( CC_LEN_EVAL );  break; 
    975             case CC_ITER:   UR_C_DECN( CC_LEN_ITER );  break; 
     1048            case CC_ITER: 
     1049            case CC_FOREACH: 
     1050                            UR_C_DECN( CC_LEN_ITER );  break; 
    9761051            case CC_CATCH:  UR_C_DECN( CC_LEN_CATCH ); break; 
    9771052 
     
    13961471UR_CALL( uc_set ) 
    13971472{ 
    1398     if( ur_is(tos, UT_LITWORD) ) 
     1473    if( ur_isAWord(tos) ) 
    13991474    { 
    14001475        UBlock* blk; 
     
    14521527    { 
    14531528        case UT_WORD: 
    1454             if( ur_is(val, UT_LITWORD) ) 
    1455                 ur_type(val) = UT_WORD; 
    1456             break; 
    1457  
     1529        case UT_SETWORD: 
     1530        case UT_GETWORD: 
    14581531        case UT_LITWORD: 
    1459             if( ur_is(val, UT_WORD) ) 
    1460                 ur_type(val) = UT_LITWORD; 
     1532            if( ur_isAWord(val) ) 
     1533                ur_type(val) = ur_datatype(tos); 
    14611534            break; 
    14621535 
  • trunk/thune/gc.c

    r117 r152  
    217217            case UT_SETWORD: 
    218218            //case UT_LITWORD:      // Only atom is used in lit-words? 
    219                 SET_BIT_BLOCK( it->word.wordBlk ); 
     219                if( it->word.wordBlk ) 
     220                { 
     221                    SET_BIT_BLOCK( it->word.wordBlk ); 
     222                } 
    220223                SET_BIT_BLOCK( it->word.valBlk ); 
    221224                break; 
     
    401404 
    402405            case CC_ITER: 
     406            case CC_FOREACH: 
    403407                it -= CC_LEN_ITER; 
    404408                break; 
  • trunk/thune/internal.h

    r109 r152  
    111111#define CC_EVAL_RUNNING     2 
    112112#define CC_ITER             3 
    113 #define CC_CATCH            4 
    114 #define CC_END              5 
     113#define CC_FOREACH          4 
     114#define CC_CATCH            5 
     115#define CC_END              6 
    115116 
    116117//#define CC_LEN_FUNC         1  // + argc 
    117118#define CC_LEN_EVAL         1 
    118119#define CC_LEN_ITER         2 
     120#define CC_LEN_FOREACH      2 
    119121#define CC_LEN_CATCH        3 
    120122#define CC_LEN_END          1 
     
    176178#define OP_VERIFY_3         28 
    177179#define OP_DO_FUNC          29 
     180#define OP_FOREACH          30 
    178181//#define OP_END              255 
    179182 
  • trunk/thune/make.c

    r141 r152  
    605605    cell = ur_appendCell( blk, type ); 
    606606 
    607     //cell->word.wordBlk = GLOBAL_WORD_BLKN; 
     607    cell->word.wordBlk = 0;     //GLOBAL_WORD_BLKN; 
    608608    cell->word.valBlk  = GLOBAL_VAL_BLKN; 
    609609    //cell->word.index   = -1; 
  • trunk/thune/print.c

    r141 r152  
    10011001            break; 
    10021002 
    1003 #ifdef LANG_ORCA 
    1004         case UT_NATIVE: 
    1005         { 
    1006             UCell bval; 
    1007             append(out, "native ", 7); 
    1008  
    1009             bval.type      = UT_BLOCK; 
    1010             bval.flags     = 0; 
    1011             bval.series.n  = val->func.specBlk; 
    1012             bval.series.it = 0; 
    1013             ur_toStr( &bval, out, depth ); 
    1014         } 
    1015             break; 
    1016 #endif 
     1003        case UT_CALL: 
     1004            append(out, "*call*", 6); 
     1005            break; 
    10171006 
    10181007        case UT_FUNCTION: 
  • trunk/thune/series.c

    r131 r152  
    309309        ur_setType(tos, UT_INT); 
    310310        ur_int(tos) = tos->series.it + 1; 
     311    } 
     312    else if( ur_isAWord(tos) ) 
     313    { 
     314        ur_setType(tos, UT_INT); 
     315        ur_int(tos) = ur_atom(tos); 
    311316    } 
    312317    else 
  • trunk/thune/tests/working/control.t

    r103 r152  
    22 
    330 :n 
    4 [n dup . 1 add :n] 
    5 [n 3 lt?] while 
     4[n dup . 1 add :n] [n 3 lt?] while 
    65"done" print 
    76 
    87 
    9 ; (block count -- ) 
    10 [ 
    11     dup zero? ift (drop drop return) 
    12     over do 1 sub recurse 
    13 ] 
    14 proc :loop 
    15  
    1680 :n 
    17 [n dup . 1 add :n] 
    18 10 loop 
     9[n dup . 1 add :n] 10 loop 
    1910"done" print 
    2011 
  • trunk/thune/tokenize.c

    r141 r152  
    533533 
    534534    ur_release( hold ); 
    535     return STACK[0]; 
     535    hold = STACK[0]; 
     536    ur_arrayFree( &stack ); 
     537    return hold; 
    536538} 
    537539