-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfparser.hh
More file actions
168 lines (127 loc) · 4.76 KB
/
Copy pathfparser.hh
File metadata and controls
168 lines (127 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/***************************************************************************\
|* Function parser v2.8 by Warp *|
|* ---------------------------- *|
|* Parses and evaluates the given function with the given variable values. *|
|* See fparser.txt for details. *|
|* *|
\***************************************************************************/
#ifndef ONCE_FPARSER_H_
#define ONCE_FPARSER_H_
// Slightly modified for use with wxWidgets by John Labenski 1/20/04
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma interface "fparser.hh"
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include <string>
#include <map>
#include <vector>
#ifdef FUNCTIONPARSER_SUPPORT_DEBUG_OUTPUT
#include <iostream>
#endif
class FunctionParser
{
public:
enum ParseErrorType
{
SYNTAX_ERROR=0, MISM_PARENTH, MISSING_PARENTH, EMPTY_PARENTH,
EXPECT_OPERATOR, OUT_OF_MEMORY, UNEXPECTED_ERROR, INVALID_VARS,
ILL_PARAMS_AMOUNT, PREMATURE_EOS, EXPECT_PARENTH_FUNC,
FP_NO_ERROR
};
int Parse(const std::string& Function, const std::string& Vars,
bool useDegrees = false);
const char* ErrorMsg() const;
inline ParseErrorType GetParseErrorType() const { return parseErrorType; }
double Eval(const double* Vars);
inline int EvalError() const { return evalErrorType; }
bool AddConstant(const std::string& name, double value);
typedef double (*FunctionPtr)(const double*);
bool AddFunction(const std::string& name,
FunctionPtr, unsigned paramsAmount);
bool AddFunction(const std::string& name, FunctionParser&);
void Optimize();
int GetNumberVariables() const { return data->varAmount; }
bool GetUseDegrees() const { return data->useDegreeConversion; }
FunctionParser();
~FunctionParser();
// Copy constructor and assignment operator (implemented using the
// copy-on-write technique for efficiency):
FunctionParser(const FunctionParser&);
FunctionParser& operator=(const FunctionParser&);
#ifdef FUNCTIONPARSER_SUPPORT_DEBUG_OUTPUT
// For debugging purposes only:
void PrintByteCode(std::ostream& dest) const;
#endif
//========================================================================
private:
//========================================================================
// Private data:
// ------------
ParseErrorType parseErrorType;
int evalErrorType;
struct Data
{
unsigned referenceCounter;
int varAmount;
bool useDegreeConversion;
typedef std::map<std::string, unsigned> VarMap_t;
VarMap_t Variables;
typedef std::map<std::string, double> ConstMap_t;
ConstMap_t Constants;
VarMap_t FuncPtrNames;
struct FuncPtrData
{
FunctionPtr ptr; unsigned params;
FuncPtrData(FunctionPtr p, unsigned par): ptr(p), params(par) {}
};
std::vector<FuncPtrData> FuncPtrs;
VarMap_t FuncParserNames;
std::vector<FunctionParser*> FuncParsers;
unsigned* ByteCode;
unsigned ByteCodeSize;
double* Immed;
unsigned ImmedSize;
double* Stack;
unsigned StackSize;
Data();
~Data();
Data(const Data&);
Data& operator=(const Data&); // not implemented on purpose
};
Data* data;
unsigned evalRecursionLevel;
// Temp data needed in Compile():
unsigned StackPtr;
std::vector<unsigned>* tempByteCode;
std::vector<double>* tempImmed;
// Private methods:
// ---------------
void copyOnWrite();
bool checkRecursiveLinking(const FunctionParser*) const;
bool isValidName(const std::string&) const;
Data::VarMap_t::const_iterator FindVariable(const char*,
const Data::VarMap_t&) const;
Data::ConstMap_t::const_iterator FindConstant(const char*) const;
int CheckSyntax(const char*);
bool Compile(const char*);
bool IsVariable(int);
void AddCompiledByte(unsigned);
void AddImmediate(double);
void AddFunctionOpcode(unsigned);
inline void incStackPtr();
int CompileIf(const char*, int);
int CompileFunctionParams(const char*, int, unsigned);
int CompileElement(const char*, int);
int CompilePow(const char*, int);
int CompileUnaryMinus(const char*, int);
int CompileMult(const char*, int);
int CompileAddition(const char*, int);
int CompileComparison(const char*, int);
int CompileAnd(const char*, int);
int CompileOr(const char*, int);
int CompileExpression(const char*, int, bool=false);
void MakeTree(void*) const;
};
#endif