Changeset 51 for trunk/orca/op.c

Show
Ignore:
Timestamp:
02/14/06 13:17:58 (3 years ago)
Author:
jvargas
Message:

op.c cleaning:
- Added use orIs() macros where appropiate
- Simplified tuples ops by introduction of orToByteRange() macro
- Added greater rebol compatibility to comparison ops for tuples

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/orca/op.c

    r47 r51  
    2424void orErrorOp( const char* name, OValue* val ) 
    2525{ 
    26     orError( "Invalid type %s for %s", orDatatypeName(val->type), name ); 
     26    orError( "Invalid type %s for %s", orDatatypeName( orType(val) ), name ); 
    2727} 
    2828 
     
    3131{ 
    3232    OValue* b = a + 1; 
    33     if( a->type == OT_INTEGER ) 
    34     { 
    35         if( b->type == OT_INTEGER ) 
     33    if( orIs(a, OT_INTEGER) ) 
     34    { 
     35        if( orIs(b, OT_INTEGER) ) 
    3636        { 
    3737            orResult( OT_INTEGER, a->integer + b->integer ); 
    3838            return; 
    3939        } 
    40         else if( b->type == OT_DECIMAL ) 
     40        else if( orIs(b, OT_DECIMAL) ) 
    4141        { 
    4242            orResultDECIMAL( (double) a->integer + b->num.decimal ); 
    4343            return; 
    4444        } 
    45         else if( b->type == OT_TUPLE ) 
     45        else if( orIs(b, OT_TUPLE) ) 
    4646        { 
    4747            int tmp, i; 
     
    5353            { 
    5454                tmp = a->integer + b->tuple[i]; 
    55                 res->tuple[i] = tmp < 0 ? 0 : tmp > 255 ? 255 : tmp; // should it be a macro? 
     55                res->tuple[i] = orToByteRange(tmp); 
    5656            }    
    5757 
     
    6161        a = b; 
    6262    } 
    63     else if( a->type == OT_DECIMAL ) 
    64     { 
    65         if( b->type == OT_INTEGER ) 
     63    else if( orIs(a, OT_DECIMAL) ) 
     64    { 
     65        if( orIs(b, OT_INTEGER) ) 
    6666        { 
    6767            orResultDECIMAL( a->num.decimal + (double) b->integer ); 
    6868            return; 
    6969        } 
    70         else if( b->type == OT_DECIMAL ) 
     70        else if( orIs(b, OT_DECIMAL) ) 
    7171        { 
    7272            orResultDECIMAL( a->num.decimal + b->num.decimal ); 
    7373            return; 
    7474        } 
    75         else if( b->type == OT_TUPLE ) 
     75        else if( orIs(b, OT_TUPLE) ) 
    7676        { 
    7777            int tmp, i; 
     
    8383            { 
    8484                tmp = (int) a->num.decimal + b->tuple[i]; 
    85                 res->tuple[i] = tmp < 0 ? 0 : tmp > 255 ? 255 : tmp; 
     85                res->tuple[i] = orToByteRange(tmp); 
    8686            }    
    8787 
     
    9191        a = b; 
    9292    } 
    93     else if( a->type == OT_TUPLE ) 
    94     { 
    95         if( b->type == OT_TUPLE ) 
     93    else if( orIs(a, OT_TUPLE) ) 
     94    { 
     95        if( orIs(b, OT_TUPLE) ) 
    9696        { 
    9797            int tmp, i; 
     
    104104            { 
    105105                tmp = a->tuple[i] + b->tuple[i]; 
    106                 res->tuple[i] = tmp > 255 ? 255 : tmp; 
    107             } 
    108             return; 
    109         } 
    110         else if( b->type == OT_INTEGER ) 
     106                res->tuple[i] = orToByteRange(tmp); 
     107            } 
     108            return; 
     109        } 
     110        else if( orIs(b, OT_INTEGER) ) 
    111111        { 
    112112            int tmp, i; 
     
    118118            { 
    119119                tmp = a->tuple[i] + b->integer; 
    120                 res->tuple[i] = tmp < 0 ? 0 : tmp > 255 ? 255 : tmp; 
    121             }             
    122             return; 
    123         } 
    124         else if( b->type == OT_DECIMAL) 
     120                res->tuple[i] = orToByteRange(tmp); 
     121            }             
     122            return; 
     123        } 
     124        else if( orIs(b, OT_DECIMAL) ) 
    125125        { 
    126126            int tmp, i; 
     
    132132            { 
    133133                tmp = a->tuple[i] + (int) b->num.decimal; 
    134                 res->tuple[i] = tmp < 0 ? 0 : tmp > 255 ? 255 : tmp; 
    135             }             
    136             return; 
    137         } 
    138         a = b; 
    139     } 
    140     else if( a->type == OT_TIME ) 
    141     { 
    142         if( b->type == OT_TIME ) 
     134                res->tuple[i] = orToByteRange(tmp); 
     135            }             
     136            return; 
     137        } 
     138        a = b; 
     139    } 
     140    else if( orIs(a, OT_TIME) ) 
     141    { 
     142        if( orIs(b, OT_TIME) ) 
    143143        { 
    144144            OTime time; 
     
    166166{ 
    167167    OValue* b = a + 1; 
    168     if( a->type == OT_INTEGER ) 
    169     { 
    170         if( b->type == OT_INTEGER ) 
     168    if( orIs(a, OT_INTEGER) ) 
     169    { 
     170        if( orIs(b, OT_INTEGER) ) 
    171171        { 
    172172            orResult( OT_INTEGER, a->integer - b->integer ); 
    173173            return; 
    174174        } 
    175         else if( b->type == OT_DECIMAL ) 
     175        else if( orIs(b, OT_DECIMAL) ) 
    176176        { 
    177177            orResultDECIMAL( (double) a->integer - b->num.decimal ); 
     
    181181        a = b; 
    182182    } 
    183     else if( a->type == OT_DECIMAL ) 
    184     { 
    185         if( b->type == OT_INTEGER ) 
     183    else if( orIs(a, OT_DECIMAL) ) 
     184    { 
     185        if( orIs(b, OT_INTEGER) ) 
    186186        { 
    187187            orResultDECIMAL( a->num.decimal - (double) b->integer ); 
    188188            return; 
    189189        } 
    190         else if( b->type == OT_DECIMAL ) 
     190        else if( orIs(b, OT_DECIMAL) ) 
    191191        { 
    192192            orResultDECIMAL( a->num.decimal - b->num.decimal ); 
     
    195195        a = b; 
    196196    } 
    197     else if( a->type == OT_TUPLE ) 
    198     { 
    199         if( b->type == OT_TUPLE ) 
     197    else if( orIs(a, OT_TUPLE) ) 
     198    { 
     199        if( orIs(b, OT_TUPLE) ) 
    200200        { 
    201201            int tmp, i; 
     
    208208            { 
    209209                tmp = a->tuple[i] - b->tuple[i]; 
    210                 res->tuple[i] = tmp < 0 ? 0 : tmp; 
    211             } 
    212             return; 
    213         } 
    214         else if( b->type == OT_INTEGER ) 
     210                res->tuple[i] = orToByteRange(tmp); 
     211            } 
     212            return; 
     213        } 
     214        else if( orIs(b, OT_INTEGER) ) 
    215215        { 
    216216            int tmp, i; 
     
    222222            { 
    223223                tmp = a->tuple[i] - b->integer; 
    224                 res->tuple[i] = tmp < 0 ? 0 : tmp > 255 ? 255 : tmp; 
    225             }             
    226             return; 
    227         } 
    228         else if( b->type == OT_DECIMAL) 
     224                res->tuple[i] = orToByteRange(tmp); 
     225            }             
     226            return; 
     227        } 
     228        else if( orIs(b, OT_DECIMAL) ) 
    229229        { 
    230230            int tmp, i; 
     
    236236            { 
    237237                tmp = a->tuple[i] - (int) b->num.decimal; 
    238                 res->tuple[i] = tmp < 0 ? 0 : tmp > 255 ? 255 : tmp; 
    239             }             
    240             return; 
    241         } 
    242         a = b; 
    243     } 
    244     else if( a->type == OT_TIME ) 
    245     { 
    246         if( b->type == OT_TIME ) 
     238                res->tuple[i] = orToByteRange(tmp); 
     239            }             
     240            return; 
     241        } 
     242        a = b; 
     243    } 
     244    else if( orIs(a, OT_TIME) ) 
     245    { 
     246        if( orIs(b, OT_TIME) ) 
    247247        { 
    248248            OTime btime; 
     
    284284{ 
    285285    OValue* b = a + 1; 
    286     if( a->type == OT_INTEGER ) 
    287     { 
    288         if( b->type == OT_INTEGER ) 
     286    if( orIs(a, OT_INTEGER) ) 
     287    { 
     288        if( orIs(b, OT_INTEGER) ) 
    289289        { 
    290290            orResult( OT_INTEGER, a->integer * b->integer ); 
    291291            return; 
    292292        } 
    293         else if( b->type == OT_DECIMAL ) 
     293        else if( orIs(b, OT_DECIMAL) ) 
    294294        { 
    295295            orResultDECIMAL( (double) a->integer * b->num.decimal ); 
    296296            return; 
    297297        } 
    298         else if( b->type == OT_TUPLE ) 
     298        else if( orIs(b, OT_TUPLE) ) 
    299299        { 
    300300            int tmp, i; 
     
    306306            { 
    307307                tmp = a->integer * b->tuple[i]; 
    308                 res->tuple[i] = tmp < 0 ? 0 : tmp > 255 ? 255 : tmp; 
     308                res->tuple[i] = orToByteRange(tmp); 
    309309            }    
    310310 
     
    314314        a = b; 
    315315    } 
    316     else if( a->type == OT_DECIMAL ) 
    317     { 
    318         if( b->type == OT_INTEGER ) 
     316    else if( orIs(a, OT_DECIMAL) ) 
     317    { 
     318        if( orIs(b, OT_INTEGER) ) 
    319319        { 
    320320            orResultDECIMAL( a->num.decimal * (double) b->integer ); 
    321321            return; 
    322322        } 
    323         else if( b->type == OT_DECIMAL ) 
     323        else if( orIs(b, OT_DECIMAL) ) 
    324324        { 
    325325            orResultDECIMAL( a->num.decimal * b->num.decimal ); 
    326326            return; 
    327327        } 
    328         else if( b->type == OT_TUPLE ) 
     328        else if( orIs(b, OT_TUPLE) ) 
    329329        { 
    330330            int tmp, i; 
     
    336336            { 
    337337                tmp = a->num.decimal * b->tuple[i]; 
    338                 res->tuple[i] = tmp < 0 ? 0 : tmp > 255 ? 255 : tmp; 
     338                res->tuple[i] = orToByteRange(tmp); 
    339339            }    
    340340 
     
    344344        a = b; 
    345345    } 
    346     else if( a->type == OT_TUPLE ) 
    347     { 
    348         if( b->type == OT_TUPLE ) 
     346    else if( orIs(a, OT_TUPLE) ) 
     347    { 
     348        if( orIs(b, OT_TUPLE) ) 
    349349        { 
    350350            int tmp, i; 
     
    357357            { 
    358358                tmp = a->tuple[i] * b->tuple[i]; 
    359                 res->tuple[i] = tmp > 255 ? 255 : tmp; 
    360             } 
    361             return; 
    362         } 
    363         else if( b->type == OT_INTEGER ) 
     359                res->tuple[i] = orToByteRange(tmp); 
     360            } 
     361            return; 
     362        } 
     363        else if( orIs(b, OT_INTEGER) ) 
    364364        { 
    365365            int tmp, i; 
     
    371371            { 
    372372                tmp = a->tuple[i] * b->integer; 
    373                 res->tuple[i] = tmp < 0 ? 0 : tmp > 255 ? 255 : tmp; 
    374             }             
    375             return; 
    376         } 
    377         else if( b->type == OT_DECIMAL) 
     373                res->tuple[i] = orToByteRange(tmp); 
     374            }             
     375            return; 
     376        } 
     377        else if( orIs(b, OT_DECIMAL) ) 
    378378        { 
    379379            int tmp, i; 
     
    385385            { 
    386386                tmp = a->tuple[i] * b->num.decimal; 
    387                 res->tuple[i] = tmp < 0 ? 0 : tmp > 255 ? 255 : tmp; 
     387                res->tuple[i] = orToByteRange(tmp); 
    388388            }             
    389389            return; 
     
    398398{ 
    399399    OValue* b = a + 1; 
    400     if( a->type == OT_INTEGER ) 
    401     { 
    402         if( b->type == OT_INTEGER ) 
     400    if( orIs(a, OT_INTEGER) ) 
     401    { 
     402        if( orIs(b, OT_INTEGER) ) 
    403403        { 
    404404            if( b->integer == 0 ) 
     
    407407            return; 
    408408        } 
    409         else if( b->type == OT_DECIMAL ) 
     409        else if( orIs(b, OT_DECIMAL) ) 
    410410        { 
    411411            if( b->num.decimal == 0.0 ) 
     
    416416        a = b; 
    417417    } 
    418     else if( a->type == OT_DECIMAL ) 
    419     { 
    420         if( b->type == OT_INTEGER ) 
     418    else if( orIs(a, OT_DECIMAL) ) 
     419    { 
     420        if( orIs(b, OT_INTEGER) ) 
    421421        { 
    422422            if( b->integer == 0 ) 
     
    425425            return; 
    426426        } 
    427         else if( b->type == OT_DECIMAL ) 
     427        else if( orIs(b, OT_DECIMAL) ) 
    428428        { 
    429429            if( b->num.decimal == 0.0 ) 
     
    434434        a = b; 
    435435    } 
    436     else if( a->type == OT_TUPLE ) 
    437     { 
    438         if( b->type == OT_TUPLE ) 
     436    else if( orIs(a, OT_TUPLE) ) 
     437    { 
     438        if( orIs(b, OT_TUPLE) ) 
    439439        { 
    440440            int tmp, i; 
     
    449449                    goto div0; 
    450450                tmp = a->tuple[i] / b->tuple[i]; 
    451                 res->tuple[i] = tmp > 255 ? 255 : tmp; 
    452             } 
    453             return; 
    454         } 
    455         else if( b->type == OT_INTEGER ) 
     451                res->tuple[i] = orToByteRange(tmp); 
     452            } 
     453            return; 
     454        } 
     455        else if( orIs(b, OT_INTEGER) ) 
    456456        { 
    457457            int tmp, i; 
     
    465465                    goto div0; 
    466466                tmp = a->tuple[i] / b->integer; 
    467                 res->tuple[i] = tmp < 0 ? 0 : tmp > 255 ? 255 : tmp; 
    468             }             
    469             return; 
    470         } 
    471         else if( b->type == OT_DECIMAL) 
     467                res->tuple[i] = orToByteRange(tmp); 
     468            }             
     469            return; 
     470        } 
     471        else if( orIs(b, OT_DECIMAL) ) 
    472472        { 
    473473            int tmp, i; 
     
    481481                    goto div0; 
    482482                tmp = a->tuple[i] / b->num.decimal; 
    483                 res->tuple[i] = tmp < 0 ? 0 : tmp > 255 ? 255 : tmp; 
     483                res->tuple[i] = orToByteRange(tmp); 
    484484            }             
    485485            return; 
     
    554554        { 
    555555            int i, len;             
    556             len = orTupleLen(a); 
    557             if( len < orTupleLen(b) ) 
    558                 return CMP_LESS; 
    559             if( len > orTupleLen(b) ) 
    560                 return CMP_GREATER; 
     556            len = orTupleLen(a) > orTupleLen(b) ? orTupleLen(a) : orTupleLen(b); 
    561557            for( i = 0; i < len; i++ ) 
    562558            { 
     
    697693    int logic = 0; 
    698694 
    699     if( a->type == b->type ) 
    700     { 
    701         switch( a->type ) 
     695    if( orType(a) == orType(b) ) 
     696    { 
     697        switch( orType(a) ) 
    702698        { 
    703699            case OT_BLOCK: 
     
    750746int orEqual( const OValue* a, const OValue* b ) 
    751747{ 
    752     if( a->type == OT_INTEGER ) 
    753     { 
    754         if( b->type == OT_INTEGER ) 
     748    if( orIs(a, OT_INTEGER) ) 
     749    { 
     750        if( orIs(b, OT_INTEGER) ) 
    755751        { 
    756752            if( a->integer == b->integer ) 
    757753                return 1; 
    758754        } 
    759         else if( b->type == OT_DECIMAL ) 
     755        else if( orIs(b, OT_DECIMAL) ) 
    760756        { 
    761757            if( ((double) a->integer) == b->num.decimal ) 
     
    764760        return 0; 
    765761    } 
    766     else if( a->type == OT_DECIMAL ) 
    767     { 
    768         if( b->type == OT_INTEGER ) 
     762    else if( orIs(a, OT_DECIMAL) ) 
     763    { 
     764        if( orIs(b, OT_INTEGER) ) 
    769765        { 
    770766            if( a->num.decimal == ((double) b->integer) ) 
    771767                return 1; 
    772768        } 
    773         else if( b->type == OT_DECIMAL ) 
     769        else if( orIs(b, OT_DECIMAL) ) 
    774770        { 
    775771            if( a->num.decimal == b->num.decimal ) 
     
    809805    OValue* b = a + 1; 
    810806 
    811     if( a->type == OT_INTEGER ) 
    812     { 
    813         if( b->type == OT_INTEGER ) 
     807    if( orIs(a, OT_INTEGER) ) 
     808    { 
     809        if( orIs(b, OT_INTEGER) ) 
    814810            logic = (a->integer == b->integer); 
    815         else if( b->type == OT_DECIMAL ) 
     811        else if( orIs(b, OT_DECIMAL) ) 
    816812            logic = ((double) a->integer == b->num.decimal); 
    817813    } 
    818     else if( a->type == OT_DECIMAL ) 
    819     { 
    820         if( b->type == OT_INTEGER ) 
     814    else if( orIs(a, OT_DECIMAL) ) 
     815    { 
     816        if( orIs(b, OT_INTEGER) ) 
    821817            logic = (a->num.decimal == (double) b->integer); 
    822         else if( b->type == OT_DECIMAL ) 
     818        else if( orIs(b, OT_DECIMAL) ) 
    823819            logic = (a->num.decimal == b->num.decimal); 
    824820    } 
    825     else if( a->type == b->type ) 
    826     { 
    827         if( a->type == OT_WORD ) 
     821    else if( orType(a) == orType(b) ) 
     822    { 
     823        if( orIs(a, OT_WORD) ) // Should it be orIsWord(a) 
    828824        { 
    829825            if( (a->word.atom == b->word.atom) && 
     
    831827                logic = 1; 
    832828        } 
    833         else if( a->type == OT_OBJECT ) 
     829        else if( orIs(a, OT_OBJECT) ) 
    834830        { 
    835831            if( a->index == b->index ) 
     
    842838                logic = 1; 
    843839        } 
    844         else if( a->type == OT_TUPLE ) 
     840        else if( orIs(a, OT_TUPLE) ) 
    845841        { 
    846842            logic = compareValue( a, b ); // tupleSame( a, b ); 
     
    896892{ 
    897893    OValue* b = a + 1; 
    898     if( a->type == OT_INTEGER ) 
    899     { 
    900         if( b->type == OT_INTEGER ) 
     894    if( orIs(a, OT_INTEGER) ) 
     895    { 
     896        if( orIs(b, OT_INTEGER) ) 
    901897        { 
    902898            orResult( OT_INTEGER, a->integer & b->integer ); 
     
    905901        a = b; 
    906902    } 
    907     else if( a->type == OT_LOGIC ) 
    908     { 
    909         if( b->type == OT_LOGIC ) 
     903    else if( orIs(a, OT_LOGIC) ) 
     904    { 
     905        if( orIs(b, OT_LOGIC) ) 
    910906        { 
    911907            orResult( OT_LOGIC, a->integer & b->integer ); 
    912908            return; 
    913909        } 
    914         else if( b->type == OT_NONE ) 
     910        else if( orIs(b, OT_NONE) ) 
    915911        { 
    916912            // Weird - cannot take none as first argument. 
     
    920916        a = b; 
    921917    } 
    922     else if( a->type == OT_TUPLE ) 
    923     { 
    924         if( b->type == OT_TUPLE ) 
     918    else if( orIs(a, OT_TUPLE) ) 
     919    { 
     920        if( orIs(b, OT_TUPLE) ) 
    925921        { 
    926922            int argc, i;             
     
    932928            return; 
    933929        } 
    934         else if( b->type == OT_INTEGER ) 
     930        else if( orIs(b, OT_INTEGER) ) 
    935931        { 
    936932            int i; 
     
    941937            return; 
    942938        } 
    943         else if( b->type == OT_DECIMAL) 
     939        else if( orIs(b, OT_DECIMAL) ) 
    944940        { 
    945941            int i; 
     
    959955{ 
    960956    OValue* b = a + 1; 
    961     if( a->type == OT_INTEGER ) 
    962     { 
    963         if( b->type == OT_INTEGER ) 
     957    if( orIs(a, OT_INTEGER) ) 
     958    { 
     959        if( orIs(b, OT_INTEGER) ) 
    964960        { 
    965961            orResult( OT_INTEGER, a->integer | b->integer ); 
     
    968964        a = b; 
    969965    } 
    970     else if( a->type == OT_LOGIC ) 
    971     { 
    972         if( b->type == OT_LOGIC ) 
     966    else if( orIs(a, OT_LOGIC) ) 
     967    { 
     968        if( orIs(b, OT_LOGIC) ) 
    973969        { 
    974970            orResult( OT_LOGIC, a->integer | b->integer ); 
     
    977973        a = b; 
    978974    } 
    979     else if( a->type == OT_TUPLE ) 
    980     { 
    981         if( b->type == OT_TUPLE ) 
     975    else if( orIs(a, OT_TUPLE) )