Changeset 408

Show
Ignore:
Timestamp:
06/11/07 04:07:02 (16 months ago)
Author:
krobillard
Message:

Thune - Simplified macros with macro! datatype.

Location:
branches/thune/thread_safe
Files:
14 modified

Legend:

Unmodified
Added
Removed
  • branches/thune/thread_safe/boot.c

    r378 r408  
    117117  "  none :os\n" 
    118118  "  [] :devices\n" 
    119   "  [\n" 
    120   "    [\n" 
    121   "      [word word!/lit-word! def] [\n" 
    122   "        def: word: reader-macros last set\n" 
    123   "      ] func :macro\n" 
    124   "      [word word!/lit-word! sig block! body block!] [\n" 
    125   "        sig  kernel-ops infuse\n" 
    126   "        body kernel-ops infuse\n" 
    127   "        func word: reader-macros last set\n" 
    128   "      ] func :macrof\n" 
    129   "      [n] [[n: 'add word! as] reduce] func :+\n" 
    130   "      [n] [[n: 'sub word! as] reduce] func :-\n" 
    131   "      [n] [[n: 'mul word! as] reduce] func :*\n" 
    132   "      [n] [[n: 'div word! as] reduce] func :/\n" 
    133   "    ] context\n" 
    134   "  ] reduce :reader-macros\n" 
    135119  "]\n" 
    136120  "context :script-env\n" 
  • branches/thune/thread_safe/charset.c

    r186 r408  
    2626/* Strict Word: a-z A-Z ?!.*&|=_~ and all ascii >= 127  */ 
    2727uint8_t charset_sword[32] = { 
     28#ifdef UR_CONFIG_MACROS 
     29        0x00,0x00,0x00,0x00,0x42,0x44,0x00,0xA0,    // Don't allow < > 
     30#else 
    2831        0x00,0x00,0x00,0x00,0x42,0x44,0x00,0xF0, 
     32#endif 
    2933        0xFE,0xFF,0xFF,0x97,0xFF,0xFF,0xFF,0x57, 
    3034        0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 
     
    3539/* Word: 0-9 a-z A-Z ?!.'+-*&|=_~ and all ascii >= 127 */ 
    3640uint8_t charset_word[32] = { 
     41#ifdef UR_CONFIG_MACROS 
     42        0x00,0x00,0x00,0x00,0xC2,0x6C,0xFF,0xA3,    // Don't allow < > 
     43#else 
    3744        0x00,0x00,0x00,0x00,0xC2,0x6C,0xFF,0xF3, 
     45#endif 
    3846        0xFE,0xFF,0xFF,0x97,0xFF,0xFF,0xFF,0x57, 
    3947        0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 
  • branches/thune/thread_safe/config.t

    r389 r408  
    55[x] math3d   "Include vector & matrix math calls" 
    66             enable trig 
     7[x] macros   "Reader macros" 
    78[ ] dt-code  "Include 'code datatype" 
    89 
     
    1314 
    1415[ ] threads  "Experimental script threads" 
    15 [ ] macros   "Experimantal reader macros" 
    1616[ ] emh      "Debugger Hooks" 
    1717 
  • branches/thune/thread_safe/eval.c

    r404 r408  
    5959 
    6060 
    61 static void _infuseOpcodes( UThread* ut, UIndex blkN ) 
     61void ur_infuseOpcodes( UThread* ut, UIndex blkN ) 
    6262{ 
    6363    UBlock* blk; 
     
    432432        if( blkN ) 
    433433        { 
    434             _infuseOpcodes( ut, blkN ); 
     434            ur_infuseOpcodes( ut, blkN ); 
    435435            return ur_eval( ut, blkN, 0 ); 
    436436        } 
  • branches/thune/thread_safe/internal.h

    r387 r408  
    1414#define BLK_GLOBAL_WORD     1 
    1515#define BLK_GLOBAL_VAL      2 
    16 //#define BLK_DSTACK          3 
     16#define BLK_CTX_STACK       3 
     17//#define BLK_DSTACK          4 
    1718 
    1819 
  • branches/thune/thread_safe/make.c

    r400 r408  
    4545static void ur_internGlobal( UThread* ut, UCell* wc, UAtom atom ) 
    4646{ 
     47    UBlock* mblk; 
     48    UCell* mtop; 
     49    UCell* mbot; 
    4750    int wrdN; 
    4851 
    49     // A module system could be implemented by looking up atom in a stack 
    50     // of module contexts here... 
    51  
    52     wrdN = ur_lookup( &ur_global, atom ); 
    53     if( wrdN > -1 ) 
    54     { 
    55         // UR_BIND_THREAD should already be set. 
    56         wc->word.wordBlk = BLK_GLOBAL_WORD; 
    57         wc->word.valBlk  = BLK_GLOBAL_VAL; 
    58         wc->word.index   = wrdN; 
    59         return; 
    60     } 
    61  
     52    // Look for atom in module context stack. 
     53    mblk = ur_blockPtr( BLK_CTX_STACK ); 
     54    mbot = mblk->ptr.cells; 
     55    mtop = mbot + mblk->used; 
     56    do 
     57    { 
     58        --mtop; 
     59        wrdN = ur_lookup( mtop, atom ); 
     60        if( wrdN > -1 ) 
     61            goto assign; 
     62    } 
     63    while( mtop != mbot ); 
     64 
     65 
     66    // Now try the shared global context. 
    6267    if( ut->env->blocks.arr.used ) 
    6368    { 
     
    7378    } 
    7479 
    75     // For modules, intern word into current module (top of context stack). 
     80    // Word not found, so intern into current module (top of context stack). 
     81    mtop = mbot + (mblk->used - 1); 
     82    wrdN = ur_internWord( mtop, atom ); 
     83 
     84assign: 
    7685 
    7786    // UR_BIND_THREAD should already be set. 
    78     wc->word.wordBlk = BLK_GLOBAL_WORD; 
    79     wc->word.valBlk  = BLK_GLOBAL_VAL; 
    80     wc->word.index   = ur_internWord( &ur_global, atom ); 
     87    wc->word.wordBlk = mtop->ctx.wordBlk; 
     88    wc->word.valBlk  = mtop->ctx.valBlk; 
     89    wc->word.index   = wrdN; 
    8190} 
    8291 
  • branches/thune/thread_safe/mkboot.t

    r377 r408  
    4141:boot-script 
    4242 
    43  
    4443{ 
    4544logic! 1 make dup :yes :on 
     
    134133  none :os 
    135134  [] :devices 
    136   [ 
    137     [ 
    138       [word word!/lit-word! def] [ 
    139         def: word: reader-macros last set 
    140       ] func :macro 
    141  
    142       [word word!/lit-word! sig block! body block!] [ 
    143         sig  kernel-ops infuse 
    144         body kernel-ops infuse 
    145         func word: reader-macros last set 
    146       ] func :macrof 
    147  
    148       [n] [[n: 'add word! as] reduce] func :+ 
    149       [n] [[n: 'sub word! as] reduce] func :- 
    150       [n] [[n: 'mul word! as] reduce] func :* 
    151       [n] [[n: 'div word! as] reduce] func :/ 
    152     ] context 
    153   ] reduce :reader-macros 
    154135] 
    155136context :script-env 
  • branches/thune/thread_safe/print.c

    r390 r408  
    717717            ur_strCatCell( ut, out, UR_ENC_ASCII, val ); 
    718718            break; 
     719 
     720        case UT_BINARY: 
     721            _binCellToStr( ut, out, val ); 
     722            break; 
    719723#if 0 
    720724        case OT_TAG:        { 
     
    728732        case OT_BITSET:     append(out, "make bitset! ", 13); 
    729733                            // Fall through to binary. 
    730  
    731         case OT_BINARY:     _binCellToStr( out, val ); 
    732                             break; 
    733734#endif 
    734735        case UT_CHAR:       append1( ur_char(val), out ); 
  • branches/thune/thread_safe/tests/working/bytecode.t

    r368 r408  
    4141        [val add-word emit16]               ;20 - block 
    4242        nop                                 ;21 - paren 
    43         nop                                 ;22 - path 
    44         nop                                 ;23 - setpath 
    45         nop                                 ;24 - slice 
    46         nop                                 ;25 - array 
    47         nop                                 ;26 - bitset 
    48         nop                                 ;27 - list 
    49         nop                                 ;28 - context 
    50         nop                                 ;29 - function 
    51         nop                                 ;30 - call 
    52         nop                                 ;31 - date 
    53         nop                                 ;32 - time 
    54         nop                                 ;33 - bitset 
    55         nop                                 ;34 - error 
    56         nop                                 ;35 - code 
    57         nop                                 ;36 - port 
    58         nop                                 ;37 - struct 
     43        nop                                 ;23 - macro 
     44        nop                                 ;23 - path 
     45        nop                                 ;24 - setpath 
     46        nop                                 ;25 - slice 
     47        nop                                 ;26 - array 
     48        nop                                 ;27 - bitset 
     49        nop                                 ;28 - list 
     50        nop                                 ;29 - context 
     51        nop                                 ;30 - function 
     52        nop                                 ;31 - call 
     53        nop                                 ;32 - date 
     54        nop                                 ;33 - time 
     55        nop                                 ;34 - bitset 
     56        nop                                 ;35 - error 
     57        nop                                 ;36 - code 
     58        nop                                 ;37 - port 
     59        nop                                 ;38 - struct 
    5960    ] :type-convert 
    6061 
  • branches/thune/thread_safe/thread.c

    r387 r408  
    159159 
    160160 
     161void ur_pushContext( UThread* ut, UContext* ctx ) 
     162{ 
     163    UCell* cell; 
     164    UBlock* blk = ur_blockPtr( BLK_CTX_STACK ); 
     165    UR_EXPAND_1( UCell, blk, cell ); 
     166    ur_copyCell( cell, *ctx ); 
     167} 
     168 
     169 
     170void ur_popContext( UThread* ut ) 
     171{ 
     172    UBlock* blk = ur_blockPtr( BLK_CTX_STACK ); 
     173    if( blk->used ) 
     174        --blk->used; 
     175} 
     176 
     177 
    161178UBinary* ur_threadTmp( UThread* ut ) 
    162179{ 
  • branches/thune/thread_safe/thune.c

    r387 r408  
    294294#define EMH_HALT(pc,end) 
    295295#endif 
     296 
     297 
     298//extern void _addCells( UThread*, UCell*, UCell* ); 
     299//extern void _mulCells( UThread*, UCell*, UCell* ); 
    296300 
    297301 
     
    468472                                goto throw_cc; 
    469473 
    470                             _infuseOpcodes( ut, tblkN ); 
     474                            ur_infuseOpcodes( ut, tblkN ); 
    471475                            ur_setSeries(val, tblkN, 0); 
    472476                            goto do_block; 
  • branches/thune/thread_safe/tokenize.c

    r390 r408  
    204204 
    205205 
    206 /* 
    207 #if UR_CONFIG_MATH3D 
    208 static int isVec3( const char* cp, int len )  
     206#ifdef UR_CONFIG_MACROS 
     207extern void ur_infuseOpcodes( UThread*, UIndex ); 
     208 
     209//#define MACRO_CONTEXT   1 
     210#ifdef MACRO_CONTEXT 
     211extern UContext ur_global; 
     212 
     213static void _pushMacroContext( UThread* ut ) 
    209214{ 
    210     if( len < 3 ) 
    211         return 0; 
    212     if( *cp == '-' ) 
    213         ++cp; 
    214     if( *cp < '0' || *cp > '9' ) 
    215         return 0; 
    216     return 1; 
    217 } 
    218  
    219  
    220 static void assignVec3( float* vec, const char* start, int len ) 
    221 { 
    222     int ch; 
    223     const char* pos; 
    224     const char* end = start + len; 
    225  
    226     *vec++ = (float) ur_stringToDec( start, end, &pos ); 
    227     while( pos != end ) 
    228     { 
    229         ch = *pos; 
    230         if( ! ur_bitIsSet(charset_white, ch) ) 
    231             break; 
    232         ++pos; 
    233     } 
    234     *vec++ = (float) ur_stringToDec( pos, end, &pos ); 
    235     while( pos != end ) 
    236     { 
    237         ch = *pos; 
    238         if( ! ur_bitIsSet(charset_white, ch) ) 
    239             break; 
    240         ++pos; 
    241     } 
    242     *vec   = (float) ur_stringToDec( pos, end, &pos ); 
     215    UBlock* blk; 
     216    UCell* mc; 
     217    int wrdN; 
     218    
     219    wrdN = ur_internWord( &ur_global, UR_ATOM_READER_MACROS ); 
     220    blk = ur_blockPtr( ur_global.ctx.valBlk ); 
     221    mc = blk->ptr.cells + wrdN; 
     222    if( ! ur_is(mc, UT_CONTEXT) ) 
     223        ur_makeContext( mc, 0 ); 
     224    ur_pushContext( ut, mc ); 
    243225} 
    244226#endif 
    245 */ 
    246  
    247  
    248 #ifdef UR_CONFIG_MACROS 
    249 struct TokenizeCallInfo 
    250 { 
    251     int valueLimit; 
    252     const char* finalInputPos; 
    253 }; 
    254  
    255  
    256 static int _evalMacro( UThread* ut, UBlock* blk, UCell* mval, 
    257                        const char** pin, const char* end ) 
    258 { 
    259     UCell* cell; 
    260  
    261     if( ur_is(mval, UT_FUNCTION) ) 
    262     { 
    263         if( mval->func.localArgs ) 
    264         { 
    265             UIndex tblkN; 
    266             UBlock* callBlk; 
    267             UCell* otos; 
    268             int ok; 
    269             struct TokenizeCallInfo tci; 
    270  
    271             tci.valueLimit    = mval->func.localArgs; 
    272             tci.finalInputPos = 0; 
    273  
    274             tblkN = ur_tokenize( ut, *pin, end, &tci ); 
    275             *pin = tci.finalInputPos; 
    276  
    277             callBlk = ur_blockPtr( tblkN ); 
    278             UR_EXPAND_1( UCell, callBlk, cell ); 
    279             ur_copyCell(cell, *mval); 
    280             otos = UR_TOS; 
    281             ok = ur_eval( ut, tblkN, 0 ); 
    282             if( ok != UR_EVAL_OK ) 
    283                 return 0; 
    284             if( UR_TOS == otos ) 
    285                 return 1; 
    286             mval = ur_result( ut, 1 ); 
    287         } 
    288     } 
    289  
    290     if( ur_is(mval, UT_BLOCK) ) 
    291     { 
    292         // Join blocks (share code with uc_join?). 
    293         UBlock* s2 = ur_block(mval); 
    294         int srcLen = s2->used - mval->series.it; 
    295  
    296         ur_arrayReserve( blk, sizeof(UCell), blk->used + srcLen ); 
    297  
    298         ur_copyCells( s2->ptr.cells + mval->series.it, 
    299                       s2->ptr.cells + s2->used, 
    300                       blk->ptr.cells + blk->used ); 
    301         blk->used += srcLen; 
    302     } 
    303     else 
    304     { 
    305         UR_EXPAND_1( UCell, blk, cell ); 
    306         ur_copyCell( cell, *mval ); 
    307     } 
    308     return 1; 
    309 } 
    310  
    311  
    312 static UCell* _macro( UBlock* macroStack, const char* a, const char* b ) 
    313 { 
    314     int wrdN; 
    315     UCell* top = macroStack->ptr.cells + macroStack->used; 
    316     UAtom atom = ur_intern( a, b - a ); 
    317  
    318     while( top != macroStack->ptr.cells ) 
    319     { 
    320         wrdN = ur_lookup( --top, atom ); 
    321         if( wrdN > -1 ) 
    322         { 
    323             return ur_blockPtr( top->ctx.valBlk )->ptr.cells + wrdN; 
    324         } 
    325     } 
    326  
    327     return 0; 
    328 } 
    329227#endif 
    330228 
     
    341239    ur_throwErr( UR_ERR_SYNTAX, "%s (line %d)", msg, lines + 1 ); \ 
    342240    goto error 
     241 
     242 
     243/* 
     244   Returns zero if end reached. 
     245*/ 
     246static const char* blockComment( const char* it, const char* end, int* lines ) 
     247{ 
     248    int ch, tn, mode; 
     249    int lineCount = 0; 
     250 
     251    mode = 0; 
     252    tn = 0; 
     253 
     254    SCAN_LOOP 
     255        if( ch == '\n' ) 
     256        { 
     257            ++lineCount; 
     258            mode = 0; 
     259        } 
     260        else 
     261        { 
     262            switch( mode ) 
     263            { 
     264                case 0: 
     265                    if( ch == '*' ) 
     266                        mode = 1; 
     267                    else if( ch == '/' ) 
     268                        mode = 2; 
     269                    break; 
     270 
     271                case 1: 
     272                    if( ch == '/' ) 
     273                    { 
     274                        if( tn == 0 ) 
     275                        { 
     276                            *lines += lineCount; 
     277                            return ++it; 
     278                        } 
     279                        --tn; 
     280                    } 
     281                    mode = 0; 
     282                    break; 
     283 
     284                case 2: 
     285                    if( ch == '*' ) 
     286                        ++tn; 
     287                    mode = 0; 
     288                    break; 
     289            } 
     290        } 
     291    SCAN_END 
     292 
     293    *lines += lineCount; 
     294    return 0; 
     295} 
    343296 
    344297 
     
    359312    int lines = 0; 
    360313#ifdef UR_CONFIG_MACROS 
    361 #define TCI     ((struct TokenizeCallInfo*) tci) 
    362     UBlock* macroStack; 
    363     int valueLimit; 
    364  
    365     cell = ur_resolvePath( UR_ATOM_SCRIPT_ENV, 
    366                            UT_WORD, UR_ATOM_READER_MACROS, 
    367                            UT_NONE ); 
    368     macroStack = cell ? ur_block(cell) : 0; 
    369     valueLimit = tci ? TCI->valueLimit : 0; 
    370 #else 
     314    int macroNest = 0; 
     315#endif 
     316 
    371317    (void) tci; 
    372 #endif 
    373318 
    374319 
     
    448393                case '[': 
    449394                case '(': 
     395                case '<': 
    450396                    ur_arrayReserve( &stack, sizeof(UIndex), stack.used+1 ); 
    451397                    STACK[ stack.used ] = ur_makeBlock( 0 ); 
    452398 
    453                     cell = ur_appendCell( BLOCK,  
    454                                           (ch == '[') ? UT_BLOCK : UT_PAREN ); 
     399                    switch( ch ) 
     400                    { 
     401                        case '[': mode = UT_BLOCK; break; 
     402                        case '(': mode = UT_PAREN; break; 
     403                        case '<': mode = UT_MACRO; 
     404#ifdef UR_CONFIG_MACROS 
     405#ifdef MACRO_CONTEXT 
     406                            if( ! macroNest ) 
     407                                _pushMacroContext( ut ); 
     408#endif 
     409                            ++macroNest; 
     410#endif 
     411                            break; 
     412                    } 
     413 
     414                    cell = ur_appendCell( BLOCK, mode ); 
    455415                    ur_setSeries( cell, STACK[ stack.used ], 0 ); 
    456416 
     
    465425                case ']': 
    466426                case ')': 
     427                case '>': 
    467428                    if( stack.used == 1 ) 
    468429                    { 
    469                         syntaxError( "End of block found without '['" ); 
     430                        ur_throwErr( UR_ERR_SYNTAX, 
     431                            "End of block '%c' has no opening match (line %d)", 
     432                                     ch, lines + 1 ); 
     433                        goto error; 
    470434                    } 
    471435                    --stack.used; 
    472436                    if( eol ) 
     437                        eol = 0; 
     438#ifdef UR_CONFIG_MACROS 
     439                    if( ch == '>' && macroNest ) 
    473440                    { 
    474                         /* 
    475                         UBlock* blk = ur_blockPtr( STACK[ stack.used - 1 ] ); 
    476                         cell = blk->ptr.cells + blk->used - 1; 
    477                         cell->id.flags |= UR_FLAG_BLOCK_EOL; 
    478                         */ 
    479                         eol = 0; 
     441                        UCell* otos; 
     442                        UBlock* blk; 
     443 
     444                        otos = UR_TOS; 
     445                        ur_infuseOpcodes( ut, STACK[ stack.used ] ); 
     446                        tn = ur_eval( ut, STACK[ stack.used ], 0 ); 
     447                        if( tn == UR_EVAL_ERROR ) 
     448                        { 
     449                            UR_CALL_OP = OP_THROW; 
     450                            goto error; 
     451                        } 
     452 
     453                        blk = ur_blockPtr( STACK[stack.used - 1] ); 
     454                        --blk->used;    // Remove macro. 
     455 
     456                        if( UR_TOS != otos ) 
     457                        { 
     458                            // Append reduced macro to current block. 
     459                            tn = UR_TOS - otos; 
     460                            UR_TOS = otos; 
     461                            ++otos; 
     462 
     463                            ur_arrayReserve( blk, sizeof(UCell), 
     464                                             blk->used + tn ); 
     465                            ur_copyCells( otos, otos + tn, 
     466                                          blk->ptr.cells + blk->used ); 
     467                            blk->used += tn; 
     468                        } 
     469 
     470#ifdef MACRO_CONTEXT 
     471                        if( macroNest == 1 ) 
     472                            ur_popContext( ut ); 
     473#endif 
     474                        --macroNest; 
    480475                    } 
    481 #ifdef UR_CONFIG_MACROS 
    482                     ++it; 
    483                     goto check_final; 
    484 #else 
     476#endif 
    485477                    break; 
    486 #endif 
    487478                } 
    488479            } 
     
    498489        eol = 0; 
    499490    } 
    500 #ifdef UR_CONFIG_MACROS 
    501 check_final: 
    502     if( valueLimit && (stack.used == 1) ) 
    503     { 
    504         if( --valueLimit == 0 ) 
    505         { 
    506             TCI->finalInputPos = it; 
    507             goto finish; 
    508         } 
    509     } 
    510 #endif 
    511491    goto start; 
    512492 
     
    544524//block_comment 
    545525 
    546     mode = 0; 
    547     tn = 0; 
    548  
    549     SCAN_LOOP 
    550