Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1894 for code


Ignore:
Timestamp:
Oct 8, 2008, 12:24:58 AM (16 years ago)
Author:
rgrieder
Message:
  • Converted ExprParser to 4 spaces/tab
  • Tiny little bug in String.cc
Location:
code/trunk/src/util
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/util/ExprParser.cc

    r1639 r1894  
    4343
    4444//! skip white spaces
    45 #define PARSE_BLANKS while (*reading_stream == ' ') ++reading_stream;
     45#define PARSE_BLANKS while (*reading_stream == ' ') ++reading_stream
    4646
    4747ExprParser::ExprParser(const std::string& str)
    4848{
    49   this->failed_ = false;
    50   this->reading_stream = str.c_str();
    51   if (str.size() == 0 || *reading_stream == '\0')
    52   {
    53     this->failed_ = true;
    54     this->result_ = 0.0;
    55   }
    56   else
    57   {
    58     this->result_ = parse_expr_8();
    59     this->remains_ = reading_stream;
    60   }
     49    this->failed_ = false;
     50    this->reading_stream = str.c_str();
     51    if (str.size() == 0 || *reading_stream == '\0')
     52    {
     53        this->failed_ = true;
     54        this->result_ = 0.0;
     55    }
     56    else
     57    {
     58        this->result_ = parse_expr_8();
     59        this->remains_ = reading_stream;
     60    }
    6161}
    6262
     
    6565double ExprParser::parse_argument()
    6666{
    67    double value = parse_expr_8();
    68    if (*reading_stream == ',')
    69    {
    70       ++reading_stream;
    71       return value;
    72    }
    73    else
    74    {
    75      this->failed_ = true;
    76      return 0;
    77    }
     67    double value = parse_expr_8();
     68    if (*reading_stream == ',')
     69    {
     70        ++reading_stream;
     71        return value;
     72    }
     73    else
     74    {
     75        this->failed_ = true;
     76        return 0;
     77    }
    7878}
    7979
    8080double ExprParser::parse_last_argument()
    8181{
    82    double value = parse_expr_8();
    83    if (*reading_stream == ')')
    84    {
    85       ++reading_stream;
    86       return value;
    87    }
    88    else
    89    {
    90      this->failed_ = true;
    91      return 0;
    92    }
     82    double value = parse_expr_8();
     83    if (*reading_stream == ')')
     84    {
     85        ++reading_stream;
     86        return value;
     87    }
     88    else
     89    {
     90        this->failed_ = true;
     91        return 0;
     92    }
    9393}
    9494
    9595double ExprParser::parse_expr_8()
    9696{
    97    double value = parse_expr_7();
    98    for(;;)
    99    {
    100       switch (op)
    101       {
    102          case oder:
     97    double value = parse_expr_7();
     98    for(;;)
     99    {
     100        switch (op)
     101        {
     102        case oder:
    103103            value = parse_expr_7() || value;
    104104            break;
    105          default: return value;
    106       }
    107    };
     105        default: return value;
     106        }
     107    };
    108108}
    109109
     
    111111double ExprParser::parse_expr_7()
    112112{
    113    double value = parse_expr_6();
    114    for(;;)
    115    {
    116       switch (op)
    117       {
    118          case und:
     113    double value = parse_expr_6();
     114    for(;;)
     115    {
     116        switch (op)
     117        {
     118        case und:
    119119            value = value && parse_expr_6();
    120120            break;
    121          default: return value;
    122       }
    123    };
     121        default: return value;
     122        }
     123    };
    124124}
    125125
    126126double ExprParser::parse_expr_6()
    127127{
    128    double value = parse_expr_5();
    129    for(;;)
    130    {
    131       switch (op)
    132       {
    133          case gleich:
     128    double value = parse_expr_5();
     129    for(;;)
     130    {
     131        switch (op)
     132        {
     133        case gleich:
    134134            value = (value == parse_expr_5());
    135135            break;
    136          case ungleich:
     136        case ungleich:
    137137            value = (value != parse_expr_5());
    138138            break;
    139          default:
     139        default:
    140140            return value;
    141       }
    142    };
     141        }
     142    };
    143143}
    144144
    145145double ExprParser::parse_expr_5()
    146146{
    147    double value = parse_expr_4();
    148    for(;;)
    149    {
    150       switch (op)
    151       {
    152          case kleiner:
     147    double value = parse_expr_4();
     148    for(;;)
     149    {
     150        switch (op)
     151        {
     152        case kleiner:
    153153            value = (value < parse_expr_4());
    154154            break;
    155          case kleinergleich:
     155        case kleinergleich:
    156156            value = (value <= parse_expr_4());
    157157            break;
    158          case groesser:
     158        case groesser:
    159159            value = (value > parse_expr_4());
    160160            break;
    161          case groessergleich:
     161        case groessergleich:
    162162            value = (value >= parse_expr_4());
    163163            break;
    164          default:
     164        default:
    165165            return value;
    166       }
    167    };
     166        }
     167    };
    168168}
    169169
    170170double ExprParser::parse_expr_4()
    171171{
    172    double value = parse_expr_3();
    173    for(;;)
    174    {
    175       switch (op)
    176       {
    177          case b_plus:
     172    double value = parse_expr_3();
     173    for(;;)
     174    {
     175        switch (op)
     176        {
     177        case b_plus:
    178178            value += parse_expr_3();
    179179            break;
    180          case b_minus:
     180        case b_minus:
    181181            value -= parse_expr_3();
    182182            break;
    183          default:
     183        default:
    184184            return value;
    185       }
    186    };
     185        }
     186    };
    187187}
    188188
    189189double ExprParser::parse_expr_3()
    190190{
    191    double value = parse_expr_2();
    192    for(;;)
    193    {
    194       switch (op)
    195       {
    196          case mal:
     191    double value = parse_expr_2();
     192    for(;;)
     193    {
     194        switch (op)
     195        {
     196        case mal:
    197197            value *= parse_expr_2();
    198198            break;
    199          case durch:
     199        case durch:
    200200            value /= parse_expr_2();
    201201            break;
    202          case modulo:
    203          {
    204             double temp = parse_expr_2();
    205             value = value - floor(value/temp)*temp;
    206             break;
    207          }
    208          default:
     202        case modulo:
     203            {
     204                double temp = parse_expr_2();
     205                value = value - floor(value/temp)*temp;
     206                break;
     207            }
     208        default:
    209209            return value;
    210       }
    211    };
     210        }
     211    };
    212212}
    213213
    214214double ExprParser::parse_expr_2()
    215215{
    216    double value = parse_expr_1();
    217    while (*reading_stream != '\0')
    218    {
    219       op = parse_binary_operator();
    220       switch (op)
    221       {
    222          case hoch:
     216    double value = parse_expr_1();
     217    while (*reading_stream != '\0')
     218    {
     219        op = parse_binary_operator();
     220        switch (op)
     221        {
     222        case hoch:
    223223            value = pow(value,parse_expr_1());
    224224            break;
    225          default:
     225        default:
    226226            return value;
    227       }
    228    };
    229    op = undef;
    230    return value;
     227        }
     228    };
     229    op = undef;
     230    return value;
    231231}
    232232
    233233double ExprParser::parse_expr_1()
    234234{
    235    PARSE_BLANKS
    236    double value;
    237 
    238    unary_operator op = parse_unary_operator();
    239    PARSE_BLANKS
    240 
    241    if (*reading_stream == '\0')
    242    {
    243      // end of string
    244      this->failed_ = true;
    245      return 0;
    246    }
    247    else if (*reading_stream > 47 && *reading_stream < 59 || *reading_stream == 46)
    248    {  // number
    249       value = strtod(reading_stream, const_cast<char**>(&reading_stream));
    250    }
    251    else if (*reading_stream > 64 && *reading_stream < 91 || *reading_stream > 96 && *reading_stream < 123 || *reading_stream == 46)
    252    {  // variable or function
    253       char* word = new char[256];
    254       parse_word(word);
    255       PARSE_BLANKS
    256       if (*reading_stream == '(')
    257       {
    258          ++reading_stream;
     235    PARSE_BLANKS;
     236    double value;
     237
     238    unary_operator op = parse_unary_operator();
     239    PARSE_BLANKS;
     240
     241    if (*reading_stream == '\0')
     242    {
     243        // end of string
     244        this->failed_ = true;
     245        return 0;
     246    }
     247    else if (*reading_stream > 47 && *reading_stream < 59 || *reading_stream == 46)
     248    {  // number
     249        value = strtod(reading_stream, const_cast<char**>(&reading_stream));
     250    }
     251    else if (*reading_stream > 64 && *reading_stream < 91 || *reading_stream > 96 && *reading_stream < 123 || *reading_stream == 46)
     252    {  // variable or function
     253        char* word = new char[256];
     254        parse_word(word);
     255        PARSE_BLANKS;
     256        if (*reading_stream == '(')
     257        {
     258            ++reading_stream;
    259259#define SWITCH word
    260          CASE_1("sin")
    261             value = sin(parse_last_argument());
    262          CASE("asin")
    263             value = asin(parse_last_argument());
    264          CASE("sinh")
    265             value = sinh(parse_last_argument());
    266          CASE("asinh")
    267          {
    268             value = parse_last_argument();
    269             value = log(sqrt(pow(value, 2) + 1) + value);
    270          }
    271          CASE("cos")
    272             value = cos(parse_last_argument());
    273          CASE("acos")
    274             value = acos(parse_last_argument());
    275          CASE("cosh")
    276             value = cosh(parse_last_argument());
    277          CASE("acosh")
    278          {
    279             value = parse_last_argument();
    280             value = log(sqrt(pow(value, 2) - 1) + value);
    281          }
    282          CASE("tan")
    283             value = tan(parse_last_argument());
    284          CASE("atan")
    285             value = atan(parse_last_argument());
    286          CASE("atan2")
    287             value = atan2(parse_argument(),parse_last_argument());
    288          CASE("tanh")
    289             value = tanh(parse_last_argument());
    290          CASE("atanh")
    291          {
    292             value = parse_last_argument();
    293             value = 0.5*log((value + 1)/(value - 1));
    294          }
    295          CASE("int")
    296             value = floor(parse_last_argument());
    297          CASE("floor")
    298             value = floor(parse_last_argument());
    299          CASE("ceil")
    300             value = ceil(parse_last_argument());
    301          CASE("abs")
    302             value = fabs(parse_last_argument());
    303          CASE("exp")
    304             value = exp(parse_last_argument());
    305          CASE("log")
    306             value = log10(parse_last_argument());
    307          CASE("ln")
    308             value = log(parse_last_argument());
    309          CASE("sign")
    310          {
    311             value = parse_last_argument();
    312             value = (value>0 ? 1 : (value<0 ? -1 : 0));
    313          }
    314          CASE("sqrt")
    315             value = sqrt(parse_last_argument());
    316          CASE("degrees")
    317             value = parse_last_argument()*180/3.1415926535897932;
    318          CASE("radians")
    319             value = parse_last_argument()*3.1415926535897932/180;
    320          CASE("mod")
    321          {
    322             value = parse_argument();
    323             double value2 = parse_last_argument();
    324             value = value - floor(value/value2)*value2;
    325          }
    326          CASE("pow")
    327             value = pow(parse_argument(),parse_last_argument());
    328          CASE("div")
    329             value = floor(parse_argument()/parse_last_argument());
    330          CASE("max")
    331            value = std::max(parse_argument(),parse_last_argument());
    332          CASE("min")
    333            value = std::min(parse_argument(),parse_last_argument());
    334          CASE_ELSE
    335          {
    336            this->failed_ = true;
    337            delete[] word;
    338            return 0;
    339          }
    340       }
    341       else
    342       {
     260            CASE_1("sin")
     261                value = sin(parse_last_argument());
     262            CASE("asin")
     263                value = asin(parse_last_argument());
     264            CASE("sinh")
     265                value = sinh(parse_last_argument());
     266            CASE("asinh")
     267            {
     268                value = parse_last_argument();
     269                value = log(sqrt(pow(value, 2) + 1) + value);
     270            }
     271            CASE("cos")
     272                value = cos(parse_last_argument());
     273            CASE("acos")
     274                value = acos(parse_last_argument());
     275            CASE("cosh")
     276                value = cosh(parse_last_argument());
     277            CASE("acosh")
     278            {
     279                value = parse_last_argument();
     280                value = log(sqrt(pow(value, 2) - 1) + value);
     281            }
     282            CASE("tan")
     283                value = tan(parse_last_argument());
     284            CASE("atan")
     285                value = atan(parse_last_argument());
     286            CASE("atan2")
     287                value = atan2(parse_argument(),parse_last_argument());
     288            CASE("tanh")
     289                value = tanh(parse_last_argument());
     290            CASE("atanh")
     291            {
     292                value = parse_last_argument();
     293                value = 0.5*log((value + 1)/(value - 1));
     294            }
     295            CASE("int")
     296                value = floor(parse_last_argument());
     297            CASE("floor")
     298                value = floor(parse_last_argument());
     299            CASE("ceil")
     300                value = ceil(parse_last_argument());
     301            CASE("abs")
     302                value = fabs(parse_last_argument());
     303            CASE("exp")
     304                value = exp(parse_last_argument());
     305            CASE("log")
     306                value = log10(parse_last_argument());
     307            CASE("ln")
     308                value = log(parse_last_argument());
     309            CASE("sign")
     310            {
     311                value = parse_last_argument();
     312                value = (value>0 ? 1 : (value<0 ? -1 : 0));
     313            }
     314            CASE("sqrt")
     315                value = sqrt(parse_last_argument());
     316            CASE("degrees")
     317                value = parse_last_argument()*180/3.1415926535897932;
     318            CASE("radians")
     319                value = parse_last_argument()*3.1415926535897932/180;
     320            CASE("mod")
     321            {
     322                value = parse_argument();
     323                double value2 = parse_last_argument();
     324                value = value - floor(value/value2)*value2;
     325            }
     326            CASE("pow")
     327                value = pow(parse_argument(),parse_last_argument());
     328            CASE("div")
     329                value = floor(parse_argument()/parse_last_argument());
     330            CASE("max")
     331                value = std::max(parse_argument(),parse_last_argument());
     332            CASE("min")
     333                value = std::min(parse_argument(),parse_last_argument());
     334            CASE_ELSE
     335            {
     336                this->failed_ = true;
     337                delete[] word;
     338                return 0;
     339            }
     340        }
     341        else
     342        {
    343343#define SWITCH word
    344          CASE_1("pi")
    345            value = 3.1415926535897932;
    346          CASE("e")
    347            value = 2.7182818284590452;
    348          CASE_ELSE
    349          {
    350            this->failed_ = true;
    351            delete[] word;
    352            return 0;
    353          }
    354       }
    355       delete[] word;
    356    }
    357    else if (*reading_stream == 40)
    358    {  // expresion in paranthesis
    359       ++reading_stream;
    360       value = parse_last_argument();
    361    }
    362    else
    363    {
    364      this->failed_ = true;
    365      return 0;
    366    }
    367 
    368    PARSE_BLANKS
    369    switch (op)
    370    {
    371       case u_nicht: return !value;
    372       case u_plus:  return  value;
    373       case u_minus: return -value;
    374       default:
    375         {
    376           this->failed_ = true;
    377           return 0;
    378         }
    379    }
     344            CASE_1("pi")
     345                value = 3.1415926535897932;
     346            CASE("e")
     347                value = 2.7182818284590452;
     348            CASE_ELSE
     349            {
     350                this->failed_ = true;
     351                delete[] word;
     352                return 0;
     353            }
     354        }
     355        delete[] word;
     356    }
     357    else if (*reading_stream == 40)
     358    {  // expresion in paranthesis
     359        ++reading_stream;
     360        value = parse_last_argument();
     361    }
     362    else
     363    {
     364        this->failed_ = true;
     365        return 0;
     366    }
     367
     368    PARSE_BLANKS;
     369    switch (op)
     370    {
     371    case u_nicht: return !value;
     372    case u_plus:  return  value;
     373    case u_minus: return -value;
     374    default:
     375        this->failed_ = true;
     376        return 0;
     377    }
    380378}
    381379
    382380char* ExprParser::parse_word(char* str)
    383381{
    384    char* word = str;
    385    int counter = 0;
    386    while (*reading_stream > 47 && *reading_stream < 58 || *reading_stream > 64 && *reading_stream < 91 || *reading_stream > 96 && *reading_stream < 123 || *reading_stream == 46)
    387    {
    388       *word++ = *reading_stream++;
    389       counter++;
    390       if (counter > 255)
    391       {
    392         this->failed_ = true;
    393         return '\0';
    394       }
    395    };
    396    *word = '\0';
    397    return str;
     382    char* word = str;
     383    int counter = 0;
     384    while (*reading_stream > 47 && *reading_stream < 58 || *reading_stream > 64 && *reading_stream < 91 || *reading_stream > 96 && *reading_stream < 123 || *reading_stream == 46)
     385    {
     386        *word++ = *reading_stream++;
     387        counter++;
     388        if (counter > 255)
     389        {
     390            this->failed_ = true;
     391            return '\0';
     392        }
     393    };
     394    *word = '\0';
     395    return str;
    398396}
    399397
    400398ExprParser::binary_operator ExprParser::parse_binary_operator()
    401399{
    402    binary_operator op;
    403    switch (*reading_stream)
    404    {
    405       case '+': op = b_plus; break;
    406       case '-': op = b_minus; break;
    407       case '*': op = mal; break;
    408       case '/': op = durch; break;
    409       case '^': op = hoch; break;
    410       case '%': op = modulo; break;
    411       case '&': op = und; break;
    412       case '|': op = oder; break;
    413       case '=': op = gleich; break;
    414       case '!': op = b_nicht; break;
    415       case '<': op = kleiner; break;
    416       case '>': op = groesser; break;
    417       default: return undef;
    418    }
    419    if (*++reading_stream == '=')
    420    {
    421       if (op > 9)
    422       {
    423          ++reading_stream;
    424          return (binary_operator)(op + 3);
    425       }
    426       else
    427       {
    428          --reading_stream;
    429          return undef;
    430       }
    431    }
    432    else
    433       return op;
     400    binary_operator op;
     401    switch (*reading_stream)
     402    {
     403    case '+': op = b_plus; break;
     404    case '-': op = b_minus; break;
     405    case '*': op = mal; break;
     406    case '/': op = durch; break;
     407    case '^': op = hoch; break;
     408    case '%': op = modulo; break;
     409    case '&': op = und; break;
     410    case '|': op = oder; break;
     411    case '=': op = gleich; break;
     412    case '!': op = b_nicht; break;
     413    case '<': op = kleiner; break;
     414    case '>': op = groesser; break;
     415    default: return undef;
     416    }
     417    if (*++reading_stream == '=')
     418    {
     419        if (op > 9)
     420        {
     421            ++reading_stream;
     422            return (binary_operator)(op + 3);
     423        }
     424        else
     425        {
     426            --reading_stream;
     427            return undef;
     428        }
     429    }
     430    else
     431        return op;
    434432}
    435433
    436434ExprParser::unary_operator ExprParser::parse_unary_operator()
    437435{
    438    switch (*reading_stream)
    439    {
    440       case '!':
    441          ++reading_stream;
    442          return u_nicht;
    443       case '+':
    444          ++reading_stream;
    445          return u_plus;
    446       case '-':
    447          ++reading_stream;
    448          return u_minus;
    449       default :
    450          return u_plus;
    451    }
    452 }
     436    switch (*reading_stream)
     437    {
     438    case '!':
     439        ++reading_stream;
     440        return u_nicht;
     441    case '+':
     442        ++reading_stream;
     443        return u_plus;
     444    case '-':
     445        ++reading_stream;
     446        return u_minus;
     447    default :
     448        return u_plus;
     449    }
     450}
  • code/trunk/src/util/ExprParser.h

    r1505 r1894  
    4141{
    4242public:
    43   enum binary_operator
    44   {
    45     b_plus,
    46     b_minus,
    47     mal,
    48     durch,
    49     modulo,
    50     hoch,
    51     undef,
    52     oder,
    53     und,
    54     gleich,
    55     b_nicht,
    56     kleiner,
    57     groesser,
    58     ungleich,
    59     kleinergleich,
    60     groessergleich
    61   };
     43    enum binary_operator
     44    {
     45        b_plus,
     46        b_minus,
     47        mal,
     48        durch,
     49        modulo,
     50        hoch,
     51        undef,
     52        oder,
     53        und,
     54        gleich,
     55        b_nicht,
     56        kleiner,
     57        groesser,
     58        ungleich,
     59        kleinergleich,
     60        groessergleich
     61    };
    6262
    63   enum unary_operator
    64   {
    65     u_plus,
    66     u_minus,
    67     u_nicht
    68   };
     63    enum unary_operator
     64    {
     65        u_plus,
     66        u_minus,
     67        u_nicht
     68    };
    6969
    7070
    71   ExprParser(const std::string& str);
    72   std::string& getRemains() { return  this->remains_; }
    73   double       getResult()  { return  this->result_; }
    74   bool         getSuccess() { return !this->failed_; }
     71    ExprParser(const std::string& str);
     72    std::string& getRemains() { return  this->remains_; }
     73    double       getResult()  { return  this->result_; }
     74    bool         getSuccess() { return !this->failed_; }
    7575
    7676private:
    77   double parse_expr_1();
    78   double parse_expr_2();
    79   double parse_expr_3();
    80   double parse_expr_4();
    81   double parse_expr_5();
    82   double parse_expr_6();
    83   double parse_expr_7();
    84   double parse_expr_8();
    85   char* parse_word(char* str);
    86   binary_operator parse_binary_operator();
    87   unary_operator parse_unary_operator();
     77    double parse_expr_1();
     78    double parse_expr_2();
     79    double parse_expr_3();
     80    double parse_expr_4();
     81    double parse_expr_5();
     82    double parse_expr_6();
     83    double parse_expr_7();
     84    double parse_expr_8();
     85    char* parse_word(char* str);
     86    binary_operator parse_binary_operator();
     87    unary_operator parse_unary_operator();
    8888
    89   double parse_argument();
    90   double parse_last_argument();
     89    double parse_argument();
     90    double parse_last_argument();
    9191
    92   binary_operator op;
    93   const char* reading_stream;
    94   bool failed_;
    95   double result_;
    96   std::string remains_;
     92    binary_operator op;
     93    const char* reading_stream;
     94    bool failed_;
     95    double result_;
     96    std::string remains_;
    9797
    9898};
  • code/trunk/src/util/String.cc

    r1889 r1894  
    7979    int pos2 = str.size() - 1;
    8080    for (; pos1 < str.size() && (str[pos1] == ' ' || str[pos1] == '\t' || str[pos1] == '\n'); pos1++);
    81     for (; pos2 != 0         && (str[pos2] == ' ' || str[pos2] == '\t' || str[pos2] == '\n'); pos2--);
     81    for (; pos2 > 0         && (str[pos2] == ' ' || str[pos2] == '\t' || str[pos2] == '\n'); pos2--);
    8282    return str.substr(pos1, pos2 - pos1 + 1);
    8383}
Note: See TracChangeset for help on using the changeset viewer.