Changeset 175 for trunk/thune/eval.c

Show
Ignore:
Timestamp:
06/08/06 22:00:46 (3 years ago)
Author:
krobillard
Message:

Thune - Added 'div. Algebraic operators now accept int!/decimal!.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/thune/eval.c

    r172 r175  
    11931193 
    11941194 
    1195 // (number number -- number) 
     1195#define MJ_INT_INT  0 
     1196#define MJ_INT_DEC  1 
     1197#define MJ_DEC_INT  2 
     1198#define MJ_DEC_DEC  3 
     1199#define MJ_NAN      4 
     1200 
     1201static int _mathJumpIndex( const UCell* a, const UCell* b ) 
     1202{ 
     1203    if( ur_is(a, UT_INT) ) 
     1204    { 
     1205        if( ur_is(b, UT_INT) ) 
     1206            return MJ_INT_INT; 
     1207        if( ur_is(b, UT_DECIMAL) ) 
     1208            return MJ_INT_DEC; 
     1209    } 
     1210    else if( ur_is(a, UT_DECIMAL) ) 
     1211    { 
     1212        if( ur_is(b, UT_INT) ) 
     1213            return MJ_DEC_INT; 
     1214        if( ur_is(b, UT_DECIMAL) ) 
     1215            return MJ_DEC_DEC; 
     1216    } 
     1217    return MJ_NAN; 
     1218} 
     1219 
     1220 
     1221#define MATH_OPERATION(OP) \ 
     1222    switch( _mathJumpIndex(res, tos) ) { \ 
     1223        case MJ_INT_INT: \ 
     1224            ur_int(res) = ur_int(res) OP ur_int(tos); \ 
     1225            break; \ 
     1226        case MJ_INT_DEC: \ 
     1227            ur_setType(res, UT_DECIMAL); \ 
     1228            ur_decimal(res) = ((double) ur_int(res)) OP ur_decimal(tos); \ 
     1229            break; \ 
     1230        case MJ_DEC_INT: \ 
     1231            ur_decimal(res) = ur_decimal(res) OP ((double) ur_int(tos)); \ 
     1232            break; \ 
     1233        case MJ_DEC_DEC: \ 
     1234            ur_decimal(res) = ur_decimal(res) OP ur_decimal(tos); \ 
     1235            break; \ 
     1236        case MJ_NAN: \ 
     1237            ur_throwErr( ur_thread, UR_EX_DATATYPE, \ 
     1238                         "Math operator requires int!/decimal!" ); \ 
     1239            break; \ 
     1240    } 
     1241 
     1242 
     1243// (number number -- sum) 
    11961244UR_CALL( uc_add ) 
    11971245{ 
     
    12031251    else 
    12041252    { 
    1205         ur_int(UR_TOS) += ur_int(tos); 
    1206     } 
    1207 } 
    1208  
    1209  
    1210 // (number number -- diff) 
     1253        UCell* res = UR_TOS; 
     1254        MATH_OPERATION( + ) 
     1255    } 
     1256} 
     1257 
     1258 
     1259// (number number -- difference) 
    12111260UR_CALL( uc_sub ) 
    12121261{ 
     
    12181267    else 
    12191268    { 
    1220         ur_int(UR_TOS) -= ur_int(tos); 
    1221     } 
    1222 } 
    1223  
    1224  
    1225 // (number number -- number) 
     1269        UCell* res = UR_TOS; 
     1270        MATH_OPERATION( - ) 
     1271    } 
     1272} 
     1273 
     1274 
     1275// (number number -- product) 
    12261276UR_CALL( uc_mult ) 
    12271277{ 
     1278    UCell* res; 
     1279 
    12281280    UR_S_DROP; 
    1229     ur_int(UR_TOS) *= ur_int(tos); 
     1281    res = UR_TOS; 
     1282 
     1283    MATH_OPERATION( * ) 
     1284} 
     1285 
     1286 
     1287// (number number -- quotient) 
     1288UR_CALL( uc_div ) 
     1289{ 
     1290    UCell* res; 
     1291 
     1292    UR_S_DROP; 
     1293    res = UR_TOS; 
     1294 
     1295    MATH_OPERATION( / ) 
    12301296} 
    12311297 
     
    19542020    { uc_sub,          "sub" }, 
    19552021    { uc_mult,         "mult" }, 
     2022    { uc_div,          "div" }, 
    19562023    { uc_equal,        "eq?" }, 
    19572024    { uc_sameQ,        "same?" },