Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/doc/src/libraries/util/ExprParser.h @ 7367

Last change on this file since 7367 was 7367, checked in by rgrieder, 14 years ago

Added Doxygen documentation for ExprParser, MathConvert, OrxEnum, OrxAssert and TriBool.
Adjusted Doxygen documentation for Clock and Exception.

  • 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@brief Declaration of FloatParser
32*/
33
34#ifndef _FloatParser_H__
35#define _FloatParser_H__
36
37#include "UtilPrereqs.h"
38
39#include <map>
40#include <string>
41
42namespace orxonox
43{
44    /** Parser for expressions like \c "3 * cos(5 + 4) / a" where \a a is a predeclared
45        variable.
46    @par Usage
47        Using it is rather simple:
48        @code
49        std::string str("3 + 4");
50        ExprParser expr;
51        expr.parse(str);
52        if (expr.getSuccess())
53        {
54            if (!expr.getRemains().empty())
55            {
56                COUT(2) << "Warning: Expression could not be parsed to the end! Remains: '" << expr.getRemains() << '\'' << std::endl;
57            }
58            double result = expr.getResult();
59        }
60        else
61            COUT(1) << "Error: Cannot calculate expression: Parse error." << std::endl;
62        @endcode
63        getRemains() returns the expression after what could be parsed. For instance
64        \c "2*3 text" will return \c "text" as remains.
65    @details
66        The implementation of this class is really old and sort of a trial for
67        a first C++ class by Reto. That explains why it looks more like C than
68        C++... Also some of the variable names are in German. <br>
69        Explaining how it works exactly is probably not possible anymore, but it
70        is based on recursively parsing the expression character by character.
71        That much I can remember.
72    @par Functions, operators and variables supported
73        - Variables:
74            - e
75            - pi
76        - Functions:
77            - sin, asin, sinh, asinh
78            - cos, acos, cosh, acosh
79            - tan, atan, tanh, atanh
80            - int, floor, ceil, abs, sign
81            - pow, sqrt, exp, ln, log
82            - mod, div
83            - min, max
84            - radians, degrees
85        - Operators:
86            - +, -, ! (unary)
87            - +, -, *, /, %, ^, |, &, !, <, >, !=, <=, >=, =
88    @note
89        Operators may not be very consistent with C++ rules, but using the class
90        for plus and minus should be perfectly ok.
91    */
92    class _UtilExport ExprParser
93    {
94    public:
95        enum binary_operator
96        {
97            b_plus,
98            b_minus,
99            mal,
100            durch,
101            modulo,
102            hoch,
103            undef,
104            oder,
105            und,
106            gleich,
107            b_nicht,
108            kleiner,
109            groesser,
110            ungleich,
111            kleinergleich,
112            groessergleich
113        };
114
115        enum unary_operator
116        {
117            u_plus,
118            u_minus,
119            u_nicht
120        };
121
122
123        ExprParser();
124        void parse(const std::string& str);
125        const std::string& getRemains() { return  this->remains_; }
126        double             getResult()  { return  this->result_; }
127        bool               getSuccess() { return !this->failed_; }
128
129        void setVariable(const std::string& varname, double value);
130
131    private:
132        double parse_expr_1();
133        double parse_expr_2();
134        double parse_expr_3();
135        double parse_expr_4();
136        double parse_expr_5();
137        double parse_expr_6();
138        double parse_expr_7();
139        double parse_expr_8();
140        char* parse_word(char* str);
141        binary_operator parse_binary_operator();
142        unary_operator parse_unary_operator();
143
144        double parse_argument();
145        double parse_last_argument();
146
147        binary_operator op;
148        const char* reading_stream;
149        bool failed_;
150        double result_;
151        std::string remains_;
152        std::map<std::string, double> variables_;
153    };
154
155    //Endzeichen für float expression: ')', '}', ']', ',', ';'
156    _UtilExport bool parse_float(char* const, char**, double*);
157    //Endzeichen angegeben
158    _UtilExport bool parse_float(char* const, char**, char, double*);
159    //Letzter Teil-float eines Vektors parsen (keine Vergleichs- und Logikoperationen)
160    _UtilExport bool parse_vector_float(char* const, char**, bool, double*);
161}
162
163#endif /* _FloatParser_H__ */
Note: See TracBrowser for help on using the repository browser.