All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fparser.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  This header file for the Function Parser library (from
5  http://warp.povusers.org/FunctionParser/fparser.html) is distributed
6  within O2scl (http://o2scl.sourceforge.net). Function Parser is
7  distributed under LGPLv3, which can be found in
8  doc/o2scl/extras/lgpl_v3_license.txt . O2scl is distributed under
9  GPLv3, found at doc/o2scl/extras/gpl_license.txt .
10 
11  -------------------------------------------------------------------
12 */
13 /** \file fparser.h
14  \brief Main header for the \ref FunctionParser class
15 */
16 
17 #ifdef DOXYGEN
18 
19 /** \brief Parse a mathematical function specified in a string
20 
21  This is version 4.5 of the Function Parser class developed
22  by Juha Nieminen and Joel Yliluoma. The full documentation is
23  not given here, but can be found at
24  http://warp.povusers.org/FunctionParser/fparser.html
25 
26  This class and other related classes reside in the global
27  namespace (not the \o2 namespace).
28 */
30 };
31 
32 #else
33 
34 #define FP_NO_SUPPORT_OPTIMIZER
35 
36 /*************************************************************************** \
37 |* Function Parser for C++ v4.5 *|
38 |*-------------------------------------------------------------------------*|
39 |* Copyright: Juha Nieminen, Joel Yliluoma *|
40 |* *|
41 |* This library is distributed under the terms of the *|
42 |* GNU Lesser General Public License version 3. *|
43 |* (See lgpl.txt and gpl.txt for the license text.) *|
44 \***************************************************************************/
45 
46 #ifndef ONCE_FPARSER_H_
47 #define ONCE_FPARSER_H_
48 
49 #include <string>
50 #include <vector>
51 
52 #ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING
53 #include <iostream>
54 #endif
55 
56 #ifdef _MSC_VER
57 // Visual Studio's warning about missing definitions for the explicit
58 // FunctionParserBase instantiations is irrelevant here.
59 #pragma warning(disable : 4661)
60 #endif
61 
62 namespace FPoptimizer_CodeTree { template<typename Value_t> class CodeTree; }
63 
64 template<typename Value_t>
65 class FunctionParserBase
66 {
67  public:
68  enum ParseErrorType
69  {
70  SYNTAX_ERROR=0, MISM_PARENTH, MISSING_PARENTH, EMPTY_PARENTH,
71  EXPECT_OPERATOR, OUT_OF_MEMORY, UNEXPECTED_ERROR, INVALID_VARS,
72  ILL_PARAMS_AMOUNT, PREMATURE_EOS, EXPECT_PARENTH_FUNC,
73  UNKNOWN_IDENTIFIER,
74  NO_FUNCTION_PARSED_YET,
75  FP_NO_ERROR
76  };
77 
78  typedef Value_t value_type;
79 
80  int Parse(const char* Function, const std::string& Vars,
81  bool useDegrees = false);
82  int Parse(const std::string& Function, const std::string& Vars,
83  bool useDegrees = false);
84 
85  void setDelimiterChar(char);
86 
87  static Value_t epsilon();
88  static void setEpsilon(Value_t);
89 
90  const char* ErrorMsg() const;
91  ParseErrorType GetParseErrorType() const;
92 
93  Value_t Eval(const Value_t* Vars);
94  int EvalError() const;
95 
96  bool AddConstant(const std::string& name, Value_t value);
97  bool AddUnit(const std::string& name, Value_t value);
98 
99  typedef Value_t (*FunctionPtr)(const Value_t*);
100 
101  bool AddFunction(const std::string& name,
102  FunctionPtr, unsigned paramsAmount);
103  bool AddFunction(const std::string& name, FunctionParserBase&);
104 
105  /*
106  AWS, 5/3/12: This subclass has been moved from below to solve
107  compilation issues on OSX.
108  */
109  class FunctionWrapper {
110  unsigned mReferenceCount;
111  friend class FunctionParserBase<Value_t>;
112 
113  public:
114 
115  FunctionWrapper(): mReferenceCount(1) {}
116  FunctionWrapper(const FunctionWrapper&): mReferenceCount(1) {}
117  virtual ~FunctionWrapper() {}
118  FunctionWrapper& operator=(const FunctionWrapper&) { return *this; }
119 
120  virtual Value_t callFunction(const Value_t*) = 0;
121  };
122 
123  template<typename DerivedWrapper>
124  bool AddFunctionWrapper(const std::string& name, const DerivedWrapper&,
125  unsigned paramsAmount);
126 
127  FunctionWrapper* GetFunctionWrapper(const std::string& name);
128 
129  bool RemoveIdentifier(const std::string& name);
130 
131  void Optimize();
132 
133 
134  int ParseAndDeduceVariables(const std::string& function,
135  int* amountOfVariablesFound = 0,
136  bool useDegrees = false);
137  int ParseAndDeduceVariables(const std::string& function,
138  std::string& resultVarString,
139  int* amountOfVariablesFound = 0,
140  bool useDegrees = false);
141  int ParseAndDeduceVariables(const std::string& function,
142  std::vector<std::string>& resultVars,
143  bool useDegrees = false);
144 
145 
146  FunctionParserBase();
147  ~FunctionParserBase();
148 
149  // Copy constructor and assignment operator (implemented using the
150  // copy-on-write technique for efficiency):
151  FunctionParserBase(const FunctionParserBase&);
152  FunctionParserBase& operator=(const FunctionParserBase&);
153 
154 
155  void ForceDeepCopy();
156 
157 
158 
159 #ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING
160  // For debugging purposes only.
161  // Performs no sanity checks or anything. If the values are wrong, the
162  // library will crash. Do not use unless you know what you are doing.
163  void InjectRawByteCode(const unsigned* bytecode, unsigned bytecodeAmount,
164  const Value_t* immed, unsigned immedAmount,
165  unsigned stackSize);
166 
167  void PrintByteCode(std::ostream& dest, bool showExpression = true) const;
168 #endif
169 
170 
171 
172 //========================================================================
173  protected:
174 //========================================================================
175  // A derived class can implement its own evaluation logic by using
176  // the parser data (found in fptypes.hh).
177  struct Data;
178  Data* getParserData();
179 
180 
181 //========================================================================
182  private:
183 //========================================================================
184 
185  friend class FPoptimizer_CodeTree::CodeTree<Value_t>;
186 
187 // Private data:
188 // ------------
189  Data* mData;
190  unsigned mStackPtr;
191  static Value_t sEpsilon;
192 
193 
194 // Private methods:
195 // ---------------
196  void CopyOnWrite();
197  bool CheckRecursiveLinking(const FunctionParserBase*) const;
198  bool NameExists(const char*, unsigned);
199  bool ParseVariables(const std::string&);
200  int ParseFunction(const char*, bool);
201  const char* SetErrorType(ParseErrorType, const char*);
202 
203  void AddFunctionOpcode(unsigned);
204  void AddImmedOpcode(Value_t v);
205  void incStackPtr();
206  void CompilePowi(long);
207  bool TryCompilePowi(Value_t);
208 
209  const char* CompileIf(const char*);
210  const char* CompileFunctionParams(const char*, unsigned);
211  const char* CompileElement(const char*);
212  const char* CompilePossibleUnit(const char*);
213  const char* CompilePow(const char*);
214  const char* CompileUnaryMinus(const char*);
215  const char* CompileMult(const char*);
216  const char* CompileAddition(const char*);
217  const char* CompileComparison(const char*);
218  const char* CompileAnd(const char*);
219  const char* CompileExpression(const char*);
220  inline const char* CompileFunction(const char*, unsigned);
221  inline const char* CompileParenthesis(const char*);
222  inline const char* CompileLiteral(const char*);
223  template<bool SetFlag>
224  inline void PushOpcodeParam(unsigned);
225  template<bool SetFlag>
226  inline void PutOpcodeParamAt(unsigned, unsigned offset);
227  const char* Compile(const char*);
228 
229  bool addFunctionWrapperPtr(const std::string&, FunctionWrapper*, unsigned);
230  static void incFuncWrapperRefCount(FunctionWrapper*);
231  static unsigned decFuncWrapperRefCount(FunctionWrapper*);
232 
233 protected:
234  // Parsing utility functions
235  static std::pair<const char*, Value_t> ParseLiteral(const char*);
236  static unsigned ParseIdentifier(const char*);
237 };
238 
239 class FunctionParser: public FunctionParserBase<double> {};
240 class FunctionParser_f: public FunctionParserBase<float> {};
241 class FunctionParser_ld: public FunctionParserBase<long double> {};
242 class FunctionParser_li: public FunctionParserBase<long> {};
243 
244 #include <complex>
245 class FunctionParser_cd: public FunctionParserBase<std::complex<double> > {};
246 class FunctionParser_cf: public FunctionParserBase<std::complex<float> > {};
247 class FunctionParser_cld: public FunctionParserBase<std::complex<long double> > {};
248 
249 template<typename Value_t>
250 template<typename DerivedWrapper>
251 bool FunctionParserBase<Value_t>::AddFunctionWrapper
252 (const std::string& name, const DerivedWrapper& wrapper, unsigned paramsAmount)
253 {
254  return addFunctionWrapperPtr
255  (name, new DerivedWrapper(wrapper), paramsAmount);
256 }
257 #endif
258 
259 #endif
Parse a mathematical function specified in a string.
Definition: fparser.h:29

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).
Hosted at Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads..