[1118] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1505] | 3 | * > www.orxonox.net < |
---|
[1118] | 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Reto Grieder |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
[7401] | 30 | @file |
---|
| 31 | @ingroup Util |
---|
| 32 | @brief Declaration of FloatParser |
---|
[1118] | 33 | */ |
---|
[1505] | 34 | |
---|
| 35 | #ifndef _FloatParser_H__ |
---|
| 36 | #define _FloatParser_H__ |
---|
| 37 | |
---|
| 38 | #include "UtilPrereqs.h" |
---|
[6417] | 39 | |
---|
| 40 | #include <map> |
---|
[1505] | 41 | #include <string> |
---|
| 42 | |
---|
[2171] | 43 | namespace orxonox |
---|
[1505] | 44 | { |
---|
[7401] | 45 | /** Parser for expressions like \c "3 * cos(5 + 4) / a" where \a a is a predeclared |
---|
| 46 | variable. |
---|
| 47 | @par Usage |
---|
| 48 | Using it is rather simple: |
---|
| 49 | @code |
---|
| 50 | std::string str("3 + 4"); |
---|
| 51 | ExprParser expr; |
---|
| 52 | expr.parse(str); |
---|
| 53 | if (expr.getSuccess()) |
---|
| 54 | { |
---|
| 55 | if (!expr.getRemains().empty()) |
---|
| 56 | { |
---|
[8858] | 57 | orxout(user_warning) << "Expression could not be parsed to the end! Remains: '" << expr.getRemains() << '\'' << endl; |
---|
[7401] | 58 | } |
---|
[8351] | 59 | float result = expr.getResult(); |
---|
[7401] | 60 | } |
---|
| 61 | else |
---|
[8858] | 62 | orxout(user_error) << "Cannot calculate expression: Parse error." << endl; |
---|
[7401] | 63 | @endcode |
---|
| 64 | getRemains() returns the expression after what could be parsed. For instance |
---|
| 65 | \c "2*3 text" will return \c "text" as remains. |
---|
| 66 | @details |
---|
| 67 | The implementation of this class is really old and sort of a trial for |
---|
| 68 | a first C++ class by Reto. That explains why it looks more like C than |
---|
| 69 | C++... Also some of the variable names are in German. <br> |
---|
| 70 | Explaining how it works exactly is probably not possible anymore, but it |
---|
| 71 | is based on recursively parsing the expression character by character. |
---|
| 72 | That much I can remember. |
---|
| 73 | @par Functions, operators and variables supported |
---|
| 74 | - Variables: |
---|
| 75 | - e |
---|
| 76 | - pi |
---|
| 77 | - Functions: |
---|
| 78 | - sin, asin, sinh, asinh |
---|
| 79 | - cos, acos, cosh, acosh |
---|
| 80 | - tan, atan, tanh, atanh |
---|
| 81 | - int, floor, ceil, abs, sign |
---|
| 82 | - pow, sqrt, exp, ln, log |
---|
| 83 | - mod, div |
---|
| 84 | - min, max |
---|
| 85 | - radians, degrees |
---|
| 86 | - Operators: |
---|
| 87 | - +, -, ! (unary) |
---|
| 88 | - +, -, *, /, %, ^, |, &, !, <, >, !=, <=, >=, = |
---|
| 89 | @note |
---|
| 90 | Operators may not be very consistent with C++ rules, but using the class |
---|
| 91 | for plus and minus should be perfectly ok. |
---|
| 92 | */ |
---|
[2171] | 93 | class _UtilExport ExprParser |
---|
[1894] | 94 | { |
---|
[2171] | 95 | public: |
---|
| 96 | enum binary_operator |
---|
| 97 | { |
---|
| 98 | b_plus, |
---|
| 99 | b_minus, |
---|
| 100 | mal, |
---|
| 101 | durch, |
---|
| 102 | modulo, |
---|
| 103 | hoch, |
---|
| 104 | undef, |
---|
| 105 | oder, |
---|
| 106 | und, |
---|
| 107 | gleich, |
---|
| 108 | b_nicht, |
---|
| 109 | kleiner, |
---|
| 110 | groesser, |
---|
| 111 | ungleich, |
---|
| 112 | kleinergleich, |
---|
| 113 | groessergleich |
---|
| 114 | }; |
---|
[1505] | 115 | |
---|
[2171] | 116 | enum unary_operator |
---|
| 117 | { |
---|
| 118 | u_plus, |
---|
| 119 | u_minus, |
---|
| 120 | u_nicht |
---|
| 121 | }; |
---|
[1505] | 122 | |
---|
| 123 | |
---|
[6417] | 124 | ExprParser(); |
---|
| 125 | void parse(const std::string& str); |
---|
[3196] | 126 | const std::string& getRemains() { return this->remains_; } |
---|
[8351] | 127 | float getResult() { return this->result_; } |
---|
[3196] | 128 | bool getSuccess() { return !this->failed_; } |
---|
[1505] | 129 | |
---|
[8351] | 130 | void setVariable(const std::string& varname, float value); |
---|
[6417] | 131 | |
---|
[2171] | 132 | private: |
---|
[8351] | 133 | float parse_expr_1(); |
---|
| 134 | float parse_expr_2(); |
---|
| 135 | float parse_expr_3(); |
---|
| 136 | float parse_expr_4(); |
---|
| 137 | float parse_expr_5(); |
---|
| 138 | float parse_expr_6(); |
---|
| 139 | float parse_expr_7(); |
---|
| 140 | float parse_expr_8(); |
---|
[2171] | 141 | char* parse_word(char* str); |
---|
| 142 | binary_operator parse_binary_operator(); |
---|
| 143 | unary_operator parse_unary_operator(); |
---|
[1505] | 144 | |
---|
[8351] | 145 | float parse_argument(); |
---|
| 146 | float parse_last_argument(); |
---|
[1505] | 147 | |
---|
[2171] | 148 | binary_operator op; |
---|
| 149 | const char* reading_stream; |
---|
| 150 | bool failed_; |
---|
[8351] | 151 | float result_; |
---|
[2171] | 152 | std::string remains_; |
---|
[8351] | 153 | std::map<std::string, float> variables_; |
---|
[2171] | 154 | }; |
---|
[1505] | 155 | |
---|
[2171] | 156 | //Endzeichen für float expression: ')', '}', ']', ',', ';' |
---|
[8351] | 157 | _UtilExport bool parse_float(char* const, char**, float*); |
---|
[2171] | 158 | //Endzeichen angegeben |
---|
[8351] | 159 | _UtilExport bool parse_float(char* const, char**, char, float*); |
---|
[2171] | 160 | //Letzter Teil-float eines Vektors parsen (keine Vergleichs- und Logikoperationen) |
---|
[8351] | 161 | _UtilExport bool parse_vector_float(char* const, char**, bool, float*); |
---|
[2171] | 162 | } |
---|
[1505] | 163 | |
---|
| 164 | #endif /* _FloatParser_H__ */ |
---|