"Fossies" - the Fresh Open Source Software Archive 
Member "mathmod-11.1/fparser/fparser.hh" (17 Jun 2021, 8427 Bytes) of package /linux/misc/mathmod-11.1-source.zip:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /***************************************************************************\
2 |* Function Parser for C++ v4.5.2 *|
3 |*-------------------------------------------------------------------------*|
4 |* Copyright: Juha Nieminen, Joel Yliluoma *|
5 |* *|
6 |* This library is distributed under the terms of the *|
7 |* GNU Lesser General Public License version 3. *|
8 |* (See lgpl.txt and gpl.txt for the license text.) *|
9 \***************************************************************************/
10
11 #ifndef ONCE_FPARSER_H_
12 #define ONCE_FPARSER_H_
13
14 #include <string>
15 #include <vector>
16 #include <complex>
17 #ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING
18 #include <iostream>
19 #endif
20
21 #ifdef _MSC_VER
22 // Visual Studio's warning about missing definitions for the explicit
23 // FunctionParserBase instantiations is irrelevant here.
24 #pragma warning(disable : 4661)
25 #endif
26
27 namespace FPoptimizer_CodeTree { template<typename Value_t> class CodeTree; }
28
29 template<typename Value_t>
30 class FunctionParserBase
31 {
32 public:
33 enum ParseErrorType
34 {
35 SYNTAX_ERROR=0, MISM_PARENTH, MISSING_PARENTH, EMPTY_PARENTH,
36 EXPECT_OPERATOR, OUT_OF_MEMORY, UNEXPECTED_ERROR, INVALID_VARS,
37 ILL_PARAMS_AMOUNT, PREMATURE_EOS, EXPECT_PARENTH_FUNC,
38 UNKNOWN_IDENTIFIER,
39 NO_FUNCTION_PARSED_YET,
40 FP_NO_ERROR
41 };
42 // Any modification to this enum must be translated to the same enum in commun.h
43 enum EvalErrorType
44 {
45 EVAL_ERROR=0,
46 DIVISION_BY_ZERO,
47 COTAN_FUNCT_ERROR,
48 EXP_FUNCT_ERROR,
49 ARC_TRIGONOMETRIC_FUNCT_ERROR,
50 IF_FUNCT_ERROR,
51 EVAL_NO_ERROR,
52 VAR_OVERFLOW
53 };
54
55 typedef Value_t value_type;
56
57
58 int Parse(const char* Function, const std::string& Vars,
59 bool useDegrees = false);
60 int Parse(const std::string& Function, const std::string& Vars,
61 bool useDegrees = false);
62
63 void setDelimiterChar(char);
64
65 static Value_t epsilon();
66 static void setEpsilon(Value_t);
67
68 const char* ErrorMsg() const;
69 ParseErrorType GetParseErrorType() const;
70
71 Value_t Eval(const Value_t* Vars);
72 std::complex<double> EvalC(const std::complex<double>* );
73 void AllocateStackMemory( unsigned int, int nbvar=0);
74 Value_t Eval2(const Value_t* Vars, unsigned Nbvar, Value_t* results, unsigned, int position=-1);
75 int EvalError() const;
76
77 bool AddConstant(const std::string& name, Value_t value);
78 bool AddUnit(const std::string& name, Value_t value);
79
80 typedef Value_t (*FunctionPtr)(const Value_t*);
81
82 bool AddFunction(const std::string& name,
83 FunctionPtr, unsigned paramsAmount);
84 bool AddFunction(const std::string& name, FunctionParserBase&);
85
86 class FunctionWrapper;
87
88 template<typename DerivedWrapper>
89 bool AddFunctionWrapper(const std::string& name, const DerivedWrapper&,
90 unsigned paramsAmount);
91
92 FunctionWrapper* GetFunctionWrapper(const std::string& name);
93
94 bool RemoveIdentifier(const std::string& name);
95
96 void Optimize();
97
98
99 int ParseAndDeduceVariables(const std::string& function,
100 int* amountOfVariablesFound = nullptr,
101 bool useDegrees = false);
102 int ParseAndDeduceVariables(const std::string& function,
103 std::string& resultVarString,
104 int* amountOfVariablesFound = nullptr,
105 bool useDegrees = false);
106 int ParseAndDeduceVariables(const std::string& function,
107 std::vector<std::string>& resultVars,
108 bool useDegrees = false);
109
110
111 FunctionParserBase();
112 ~FunctionParserBase();
113
114 // Copy constructor and assignment operator (implemented using the
115 // copy-on-write technique for efficiency):
116 FunctionParserBase(const FunctionParserBase&);
117 FunctionParserBase& operator=(const FunctionParserBase&);
118
119
120 void ForceDeepCopy();
121
122
123
124 #ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING
125 // For debugging purposes only.
126 // Performs no sanity checks or anything. If the values are wrong, the
127 // library will crash. Do not use unless you know what you are doing.
128 void InjectRawByteCode(const unsigned* bytecode, unsigned bytecodeAmount,
129 const Value_t* immed, unsigned immedAmount,
130 unsigned stackSize);
131
132 void PrintByteCode(std::ostream& dest, bool showExpression = true) const;
133 #endif
134
135
136
137 //========================================================================
138 protected:
139 //========================================================================
140 // A derived class can implement its own evaluation logic by using
141 // the parser data (found in fptypes.hh).
142 struct Data;
143 Data* getParserData();
144
145
146 //========================================================================
147 public:
148 //========================================================================
149
150 friend class FPoptimizer_CodeTree::CodeTree<Value_t>;
151
152 // Private data:
153 // ------------
154 Data* mData;
155 unsigned mStackPtr;
156
157
158 // Private methods:
159 // ---------------
160 void CopyOnWrite();
161 void CopyOnWrite2();
162 bool CheckRecursiveLinking(const FunctionParserBase*) const;
163 bool NameExists(const char*, unsigned);
164 bool ParseVariables(const std::string&);
165 int ParseFunction(const char*, bool);
166 const char* SetErrorType(ParseErrorType, const char*);
167
168 void AddFunctionOpcode(unsigned);
169 void AddImmedOpcode(Value_t v);
170 void incStackPtr();
171 void CompilePowi(long);
172 bool TryCompilePowi(Value_t);
173
174 const char* CompileIf(const char*);
175 const char* CompileFunctionParams(const char*, unsigned);
176 const char* CompileElement(const char*);
177 const char* CompilePossibleUnit(const char*);
178 const char* CompilePow(const char*);
179 const char* CompileUnaryMinus(const char*);
180 const char* CompileMult(const char*);
181 const char* CompileAddition(const char*);
182 const char* CompileComparison(const char*);
183 const char* CompileAnd(const char*);
184 const char* CompileExpression(const char*);
185 inline const char* CompileFunction(const char*, unsigned);
186 inline const char* CompileParenthesis(const char*);
187 inline const char* CompileLiteral(const char*);
188 template<bool SetFlag>
189 inline void PushOpcodeParam(unsigned);
190 template<bool SetFlag>
191 inline void PutOpcodeParamAt(unsigned, unsigned offset);
192 const char* Compile(const char*);
193
194 bool addFunctionWrapperPtr(const std::string&, FunctionWrapper*, unsigned);
195 static void incFuncWrapperRefCount(FunctionWrapper*);
196 static unsigned decFuncWrapperRefCount(FunctionWrapper*);
197
198 protected:
199 // Parsing utility functions
200 static std::pair<const char*, Value_t> ParseLiteral(const char*);
201 static unsigned ParseIdentifier(const char*);
202 };
203
204 class FunctionParser: public FunctionParserBase<double> {};
205 class FunctionParser_f: public FunctionParserBase<float> {};
206 class FunctionParser_ld: public FunctionParserBase<long double> {};
207 class FunctionParser_li: public FunctionParserBase<long> {};
208
209 #include <complex>
210 class FunctionParser_cd: public FunctionParserBase<std::complex<double> > {};
211 class FunctionParser_cf: public FunctionParserBase<std::complex<float> > {};
212 class FunctionParser_cld: public FunctionParserBase<std::complex<long double> > {};
213
214
215
216 template<typename Value_t>
217 class FunctionParserBase<Value_t>::FunctionWrapper
218 {
219 unsigned mReferenceCount;
220 friend class FunctionParserBase<Value_t>;
221
222 public:
223 FunctionWrapper(): mReferenceCount(1) {}
224 FunctionWrapper(const FunctionWrapper&): mReferenceCount(1) {}
225 virtual ~FunctionWrapper() {}
226 FunctionWrapper& operator=(const FunctionWrapper&) { return *this; }
227
228 virtual Value_t callFunction(const Value_t*) = 0;
229 };
230
231 template<typename Value_t>
232 template<typename DerivedWrapper>
233 bool FunctionParserBase<Value_t>::AddFunctionWrapper
234 (const std::string& name, const DerivedWrapper& wrapper, unsigned paramsAmount)
235 {
236 return addFunctionWrapperPtr
237 (name, new DerivedWrapper(wrapper), paramsAmount);
238 }
239 #endif
240
241 extern template class FunctionParserBase<double>; // explicit instantiation declaration