Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6185


Ignore:
Timestamp:
Nov 30, 2009, 11:32:53 PM (14 years ago)
Author:
rgrieder
Message:

Extended ExprParser to support arbitrary variables. You can set them with ExprParser::setVariable.

Location:
code/branches/presentation2/src/libraries
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation2/src/libraries/core/ConsoleCommandCompilation.cc

    r6105 r6185  
    158158    float calculate(const std::string& calculation)
    159159    {
    160         ExprParser expr(calculation);
     160        ExprParser expr;
     161        expr.parse(calculation);
    161162        if (expr.getSuccess())
    162163        {
  • code/branches/presentation2/src/libraries/util/ExprParser.cc

    r5738 r6185  
    4747namespace orxonox
    4848{
    49     ExprParser::ExprParser(const std::string& str)
     49    ExprParser::ExprParser()
    5050    {
    5151        this->failed_ = false;
     52        this->variables_["pi"] = 3.1415926535897932;
     53        this->variables_["e"] = 2.7182818284590452;
     54    }
     55
     56    void ExprParser::setVariable(const std::string& varname, double value)
     57    {
     58        this->variables_[varname] = value;
     59    }
     60
     61    void ExprParser::parse(const std::string& str)
     62    {
    5263        this->reading_stream = str.c_str();
    5364        if (str.size() == 0 || *reading_stream == '\0')
     
    343354            else
    344355            {
    345 #define SWITCH word
    346                 CASE_1("pi")
    347                     value = 3.1415926535897932;
    348                 CASE("e")
    349                     value = 2.7182818284590452;
    350                 CASE_ELSE
     356                std::map<std::string, double>::const_iterator it = this->variables_.find(word);
     357                if (it != this->variables_.end())
     358                    value = it->second;
     359                else
    351360                {
    352361                    this->failed_ = true;
     
    358367        }
    359368        else if (*reading_stream == 40)
    360         {  // expresion in paranthesis
     369        {  // expression in parenthesis
    361370            ++reading_stream;
    362371            value = parse_last_argument();
  • code/branches/presentation2/src/libraries/util/ExprParser.h

    r5738 r6185  
    3636
    3737#include "UtilPrereqs.h"
     38
     39#include <map>
    3840#include <string>
    3941
     
    7173
    7274
    73         ExprParser(const std::string& str);
     75        ExprParser();
     76        void parse(const std::string& str);
    7477        const std::string& getRemains() { return  this->remains_; }
    7578        double             getResult()  { return  this->result_; }
    7679        bool               getSuccess() { return !this->failed_; }
     80
     81        void setVariable(const std::string& varname, double value);
    7782
    7883    private:
     
    97102        double result_;
    98103        std::string remains_;
    99 
     104        std::map<std::string, double> variables_;
    100105    };
    101106
Note: See TracChangeset for help on using the changeset viewer.