1 /***************************************************************************\
2 |* Function parser v2.51 by Warp *|
3 |* ----------------------------- *|
4 |* Parses and evaluates the given function with the given variable values. *|
6 \***************************************************************************/
8 #ifndef ONCE_FPARSER_H_
9 #define ONCE_FPARSER_H_
19 int Parse(const std::string& Function, const std::string& Vars,
20 bool useDegrees = false);
21 const char* ErrorMsg(void) const;
22 double Eval(const double* Vars);
23 inline int EvalError(void) const { return EvalErrorType; }
25 bool AddConstant(const std::string& name, double value);
27 typedef double (*FunctionPtr)(const double*);
29 bool AddFunction(const std::string& name,
30 FunctionPtr, unsigned paramsAmount);
31 bool AddFunction(const std::string& name, FunctionParser&);
41 // For debugging purposes only:
42 void PrintByteCode(std::ostream& dest) const;
44 //========================================================================
46 //========================================================================
47 int varAmount, ParseErrorType,EvalErrorType;
49 typedef std::map<std::string, unsigned> VarMap_t;
52 typedef std::map<std::string, double> ConstMap_t;
55 VarMap_t FuncPtrNames;
58 FunctionPtr ptr; unsigned params;
59 FuncPtrData(FunctionPtr p, unsigned par): ptr(p), params(par) {}
61 std::vector<FuncPtrData> FuncPtrs;
63 VarMap_t FuncParserNames;
64 std::vector<FunctionParser*> FuncParsers;
71 unsigned ByteCodeSize;
75 unsigned StackSize, StackPtr;
78 // Temp vectors for bytecode:
79 std::vector<unsigned>* tempByteCode;
80 std::vector<double>* tempImmed;
82 bool useDegreeConversion;
84 bool checkRecursiveLinking(const FunctionParser*);
86 bool isValidName(const std::string&);
87 VarMap_t::const_iterator FindVariable(const char*, const VarMap_t&);
88 ConstMap_t::const_iterator FindConstant(const char*);
89 int CheckSyntax(const char*);
90 bool Compile(const char*);
92 void AddCompiledByte(unsigned);
93 void AddImmediate(double);
94 void AddFunctionOpcode(unsigned);
95 int CompileIf(const char*, int);
96 int CompileFunctionParams(const char*, int, unsigned);
97 int CompileElement(const char*, int);
98 int CompilePow(const char*, int);
99 int CompileMult(const char*, int);
100 int CompileAddition(const char*, int);
101 int CompileComparison(const char*, int);
102 int CompileAnd(const char*, int);
103 int CompileOr(const char*, int);
104 int CompileExpression(const char*, int, bool=false);
107 void MakeTree(struct CodeTree *result) const;
109 FunctionParser(const FunctionParser&);
110 const FunctionParser& operator=(const FunctionParser&);