Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/OrxyRoad_FS18/src/libraries/util/ExprParser.h @ 12206

Last change on this file since 12206 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
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/**
30@file
31@ingroup Util
32@brief Declaration of FloatParser
33*/
34
35#ifndef _FloatParser_H__
36#define _FloatParser_H__
37
38#include "UtilPrereqs.h"
39
40#include <map>
41#include <string>
42
43namespace orxonox
44{
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            {
57                orxout(user_warning) << "Expression could not be parsed to the end! Remains: '" << expr.getRemains() << '\'' << endl;
58            }
59            float result = expr.getResult();
60        }
61        else
62            orxout(user_error) << "Cannot calculate expression: Parse error." << endl;
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    */
93    class _UtilExport ExprParser
94    {
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        };
115
116        enum unary_operator
117        {
118            u_plus,
119            u_minus,
120            u_nicht
121        };
122
123
124        ExprParser();
125        void parse(const std::string& str);
126        const std::string& getRemains() { return  this->remains_; }
127        float              getResult()  { return  this->result_; }
128        bool               getSuccess() { return !this->failed_; }
129
130        void setVariable(const std::string& varname, float value);
131
132    private:
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();
141        void parse_word(char* str);
142        binary_operator parse_binary_operator();
143        unary_operator parse_unary_operator();
144
145        float parse_argument();
146        float parse_last_argument();
147
148        binary_operator op;
149        const char* reading_stream;
150        bool failed_;
151        float result_;
152        std::string remains_;
153        std::map<std::string, float> variables_;
154    };
155
156    //Endzeichen für float expression: ')', '}', ']', ',', ';'
157    _UtilExport bool parse_float(char* const, char**, float*);
158    //Endzeichen angegeben
159    _UtilExport bool parse_float(char* const, char**, char, float*);
160    //Letzter Teil-float eines Vektors parsen (keine Vergleichs- und Logikoperationen)
161    _UtilExport bool parse_vector_float(char* const, char**, bool, float*);
162}
163
164#endif /* _FloatParser_H__ */
Note: See TracBrowser for help on using the repository browser.