Changeset 1721 for trunk/src/kmk


Ignore:
Timestamp:
Sep 4, 2008, 5:19:08 AM (17 years ago)
Author:
bird
Message:

kmk: Some more ifcond operators.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/ifcond.c

    r1719 r1721  
    706706
    707707/**
     708 * Logical NOT.
     709 *
     710 * @returns Status code.
     711 * @param   pThis       The instance.
     712 */
     713static IFCONDRET ifcond_op_logical_not(PIFCOND pThis)
     714{
     715    PIFCONDVAR pVar = &pThis->aVars[pThis->iVar];
     716    assert(pThis->iVar >= 0);
     717
     718    ifcond_var_assign_bool(pVar, !ifcond_var_make_bool(pVar));
     719
     720    return kIfCondRet_Ok;
     721}
     722
     723
     724/////
     725
     726
     727/**
     728 * Not equal.
     729 *
     730 * @returns Status code.
     731 * @param   pThis       The instance.
     732 */
     733static IFCONDRET ifcond_op_equal(PIFCOND pThis)
     734{
     735    PIFCONDVAR  pVar1 = &pThis->aVars[pThis->iVar - 1];
     736    PIFCONDVAR  pVar2 = &pThis->aVars[pThis->iVar];
     737    assert(pThis->iVar >= 1);
     738
     739    /*
     740     * If it's the same type, things are simple.
     741     */
     742    if (ifcond_var_is_string(pVar1) == ifcond_var_is_string(pVar2))
     743    {
     744        if (ifcond_var_is_string(pVar1))
     745        {
     746            ifcond_var_make_simple_string(pVar1);
     747            ifcond_var_make_simple_string(pVar2);
     748            ifcond_var_assign_bool(pVar1, !strcmp(pVar1->uVal.psz, pVar2->uVal.psz));
     749        }
     750        else
     751            ifcond_var_assign_bool(pVar1, pVar1->uVal.i == pVar2->uVal.i);
     752    }
     753    else
     754    {
     755        /*
     756         * If the type differs, there are now two options:
     757         *  1. Convert the string to a valid number and compare the numbers.
     758         *  2. Convert an empty string to a 'false' boolean value and compare
     759         *     numerically. This one is a bit questionable, so it's currently
     760         *     not enabled.
     761         */
     762        IFCONDINT64 iVal1;
     763        PIFCONDVAR  pVarRet = pVar1;
     764
     765        /* switch so pVar1 is the string. */
     766        if (!ifcond_var_is_string(pVar1))
     767        {
     768            pVar1 = pVar2;
     769            pVar2 = pVarRet;
     770        }
     771
     772        ifcond_var_make_simple_string(pVar1);
     773        if (ifcond_string_to_num(NULL, &iVal1, pVar1->uVal.psz, 1 /* fQuiet */) >= kIfCondRet_Ok)
     774            ifcond_var_assign_bool(pVar1, iVal1 == pVar2->uVal.i);
     775        else
     776        {
     777#if 0 /* bogus consept */
     778            const char *psz = pVar1->uVal.psz;
     779            while (isspace((unsigned char)*psz))
     780                psz++;
     781            if (!*psz)
     782                ifcond_var_assign_bool(pVar1, iVal1 == pVar2->uVal.i);
     783            else
     784#endif
     785            {
     786                ifcond_error(pThis, "Cannot compare strings and numbers");
     787                return kIfCondRet_Error;
     788            }
     789        }
     790    }
     791
     792    ifcond_pop_and_delete_var(pThis);
     793    return kIfCondRet_Ok;
     794}
     795
     796
     797/**
     798 * Not equal.
     799 *
     800 * @returns Status code.
     801 * @param   pThis       The instance.
     802 */
     803static IFCONDRET ifcond_op_not_equal(PIFCOND pThis)
     804{
     805    IFCONDRET rc = ifcond_op_equal(pThis);
     806    if (rc >= kIfCondRet_Ok)
     807        rc = ifcond_op_logical_not(pThis);
     808    return rc;
     809}
     810
     811
     812/**
     813 * Bitwise AND.
     814 *
     815 * @returns Status code.
     816 * @param   pThis       The instance.
     817 */
     818static IFCONDRET ifcond_op_bitwise_and(PIFCOND pThis)
     819{
     820    IFCONDRET rc;
     821    assert(pThis->iVar >= 1);
     822
     823    rc = ifcond_var_make_num(pThis, &pThis->aVars[pThis->iVar - 1]);
     824    if (rc >= kIfCondRet_Ok)
     825    {
     826        rc = ifcond_var_make_num(pThis, &pThis->aVars[pThis->iVar]);
     827        if (rc >= kIfCondRet_Ok)
     828            pThis->aVars[pThis->iVar - 1].uVal.i &= pThis->aVars[pThis->iVar].uVal.i;
     829    }
     830
     831    ifcond_pop_and_delete_var(pThis);
     832    return kIfCondRet_Ok;
     833}
     834
     835
     836/**
     837 * Bitwise XOR.
     838 *
     839 * @returns Status code.
     840 * @param   pThis       The instance.
     841 */
     842static IFCONDRET ifcond_op_bitwise_xor(PIFCOND pThis)
     843{
     844    IFCONDRET rc;
     845    assert(pThis->iVar >= 1);
     846
     847    rc = ifcond_var_make_num(pThis, &pThis->aVars[pThis->iVar - 1]);
     848    if (rc >= kIfCondRet_Ok)
     849    {
     850        rc = ifcond_var_make_num(pThis, &pThis->aVars[pThis->iVar]);
     851        if (rc >= kIfCondRet_Ok)
     852            pThis->aVars[pThis->iVar - 1].uVal.i ^= pThis->aVars[pThis->iVar].uVal.i;
     853    }
     854
     855    ifcond_pop_and_delete_var(pThis);
     856    return kIfCondRet_Ok;
     857}
     858
     859
     860/**
    708861 * Bitwise OR.
    709862 *
     
    843996    IFCOND_OP(">=",          60,      2,    ifcond_op_greater_or_equal_than),
    844997    IFCOND_OP(">",           60,      2,    ifcond_op_greater_than),
     998#endif
    845999    IFCOND_OP("==",          55,      2,    ifcond_op_equal),
    8461000    IFCOND_OP("!=",          55,      2,    ifcond_op_not_equal),
    8471001    IFCOND_OP("!",           80,      1,    ifcond_op_logical_not),
    8481002    IFCOND_OP("^",           45,      2,    ifcond_op_bitwise_xor),
    849 #endif
    8501003    IFCOND_OP("&&",          35,      2,    ifcond_op_logical_and),
    851     /*IFCOND_OP("&",           50,      2,    ifcond_op_bitwise_and),*/
     1004    IFCOND_OP("&",           50,      2,    ifcond_op_bitwise_and),
    8521005    IFCOND_OP("||",          30,      2,    ifcond_op_logical_or),
    8531006    IFCOND_OP("|",           40,      2,    ifcond_op_bitwise_or),
     
    13311484
    13321485#endif /* CONFIG_WITH_IF_CONDITIONALS */
     1486
Note: See TracChangeset for help on using the changeset viewer.