Home

Download

Features

Screenshots

Handbook

Browse Source

Authors

SourceForge.net Logo
Hosted by SourceForge.net

OSI Certified


Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   Related Pages   Search  

fparser.hh

00001 /***************************************************************************\
00002 |* Function parser v2.51 by Warp                                           *|
00003 |* -----------------------------                                           *|
00004 |* Parses and evaluates the given function with the given variable values. *|
00005 |*                                                                         *|
00006 \***************************************************************************/
00007 
00008 #ifndef ONCE_FPARSER_H_
00009 #define ONCE_FPARSER_H_
00010 
00011 #include <string>
00012 #include <map>
00013 #include <vector>
00014 #include <iostream>
00015 
00016 class FunctionParser
00017 {
00018 public:
00019     int Parse(const std::string& Function, const std::string& Vars,
00020               bool useDegrees = false);
00021     const char* ErrorMsg(void) const;
00022     double Eval(const double* Vars);
00023     inline int EvalError(void) const { return EvalErrorType; }
00024 
00025     bool AddConstant(const std::string& name, double value);
00026 
00027     typedef double (*FunctionPtr)(const double*);
00028 
00029     bool AddFunction(const std::string& name,
00030                      FunctionPtr, unsigned paramsAmount);
00031     bool AddFunction(const std::string& name, FunctionParser&);
00032 
00033     void Optimize();
00034 
00035 
00036     FunctionParser();
00037     ~FunctionParser();
00038 
00039 
00040 
00041     // For debugging purposes only:
00042     void PrintByteCode(std::ostream& dest) const;
00043 
00044 protected:
00045     int varAmount, ParseErrorType,EvalErrorType;       
00046     
00047 //========================================================================
00048 private:
00049 //========================================================================
00050 
00051     typedef std::map<std::string, unsigned> VarMap_t;
00052     VarMap_t Variables;
00053 
00054     typedef std::map<std::string, double> ConstMap_t;
00055     ConstMap_t Constants;
00056 
00057     VarMap_t FuncPtrNames;
00058     struct FuncPtrData
00059     {
00060         FunctionPtr ptr; unsigned params;
00061         FuncPtrData(FunctionPtr p, unsigned par): ptr(p), params(par) {}
00062     };
00063     std::vector<FuncPtrData> FuncPtrs;
00064 
00065     VarMap_t FuncParserNames;
00066     std::vector<FunctionParser*> FuncParsers;
00067 
00068     struct CompiledCode
00069     {   CompiledCode();
00070         ~CompiledCode();
00071 
00072         unsigned* ByteCode;
00073         unsigned ByteCodeSize;
00074         double* Immed;
00075         unsigned ImmedSize;
00076         double* Stack;
00077         unsigned StackSize, StackPtr;
00078     } Comp;
00079 
00080     // Temp vectors for bytecode:
00081     std::vector<unsigned>* tempByteCode;
00082     std::vector<double>* tempImmed;
00083 
00084     bool useDegreeConversion;
00085 
00086     bool checkRecursiveLinking(const FunctionParser*);
00087 
00088     bool isValidName(const std::string&);
00089     VarMap_t::const_iterator FindVariable(const char*, const VarMap_t&);
00090     ConstMap_t::const_iterator FindConstant(const char*);
00091     int CheckSyntax(const char*);
00092     bool Compile(const char*);
00093     bool IsVariable(int);
00094     void AddCompiledByte(unsigned);
00095     void AddImmediate(double);
00096     void AddFunctionOpcode(unsigned);
00097     int CompileIf(const char*, int);
00098     int CompileFunctionParams(const char*, int, unsigned);
00099     int CompileElement(const char*, int);
00100     int CompilePow(const char*, int);
00101     int CompileMult(const char*, int);
00102     int CompileAddition(const char*, int);
00103     int CompileComparison(const char*, int);
00104     int CompileAnd(const char*, int);
00105     int CompileOr(const char*, int);
00106     int CompileExpression(const char*, int, bool=false);
00107 
00108 
00109     void MakeTree(struct CodeTree *result) const;
00110 
00111     FunctionParser(const FunctionParser&);
00112     const FunctionParser& operator=(const FunctionParser&);
00113 };
00114 
00115 #endif