Changeset 168

Show
Ignore:
Timestamp:
06/05/06 03:23:01 (3 years ago)
Author:
krobillard
Message:

Thune -

'set can now assign a block of words from a series.
Implemented 'foreach as a function.
Boot.c is now built from mkboot.t script.

Location:
trunk/thune
Files:
2 added
10 modified

Legend:

Unmodified
Added
Removed
  • trunk/thune/boot.c

    r164 r168  
     1/* Generated by mkboot.t */ 
    12 
    23static char _bootScript[] = 
     
    2930  "  opcode! 25 'dec     make :dec\n" 
    3031  "  opcode! 26 'verify  make :verify\n" 
    31   "  opcode! 30 'foreach make :foreach\n" 
    3232  "] make :urlan-ops\n" 
    3333; 
     
    5454  "]\n" 
    5555  "proc :loop   ;(block count -- )\n" 
     56  "[| body words len-1]\n" 
     57  "[\n" 
     58  "   block! block! verify/2\n" 
     59  "   :body :words\n" 
     60  "   words [first word! verify drop] iter\n" 
     61  "   words length?\n" 
     62  "   dup dec :len-1\n" 
     63  "   [\n" 
     64  "     1  [words set            body do]\n" 
     65  "     2  [words set next       body do]\n" 
     66  "        [words set len-1 skip body do]\n" 
     67  "   ] select\n" 
     68  "   iter\n" 
     69  "]\n" 
     70  "func :foreach       ; (ser words body -- )\n" 
    5671  "[error! swap make throw] proc :error\n" 
    5772  "[swap context bind proc] proc :proc.env      ;(env body -- proc)\n" 
     
    5974  "3.14159265358979323846 :pi\n" 
    6075  "[\n" 
    61   "  {0.0.1} :version\n" 
     76  "  \"0.0.1\" :version\n" 
    6277  "  none :os\n" 
    6378  "] context :env\n" 
    6479  "[now swap do now swap sub] proc :time-blk\n" 
    6580; 
    66  
    67  
    68 /* 
    69   "quit: :q\n" 
    70   "none!  0 make :none\n" 
    71   "logic! 1 make [true yes on] set\n" 
    72   "logic! 0 make [false no off] set\n" 
    73   "[\n" 
    74 */ 
  • trunk/thune/doc/UserManual

    r164 r168  
    189189while     (body cond -- )            Evaluate body while cond is true. 
    190190loop      (block n -- )              Repeat block **n** times. 
    191 iter      (ser block -- )            Iterate over series. 
     191iter      (ser body -- )             Iterate over series. 
     192foreach   (ser words body -- )       Iterate over series and assign elements. 
    192193proc      (body -- )                 Create procedure. 
    193194func      (sig body -- )             Create function with local values. 
     
    285286prev        (ser -- ser)            Decrement start of series. 
    286287next        (ser -- ser)            Increment start of series. 
     288skip        (ser n -- ser)          Increment start of series by **n**. 
    287289slice.prev  (slice -- slice)        Decrement end of slice. 
    288290slice.next  (slice -- slice)        Increment end of slice. 
  • trunk/thune/eval.c

    r164 r168  
    8989 
    9090#define RESET_ITER(a,b) \ 
    91     b = UR_TOC->cp.cell; \ 
    92     a = UR_TOC[-1].cp.cell; \ 
    93     UR_C_GROW 
    94  
    95  
    96 #define PUSHC_FOREACH(si,a,b) \ 
    97     UR_TOC->cp.cell = a; \ 
    98     UR_C_GROW; \ 
    99     UR_TOC->cp.code = CC_FOREACH; \ 
    100     UR_TOC->cp.n    = si; \ 
    101     UR_TOC->cp.cell = b; \ 
    102     UR_C_GROW 
    103  
    104 #define RESET_FOREACH(a,b) \ 
    10591    b = UR_TOC->cp.cell; \ 
    10692    a = UR_TOC[-1].cp.cell; \ 
     
    506492 
    507493            case CC_ITER: 
    508             case CC_FOREACH: 
    509494                 toc -= CC_LEN_ITER; 
    510495                 break; 
     
    665650                        } 
    666651                        break; 
    667  
    668                     case OP_FOREACH:        // (blk series ['words] -- ) 
    669                         ++pc; 
    670                         val = UR_TOS; 
    671                         while( ur_is(val, UT_LITWORD) ) 
    672                             ++val; 
    673                         PUSHC_EVAL( blkN, start, pc ); 
    674                         SET_BLK_PC( val[1].series.n, 
    675                                     val[1].series.it ); 
    676                         PUSHC_FOREACH( UR_BOS - val, pc, end ); 
    677                         goto control; 
    678652 
    679653                    case OP_RECURSE: 
     
    975949            goto control; 
    976950 
    977         case CC_FOREACH: 
    978             val = UR_BOS - UR_TOC->cp.n; 
    979             if( ur_itLen(val) > 0 ) 
    980             { 
    981                 UR_TOS->series.it++; 
    982                 RESET_FOREACH( pc, end ); 
    983                 UR_S_DUP; 
    984                 goto execute; 
    985             } 
    986             UR_C_DEC; 
    987             UR_S_DROPN( UR_TOC->cp.n );     // Pop iterator values. 
    988             goto control; 
    989  
    990951        case CC_CATCH: 
    991952            UR_C_DECN( 2 ); 
     
    10441005 
    10451006            case CC_ITER: 
    1046             case CC_FOREACH: 
    10471007                UR_C_DECN( CC_LEN_ITER ); 
    10481008                break; 
     
    13121272            break; 
    13131273 
     1274        case UT_CHAR: 
    13141275        case UT_INT: 
    1315             if( ur_is(b, UT_INT) ) 
     1276            if( ur_is(b, UT_INT) || ur_is(b, UT_CHAR) ) 
    13161277            { 
    13171278                if( ur_int(a) == ur_int(b) ) 
     
    15411502 
    15421503 
    1543 // (val word -- ) 
     1504/* 
     1505  (val word -- ) 
     1506 
     1507  If val is a series and word is a block then the words are set to the 
     1508  first N elements of the series (where N is the number of words in word). 
     1509*/ 
    15441510UR_CALL( uc_set ) 
    15451511{ 
     1512    UBlock* blk; 
     1513    UCell* cell; 
     1514    UCell* val; 
     1515 
     1516    val = ur_s_prev( tos ); 
     1517 
    15461518    if( ur_isAWord(tos) ) 
    15471519    { 
    1548         UBlock* blk; 
    1549         UCell* cell; 
    1550         UCell* val; 
    1551  
    15521520        if( ur_wordIsUnbound(tos) ) 
    15531521        { 
     
    15631531        } 
    15641532 
    1565         val = ur_s_prev( tos ); 
    15661533        ur_wordCell( tos, blk, cell ); 
    15671534        ur_copyCell( cell, *val ); 
     1535    } 
     1536    else if( ur_is(tos, UT_BLOCK) ) 
     1537    { 
     1538        UCell* it; 
     1539        UCell* end; 
     1540 
     1541        blk = ur_block(tos); 
     1542        UR_ITER_BLOCK( it, end, blk, tos ); 
     1543 
     1544        if( ur_isASeries(val) ) 
     1545        { 
     1546            int n = 0; 
     1547            while( it != end ) 
     1548            { 
     1549                if( ur_isAWord(it) ) 
     1550                { 
     1551                    ur_wordCell( it, blk, cell ); 
     1552                    ur_pick( val, n++, cell ); 
     1553                } 
     1554                ++it; 
     1555            } 
     1556        } 
     1557        else 
     1558        { 
     1559            while( it != end ) 
     1560            { 
     1561                if( ur_isAWord(it) ) 
     1562                { 
     1563                    ur_wordCell( it, blk, cell ); 
     1564                    ur_copyCell( cell, *val ); 
     1565                } 
     1566                ++it; 
     1567            } 
     1568        } 
    15681569    } 
    15691570    UR_S_DROPN(2); 
  • trunk/thune/gc.c

    r162 r168  
    404404 
    405405            case CC_ITER: 
    406             case CC_FOREACH: 
    407406                SET_BIT_BLOCK( it->cp.n ); 
    408407                it -= CC_LEN_ITER; 
  • trunk/thune/internal.h

    r164 r168  
    117117#define CC_EVAL_RUNNING     2 
    118118#define CC_ITER             3 
    119 #define CC_FOREACH          4 
    120 #define CC_CATCH            5 
    121 #define CC_END              6 
     119#define CC_CATCH            4 
     120#define CC_END              5 
    122121#define CC_COUNT            (CC_END+1) 
    123122 
     
    126125#define CC_LEN_EVAL         1 
    127126#define CC_LEN_ITER         2 
    128 #define CC_LEN_FOREACH      2 
    129127#define CC_LEN_CATCH        3 
    130128#define CC_LEN_END          1 
     
    186184#define OP_28               28 
    187185#define OP_DO_FUNC          29 
    188 #define OP_FOREACH          30 
    189186//#define OP_END              255 
    190187 
  • trunk/thune/print.c

    r162 r168  
    4949        EXPAND( str, len ); 
    5050    str->used += copyUtf16ToAscii( str->ptr.c + str->used, cp, len ); 
     51} 
     52 
     53 
     54/* 
     55   c2 must be a string! or string slice!. 
     56*/ 
     57void ur_strCatCell( UString* out, int encoding, const UCell* c2 ) 
     58{ 
     59    UString* str2; 
     60    int si; 
     61    int used; 
     62 
     63    str2 = ur_bin( c2 ); 
     64    si = c2->series.it; 
     65 
     66    used = str2->used; 
     67    if( ur_is(c2, UT_SLICE) ) 
     68    { 
     69        if( used > c2->slice.end ) 
     70            used = c2->slice.end; 
     71    } 
     72    used -= si; 
     73 
     74    if( used > 0 ) 
     75    { 
     76        if( encoding == UR_ENC_ASCII ) 
     77        { 
     78            switch( ur_encoding(c2) ) 
     79            { 
     80                case UR_ENC_ASCII: 
     81                case UR_ENC_UTF8: 
     82                    ur_strCat( out, str2->ptr.c + si, used ); 
     83                    break; 
     84 
     85                case UR_ENC_UTF16: 
     86                    ur_strCatUtf16( out, str2->ptr.u16 + si, used ); 
     87                    break; 
     88            } 
     89        } 
     90        else 
     91        { 
     92            assert( "encoding not handled" && 0 ); 
     93        } 
     94    } 
    5195} 
    5296 
     
    609653 
    610654        case UT_STRING: 
    611         { 
    612             UString* str = ur_bin( val ); 
    613             int si = val->series.it; 
    614             int used = str->used - si; 
    615             if( used > 0 ) 
    616             { 
    617                 switch( ur_encoding(val) ) 
    618                 { 
    619                     case UR_ENC_ASCII: 
    620                     case UR_ENC_UTF8: 
    621                         append( out, str->ptr.c + si, used ); 
    622                         break; 
    623  
    624                     case UR_ENC_UTF16: 
    625                         EXPAND( out, used ); 
    626                         out->used += copyUtf16ToAscii( out->ptr.c, 
    627                                                        str->ptr.u16, used ); 
    628                         break; 
    629                 } 
    630             } 
    631         } 
     655            ur_strCatCell( out, UR_ENC_ASCII, val ); 
    632656            break; 
    633657#if 0 
     
    702726        } 
    703727            break; 
     728 
     729        case UT_SLICE: 
     730            switch( ur_sliceDT(val) ) 
     731            { 
     732                case UT_STRING: 
     733                    ur_strCatCell( out, UR_ENC_ASCII, val ); 
     734                    break; 
     735            } 
     736            break; 
     737 
    704738#if 0 
    705739        case OT_OBJECT: 
  • trunk/thune/series.c

    r166 r168  
    2323#include "charset.h" 
    2424#include "internal.h" 
     25 
     26 
     27extern void ur_strCatCell( UString* out, int encoding, const UCell* c2 ); 
    2528 
    2629 
     
    885888    switch( ur_type(ser) ) 
    886889    { 
    887 #if 0 
    888         case UT_BINARY: 
     890        //case UT_BINARY: 
     891 
    889892        case UT_STRING: 
    890             if( ur_isAString(tos) ) 
    891             { 
    892                 UBinary* s1 = ur_bin(ser); 
    893                 UBinary* s2 = ur_bin(tos); 
    894                 int used = s1->used; 
    895                 int srcLen = s2->used - tos->series.it; 
    896  
    897                 ur_arrayReserve( s1, sizeof(char), used + srcLen ); 
    898  
    899                 memCpy( s1->ptr.cells + s1->used, 
    900                         s2->ptr.cells + tos->series.it, srcLen ); 
    901                 s1->used += srcLen; 
    902             } 
    903             break; 
    904 #endif 
     893            if( ur_is(tos, UT_STRING) || 
     894                (ur_is(tos, UT_SLICE) && (ur_sliceDT(tos) == UT_STRING)) ) 
     895            { 
     896                ur_strCatCell( ur_bin(ser), ur_encoding(ser), tos ); 
     897            } 
     898            break; 
     899 
    905900        case UT_BLOCK: 
    906901        case UT_PAREN: 
  • trunk/thune/tests/Makefile

    r5 r168  
    11# Makefile  
     2 
     3.PHONY: grind clean 
    24 
    35test: 
    46        @./run_test working/*.t 
    57 
     8grind: 
     9        @./grind working/*.t 
     10 
    611clean: 
    712        @rm -f working/*.out 
  • trunk/thune/tests/working/control.good

    r19 r168  
     1--- while 0 --- 
    120 
    231 
    342 
    4 done 
     5--- loop 0 --- 
    560 
    671 
     
    13148 
    14159 
    15 done 
     16--- foreach 0 --- 
     17--- foreach 1 --- 
     18 1 
     19 2 
     20 a 
     21 b 
     22 one 
     23 two 
     24 tree 
     25--- foreach 2 --- 
     26 1 2 
     27 a b 
     28 one two 
     29 tree none 
     30--- foreach 3 --- 
     31 1 2 a 
     32 b one two 
     33 tree none none 
     34--- foreach 8 --- 
     35 1 2 a b one two tree none 
     36Tests done 
  • trunk/thune/tests/working/control.t

    r152 r168  
    11; Control flow test 
    22 
     3 
     4"--- while 0 ---" print 
    350 :n 
    46[n dup . 1 add :n] [n 3 lt?] while 
    5 "done" print 
    67 
    78 
     9"--- loop 0 ---" print 
    8100 :n 
    911[n dup . 1 add :n] 10 loop 
    10 "done" print 
    1112 
     13 
     14[[" " prin first reduce prin] iter eol prin] proc :rprint     ; (block -- ) 
     15 
     16[1 2 'a 'b "one" "two" "tree"] :data 
     17 
     18"--- foreach 0 ---" print 
     19data tail [a] [[a] rprint] foreach 
     20 
     21"--- foreach 1 ---" print 
     22data [a] [[a] rprint] foreach 
     23 
     24"--- foreach 2 ---" print 
     25data [a b] [[a b] rprint] foreach 
     26 
     27"--- foreach 3 ---" print 
     28data [a b c] [[a b c] rprint] foreach 
     29 
     30"--- foreach 8 ---" print 
     31data [a b c d e f g h] [[a b c d e f g h] rprint] foreach 
     32 
     33 
     34"Tests done" print