]> Shamusworld >> Repos - architektonas/blob - fparser/src/fparser.h
Initial import
[architektonas] / fparser / src / fparser.h
1 /***************************************************************************\
2 |* Function parser v2.51 by Warp                                           *|
3 |* -----------------------------                                           *|
4 |* Parses and evaluates the given function with the given variable values. *|
5 |*                                                                         *|
6 \***************************************************************************/
7
8 #ifndef ONCE_FPARSER_H_
9 #define ONCE_FPARSER_H_
10
11 #include <string>
12 #include <map>
13 #include <vector>
14 #include <iostream>
15
16 class FunctionParser
17 {
18 public:
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; }
24
25     bool AddConstant(const std::string& name, double value);
26
27     typedef double (*FunctionPtr)(const double*);
28
29     bool AddFunction(const std::string& name,
30                      FunctionPtr, unsigned paramsAmount);
31     bool AddFunction(const std::string& name, FunctionParser&);
32
33     void Optimize();
34
35
36     FunctionParser();
37     ~FunctionParser();
38
39
40
41     // For debugging purposes only:
42     void PrintByteCode(std::ostream& dest) const;
43
44 //========================================================================
45 private:
46 //========================================================================
47     int varAmount, ParseErrorType,EvalErrorType;
48
49     typedef std::map<std::string, unsigned> VarMap_t;
50     VarMap_t Variables;
51
52     typedef std::map<std::string, double> ConstMap_t;
53     ConstMap_t Constants;
54
55     VarMap_t FuncPtrNames;
56     struct FuncPtrData
57     {
58         FunctionPtr ptr; unsigned params;
59         FuncPtrData(FunctionPtr p, unsigned par): ptr(p), params(par) {}
60     };
61     std::vector<FuncPtrData> FuncPtrs;
62
63     VarMap_t FuncParserNames;
64     std::vector<FunctionParser*> FuncParsers;
65
66     struct CompiledCode
67     {   CompiledCode();
68         ~CompiledCode();
69
70         unsigned* ByteCode;
71         unsigned ByteCodeSize;
72         double* Immed;
73         unsigned ImmedSize;
74         double* Stack;
75         unsigned StackSize, StackPtr;
76     } Comp;
77
78     // Temp vectors for bytecode:
79     std::vector<unsigned>* tempByteCode;
80     std::vector<double>* tempImmed;
81
82     bool useDegreeConversion;
83
84     bool checkRecursiveLinking(const FunctionParser*);
85
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*);
91     bool IsVariable(int);
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);
105
106
107     void MakeTree(struct CodeTree *result) const;
108
109     FunctionParser(const FunctionParser&);
110     const FunctionParser& operator=(const FunctionParser&);
111 };
112
113 #endif