[Orxonox-commit 1467] r6185 - in code/branches/presentation2/src/libraries: core util
rgrieder at orxonox.net
rgrieder at orxonox.net
Mon Nov 30 23:32:53 CET 2009
Author: rgrieder
Date: 2009-11-30 23:32:53 +0100 (Mon, 30 Nov 2009)
New Revision: 6185
Modified:
code/branches/presentation2/src/libraries/core/ConsoleCommandCompilation.cc
code/branches/presentation2/src/libraries/util/ExprParser.cc
code/branches/presentation2/src/libraries/util/ExprParser.h
Log:
Extended ExprParser to support arbitrary variables. You can set them with ExprParser::setVariable.
Modified: code/branches/presentation2/src/libraries/core/ConsoleCommandCompilation.cc
===================================================================
--- code/branches/presentation2/src/libraries/core/ConsoleCommandCompilation.cc 2009-11-30 19:50:44 UTC (rev 6184)
+++ code/branches/presentation2/src/libraries/core/ConsoleCommandCompilation.cc 2009-11-30 22:32:53 UTC (rev 6185)
@@ -157,7 +157,8 @@
float calculate(const std::string& calculation)
{
- ExprParser expr(calculation);
+ ExprParser expr;
+ expr.parse(calculation);
if (expr.getSuccess())
{
if (expr.getResult() == 42.0)
Modified: code/branches/presentation2/src/libraries/util/ExprParser.cc
===================================================================
--- code/branches/presentation2/src/libraries/util/ExprParser.cc 2009-11-30 19:50:44 UTC (rev 6184)
+++ code/branches/presentation2/src/libraries/util/ExprParser.cc 2009-11-30 22:32:53 UTC (rev 6185)
@@ -46,9 +46,20 @@
namespace orxonox
{
- ExprParser::ExprParser(const std::string& str)
+ ExprParser::ExprParser()
{
this->failed_ = false;
+ this->variables_["pi"] = 3.1415926535897932;
+ this->variables_["e"] = 2.7182818284590452;
+ }
+
+ void ExprParser::setVariable(const std::string& varname, double value)
+ {
+ this->variables_[varname] = value;
+ }
+
+ void ExprParser::parse(const std::string& str)
+ {
this->reading_stream = str.c_str();
if (str.size() == 0 || *reading_stream == '\0')
{
@@ -342,12 +353,10 @@
}
else
{
-#define SWITCH word
- CASE_1("pi")
- value = 3.1415926535897932;
- CASE("e")
- value = 2.7182818284590452;
- CASE_ELSE
+ std::map<std::string, double>::const_iterator it = this->variables_.find(word);
+ if (it != this->variables_.end())
+ value = it->second;
+ else
{
this->failed_ = true;
delete[] word;
@@ -357,7 +366,7 @@
delete[] word;
}
else if (*reading_stream == 40)
- { // expresion in paranthesis
+ { // expression in parenthesis
++reading_stream;
value = parse_last_argument();
}
Modified: code/branches/presentation2/src/libraries/util/ExprParser.h
===================================================================
--- code/branches/presentation2/src/libraries/util/ExprParser.h 2009-11-30 19:50:44 UTC (rev 6184)
+++ code/branches/presentation2/src/libraries/util/ExprParser.h 2009-11-30 22:32:53 UTC (rev 6185)
@@ -35,6 +35,8 @@
#define _FloatParser_H__
#include "UtilPrereqs.h"
+
+#include <map>
#include <string>
namespace orxonox
@@ -70,11 +72,14 @@
};
- ExprParser(const std::string& str);
+ ExprParser();
+ void parse(const std::string& str);
const std::string& getRemains() { return this->remains_; }
double getResult() { return this->result_; }
bool getSuccess() { return !this->failed_; }
+ void setVariable(const std::string& varname, double value);
+
private:
double parse_expr_1();
double parse_expr_2();
@@ -96,7 +101,7 @@
bool failed_;
double result_;
std::string remains_;
-
+ std::map<std::string, double> variables_;
};
//Endzeichen für float expression: ')', '}', ']', ',', ';'
More information about the Orxonox-commit
mailing list