Changeset 144 for trunk/orca/ovalue.c

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

Native arguments are now kept on the stack until after the call and the
result is now always put into a1.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/orca/ovalue.c

    r142 r144  
    133133 
    134134 
    135 static void orNop() 
     135OR_NATIVE( orNop ) 
    136136{ 
    137137    //cprint( "KR nop %p %d\n", orEnv->dataStack.buf, orEnv->dataStack.used ); 
     
    319319void orInitEnv( OEnv* env, int dataStackSize, int callStackSize ) 
    320320{ 
     321    OValue* res; 
     322 
    321323#ifdef DEBUG 
    322324    _validateEnv(); 
     
    399401    OBlock* blk; 
    400402    OBlock* typeBlk; 
    401     OValue* res; 
    402403    OValue* wval; 
    403404    OContext ctx; 
     
    411412    orAppendBlock( typeBlk, 0 ); 
    412413 
    413     res = orRESULT; 
     414    res = orEnv->dataStack.values; 
    414415    orSetTF( res, OT_BLOCK ); 
    415416    orSetSeries( res, orBlockN(blk), 0 ); 
     
    431432    { 
    432433        cprint( "** Boot Error: " ); 
    433         orPrintNative( orRESULT ); 
     434        orPrintNative( res ); 
    434435    } 
    435436 
     
    450451    { 
    451452        cprint( "** Boot Error: " ); 
    452         orPrintNative( orRESULT ); 
     453        orPrintNative( res ); 
    453454    } 
    454455 
     
    685686#endif 
    686687        case OT_LOGIC: 
    687             orResult( OT_LOGIC, orInt(from) ? 1 : 0 ); 
     688            orSetTF( res, OT_LOGIC ); 
     689            orInt(res) =  orInt(from) ? 1 : 0; 
    688690            return 1; 
    689691 
     
    740742                    (it[1].type == OT_INTEGER) ) 
    741743                { 
    742                     OValue* res = orRESULT; 
    743744                    orSetTF( res, OT_PAIR ); 
    744745                    res->pair[0] = it[0].integer; 
     
    901902                // Return curried function to pick up body argument. 
    902903                // Too bad we have no way of executing this native. 
    903                 OValue* res = orRESULT; 
    904                 res->type  = OT_NATIVE; 
    905                 res->flags = 0; 
    906                 res->argc  = 1; 
    907                 res->refc  = 0; 
    908                 res->native.addr    = orFuncNative; 
    909                 res->native.specBlk = 0; 
     904                a1->type  = OT_NATIVE; 
     905                a1->flags = 0; 
     906                a1->argc  = 1; 
     907                a1->refc  = 0; 
     908                a1->native.addr    = orFuncNative; 
     909                a1->native.specBlk = 0; 
    910910                } 
    911911                return; 
     
    915915            case OT_VEC2: 
    916916            case OT_VEC3: 
    917             { 
    918                 OValue* res = orRESULT; 
    919                 orSetTF( res, orInt(a1) ); 
    920                 res->vec3.x = 0.0f; 
    921                 res->vec3.y = 0.0f; 
    922                 res->vec3.z = 0.0f; 
     917                orSetTF( a1, orInt(a1) ); 
     918                a1->vec3.x = 0.0f; 
     919                a1->vec3.y = 0.0f; 
     920                a1->vec3.z = 0.0f; 
    923921                if( a2->type == OT_BLOCK ) 
    924                     orLoadVectorBlock( &res->vec3.x, 3, a2 );  
    925             } 
     922                    orLoadVectorBlock( &a1->vec3.x, 3, a2 );  
    926923                return; 
    927924 
     
    12391236 
    12401237 
    1241 /* Public because of Orca-GL */ 
     1238/* Public for Orca-GL */ 
    12421239OR_NATIVE_PUB( orLoadNative ) 
    12431240{ 
     
    12611258 
    12621259        orTermCStr( str ); 
    1263  
    12641260        file = orStrChars( str, a1 ); 
    12651261 
     
    12951291                        { 
    12961292                            orResultBLOCK( blk - orBLOCKS ); 
    1297                             orRESULT->flags |= OR_FLAG_HEADER; 
     1293                            a1->flags |= OR_FLAG_HEADER; 
    12981294                        } 
    12991295                    } 
     
    13771373#define REF_DO_ARG      a1+2 
    13781374 
     1375/* 
     1376  do: native [value /args arg] 
     1377*/ 
    13791378OR_NATIVE_PUB( orDoNative ) 
    13801379{ 
     
    13821381    { 
    13831382        orEvalBlock( orBLOCK( a1 ), a1->series.it ); 
     1383        orCopyV(a1, a1[3]); 
    13841384    } 
    13851385    else if( orIs(a1, OT_FILE) ) 
     
    14111411            orRefPop( 1 ); 
    14121412            orEvalBlock( orBlockPtr( blkN ), 2 ); 
     1413            orCopyV(a1, a1[3]); 
    14131414            _popScript(); 
    14141415        } 
     
    14241425            return; 
    14251426        orEvalBlock( orBLOCK( a1 ), 0 ); 
     1427        orCopyV(a1, a1[3]); 
    14261428    } 
    14271429    else if( orIs(a1, OT_WORD) ) 
     
    15251527{ 
    15261528    int logic; 
    1527     if( (a1->type == OT_NONE) || 
    1528         ((a1->type == OT_LOGIC) && (orInt(a1) == OR_FALSE)) ) 
     1529    if( orIfFalse(a1) ) 
    15291530        logic = OR_TRUE; 
    15301531    else 
     
    15351536 
    15361537 
     1538/* 
     1539  if: native [cond body] 
     1540*/ 
    15371541OR_NATIVE( orIfNative ) 
    15381542{ 
     
    15411545        OValue* a2 = a1 + 1; 
    15421546        orEvalBlock( orBLOCK( a2 ), a2->series.it ); 
     1547        orCopyV( a1, a1[2] ); 
    15431548        return; 
    15441549    } 
     
    15531558        OValue* a2 = a1 + 1; 
    15541559        orEvalBlock( orBLOCK( a2 ), a2->series.it ); 
     1560        orCopyV( a1, a1[2] ); 
    15551561        return; 
    15561562    } 
     
    15621568{ 
    15631569    OValue* blkArg = a1 + 2; 
    1564     if( a1->type != OT_NONE ) 
    1565     { 
    1566         if( (a1->type != OT_LOGIC) || (orInt(a1) != 0)  ) 
    1567             --blkArg; 
    1568     } 
     1570    if( orIfTrue(a1) ) 
     1571        --blkArg; 
    15691572    orEvalBlock( orBLOCK( blkArg ), blkArg->series.it ); 
    1570 } 
    1571  
    1572  
     1573    orCopyV( a1, a1[3] ); 
     1574} 
     1575 
     1576 
     1577/* 
     1578  loop: native [count block] 
     1579*/ 
    15731580OR_NATIVE( orLoopNative ) 
    15741581{ 
     
    15771584    int n = orInt(a1); 
    15781585 
    1579     ++a1; 
    1580     body = a1->index; 
    1581     bi   = a1->series.it; 
     1586    body = (a1 + 1)->index; 
     1587    bi   = (a1 + 1)->series.it; 
     1588 
     1589    orStackDrop(2); 
    15821590 
    15831591    while( n > 0 ) 
     
    15881596            break; 
    15891597    } 
     1598 
     1599    orStackGrow(2); 
    15901600} 
    15911601 
     
    16591669 
    16601670 
     1671/* 
     1672  until: native [cond] 
     1673*/ 
    16611674OR_NATIVE( orUntilNative ) 
    16621675{ 
    1663     OValue* res; 
     1676    OValue* res = a1 + 1; 
    16641677    OIndex cond = a1->index; 
    16651678    OIndex ci   = a1->series.it; 
     
    16741687            break; 
    16751688        } 
    1676         res = orRESULT; 
    1677         if( res->type != OT_NONE ) 
    1678         { 
    1679             if( (res->type != OT_LOGIC) || (orInt(res) != 0) ) 
    1680                 break; 
    1681         } 
    1682     } 
    1683 } 
    1684  
    1685  
     1689        if( orIfTrue(res) ) 
     1690        { 
     1691            orCopyV(a1, *res); 
     1692            break; 
     1693        } 
     1694    } 
     1695} 
     1696 
     1697 
     1698/* 
     1699  while: native [cond body] 
     1700*/ 
    16861701OR_NATIVE( orWhileNative ) 
    16871702{ 
     
    16931708    OIndex bi   = (a1 + 1)->series.it; 
    16941709 
    1695     orRefAvailErr( 2 ) 
    1696     orRefPush( OT_BLOCK, cond ); 
    1697     orRefPush( OT_BLOCK, body ); 
     1710    res = orTOS; 
    16981711 
    16991712loop: 
     
    17031716        goto exit; 
    17041717 
    1705     res = orRESULT; 
    1706     if( res->type != OT_NONE ) 
    1707     { 
    1708         if( (res->type != OT_LOGIC) || (orInt(res) != 0) ) 
    1709         { 
    1710             if( onstack ) 
    1711             { 
    1712                 onstack = 0; 
    1713                 --orEnv->dataStack.used; 
    1714             } 
    1715  
    1716             orEvalBlock( orBLOCKS + body, bi ); 
    1717             if( orErrorThrown ) 
    1718             { 
    1719                 if( orErrorIsType(OR_ERROR_BREAK) ) 
    1720                 { 
    1721                     orErrorClear; 
    1722                     res = orRESULT; 
    1723                     orSetTF( res, OT_UNSET ); 
    1724                 } 
    1725                 goto pop; 
    1726             } 
    1727  
    1728             ++orEnv->dataStack.used; 
    1729             onstack = 1; 
    1730             goto loop; 
    1731         } 
     1718    res = orTOS; 
     1719    if( orIfTrue(res) ) 
     1720    { 
     1721        if( onstack ) 
     1722        { 
     1723            onstack = 0; 
     1724            --orEnv->dataStack.used; 
     1725        } 
     1726 
     1727        orEvalBlock( orBLOCKS + body, bi ); 
     1728        if( orErrorThrown ) 
     1729        { 
     1730            if( orErrorIsType(OR_ERROR_BREAK) ) 
     1731            { 
     1732                orErrorClear; 
     1733                orSetTF( a1, OT_UNSET ); 
     1734            } 
     1735            orCopyV( a1, a1[2] ); 
     1736            return; 
     1737        } 
     1738 
     1739        ++orEnv->dataStack.used; 
     1740        onstack = 1; 
     1741        goto loop; 
    17321742    } 
    17331743 
     
    17351745 
    17361746    if( onstack ) 
     1747    { 
    17371748        --orEnv->dataStack.used; 
    1738  
    1739 pop: 
    1740  
    1741     orRefPop( 2 ); 
     1749        orCopyV( a1, a1[2] ); 
     1750    } 
    17421751} 
    17431752 
     
    17741783 
    17751784 
    1776 void orQuitNative() 
    1777 { 
    1778     OValue* res = orRESULT; 
    1779  
    1780     orSetError( res, OR_ERROR_QUIT ); 
    1781  
    1782     res->error.msg     = 0; 
    1783     res->error.block   = 0; 
    1784     res->error.nearVal = 0; 
    1785 } 
    1786  
    1787  
    1788 void orHaltNative() 
    1789 { 
    1790     OValue* res = orRESULT; 
    1791  
    1792     orSetError( res, OR_ERROR_HALT ); 
    1793  
    1794     res->error.msg     = 0; 
    1795     res->error.block   = 0; 
    1796     res->error.nearVal = 0; 
     1785OR_NATIVE( orQuitNative ) 
     1786{ 
     1787    orSetError( a1, OR_ERROR_QUIT ); 
     1788 
     1789    a1->error.msg     = 0; 
     1790    a1->error.block   = 0; 
     1791    a1->error.nearVal = 0; 
     1792} 
     1793 
     1794 
     1795OR_NATIVE( orHaltNative ) 
     1796{ 
     1797    orSetError( a1, OR_ERROR_HALT ); 
     1798 
     1799    a1->error.msg     = 0; 
     1800    a1->error.block   = 0; 
     1801    a1->error.nearVal = 0; 
    17971802} 
    17981803 
     
    18251830    // (must set result index before type). 
    18261831 
    1827     OValue* res = orRESULT; 
    1828  
    18291832    if( orRefineSet(REF_TYPEQ_WORD) ) 
    18301833    { 
    18311834        // NOTE: This assumes word index/atom are the same as type. 
    1832         res->word.context = 0; 
    1833         res->word.index   = a1->type; 
    1834         res->word.atom    = a1->type; 
    1835         orSetTF( res, OT_WORD ); 
     1835        a1->word.context = 0; 
     1836        a1->word.index   = a1->type; 
     1837        a1->word.atom    = a1->type; 
     1838        orSetTF( a1, OT_WORD ); 
    18361839    } 
    18371840    else 
    18381841    { 
    1839         res->index = a1->type; 
    1840         orSetTF( res, OT_DATATYPE ); 
     1842        a1->index = a1->type; 
     1843        orSetTF( a1, OT_DATATYPE ); 
    18411844    } 
    18421845} 
     
    18441847 
    18451848/* 
    1846   try: native [ block [block!] ] 
     1849  try: native [block [block!]] 
    18471850*/ 
    18481851OR_NATIVE( orTryNative ) 
     
    18511854   if( orErrorThrown ) 
    18521855   { 
    1853        OValue* res = orRESULT; 
    1854        if( res != orEnv->error ) 
    1855            *res = *orEnv->error; 
     1856       orCopyV(a1, *orEnv->error); 
    18561857       orErrorClear; 
     1858   } 
     1859   else 
     1860   { 
     1861       orCopyV(a1, a1[1]); 
    18571862   } 
    18581863} 
     
    23242329            int ppos; 
    23252330 
    2326             wval = orRESULT; 
     2331            wval = orTOS; 
    23272332            ppos = orEvalPath( orBLOCK(pc), pc->series.it, 0, wval ); 
    23282333            if( orErrorThrown ) 
     
    25682573 
    25692574    assert( data->used > 0 ); 
    2570     data->used -= funcv.argc + funcv.refc; 
     2575    { 
     2576    int total = funcv.argc + funcv.refc; 
     2577    int level = data->used; 
    25712578 
    25722579    // NOTE: Natives may need to orHold() any complex argument values since 
    25732580    // the stack no longer references them here. 
    25742581 
    2575     funcv.native.addr( DATA_TOP ); 
     2582    funcv.native.addr( DATA_TOP - total ); 
     2583    assert( level == data->used ); 
     2584    data->used -= total; 
     2585    } 
    25762586    goto sFinishNative; 
    25772587 
     
    29842994        orRefPush( OT_BLOCK, nblkN ); 
    29852995 
    2986         orRESULT->type = OT_UNSET; 
     2996        orTOS->type = OT_UNSET; 
    29872997        while( vi < used ) 
    29882998        { 
     
    29983008            rblk = orBlockPtr( nblkN ); 
    29993009            OA_EXPAND1( OValue, rblk, val ); 
    3000             orCopyV( val, *orRESULT ); 
     3010            orCopyV( val, *orTOS ); 
    30013011        } 
    30023012 
     
    30223032                break; 
    30233033 
    3024             res = orRESULT; 
     3034            res = orTOS; 
    30253035            if( orIs(res, OT_BLOCK) && ! only ) 
    30263036            { 
     
    30693079 
    30703080/* 
     3081   any: native [block] 
     3082 
    30713083   Result is the first value that is not false or none! 
    30723084*/ 
     
    30803092    int reti; 
    30813093 
    3082     orRESULT->type = OT_UNSET; 
     3094    orTOS->type = OT_UNSET; 
    30833095 
    30843096    while( vi < used ) 
     
    30923104        vi = reti; 
    30933105 
    3094         res = orRESULT; 
    3095         if( res->type != OT_NONE ) 
    3096         { 
    3097             if( (res->type != OT_LOGIC) || (orLogic(res) != 0) ) 
    3098             { 
    3099                 return; 
    3100             } 
     3106        res = orTOS; 
     3107        if( orIfTrue(res) ) 
     3108        { 
     3109            orCopyV(a1, *res); 
     3110            return; 
    31013111        } 
    31023112    } 
     
    31063116 
    31073117 
     3118/* 
     3119   all: native [block] 
     3120*/ 
    31083121OR_NATIVE( orAllNative ) 
    31093122{ 
     
    31153128    int reti; 
    31163129 
    3117     orRESULT->type = OT_UNSET; 
     3130    orTOS->type = OT_UNSET; 
    31183131 
    31193132    while( vi < used ) 
     
    31273140        vi = reti; 
    31283141 
    3129         res = orRESULT; 
    3130         if( orIs(res, OT_NONE) ) 
     3142        res = orTOS; 
     3143        if( orIfFalse(res) ) 
    31313144            goto fail; 
    3132         if( orIs(res, OT_LOGIC) && (orLogic(res) == 0) ) 
    3133             goto fail; 
    3134     } 
     3145    } 
     3146 
     3147    orCopyV(a1, a1[1]); 
    31353148    return; 
    31363149 
     
    31583171    orRefPush( OT_BLOCK, blkN ); 
    31593172 
    3160     orRESULT->type = OT_UNSET; 
     3173    orTOS->type = OT_UNSET; 
    31613174 
    31623175    while( vi < used ) 
     
    33343347    msg->used = cp - msg->charArray; 
    33353348 
    3336     res = orRESULT; 
     3349    res = orTOS; 
    33373350 
    33383351    orSetError( res, type ); 
     
    41984211OR_NATIVE_PUB( orNativeNative ) 
    41994212{ 
    4200     if( a1->type == OT_BLOCK ) 
     4213    if( orIs(a1, OT_BLOCK) ) 
    42014214    { 
    42024215        OBlock* blk; 
     
    42804293#endif 
    42814294 
    4282         val = orRESULT; 
     4295        val = a1; 
    42834296        val->type  = OT_NATIVE; 
    42844297        val->flags = 0; 
     
    42974310OR_NATIVE( orDoesNative ) 
    42984311{ 
    4299     if( a1->type == OT_BLOCK ) 
     4312    if( orIs(a1, OT_BLOCK) ) 
    43004313    { 
    43014314        OIndex bodyN; 
    4302  
    43034315        bodyN = a1->index; 
    43044316 
    4305         { 
    4306         OValue* res = orRESULT; 
    4307         res->type  = OT_FUNCTION; 
    4308         res->flags = 0; 
    4309         res->argc  = 0; 
    4310         res->refc  = 0; 
    4311         res->func.bodyBlk = bodyN; 
    4312         res->func.specBlk = 0; 
    4313         res->func.context = 0; 
    4314         } 
     4317        a1->type  = OT_FUNCTION; 
     4318        a1->flags = 0; 
     4319        a1->argc  = 0; 
     4320        a1->refc  = 0; 
     4321        a1->func.bodyBlk = bodyN; 
     4322        a1->func.specBlk = 0; 
     4323        a1->func.context = 0; 
    43154324        return; 
    43164325    } 
     
    44634472    int specIndex = a1->index; 
    44644473 
    4465     OValue* res = orRESULT; 
    4466  
    4467     res->type  = OT_FUNCTION; 
    4468     res->flags = flags; 
    4469     res->argc  = argc; 
    4470     res->refc  = refc; 
    4471     res->func.bodyBlk = bodyN; 
    4472     res->func.specBlk = specIndex; 
    4473     res->func.context = ctx.vblkN; 
     4474    a1->type  = OT_FUNCTION; 
     4475    a1->flags = flags; 
     4476    a1->argc  = argc; 
     4477    a1->refc  = refc; 
     4478    a1->func.bodyBlk = bodyN; 
     4479    a1->func.specBlk = specIndex; 
     4480    a1->func.context = ctx.vblkN; 
    44744481 
    44754482#if 0