//========================================================== // General Purpose Library //========================================================== // Evaluator.h // // A class for evaluating mathematical formulas // // Copyright 1995-96 Scott Robert Ladd. All rights reserved //========================================================== #ifndef COYOTE_EVALUATOR_H #define COYOTE_EVALUATOR_H #ifdef _MSC_VER #pragma warning (disable: 4786) #endif #include "string.h" #include "map.h" #include "Diagnose.h" using namespace Coyote; namespace Coyote { #define TOKEN_MAX 32 //----------------------------------------------- // EvalEx // Exceptions generated by Evaluator objects //----------------------------------------------- // error codes enum EvalError { EVX_SYNTAX, EVX_PARENTHESIS, EVX_DECIMAL, EVX_INVCHAR, EVX_NULLDATA, EVX_NOVAR, EVX_OVERFLOW }; // exception objects class EvalEx : public ExceptionBase { public: EvalEx ( EvalError err ) { Error = err; } EvalError WhatsWrong() { return Error; } virtual void Explain ( DiagOutput & out ); private: EvalError Error; }; //-------------------------------------------------- // VarName // Defines a variable name (key) for dictionary //-------------------------------------------------- struct VarName { char Text[TOKEN_MAX + 1]; VarName ( const char * vname = NULL ); VarName ( const VarName & vn ); void operator = ( const VarName & vn ); bool operator == ( const VarName & vn ) const; bool operator < ( const VarName & vn ) const; }; inline VarName::VarName ( const char * vname ) { if ((vname == NULL) || (vname[0] == 0)) Text[0] = 0; else strncpy(Text,vname,TOKEN_MAX); } inline VarName::VarName ( const VarName & vn ) { strncpy(Text,vn.Text,TOKEN_MAX); } inline void VarName::operator = ( const VarName & vn ) { strncpy(Text,vn.Text,TOKEN_MAX); } inline bool VarName::operator == ( const VarName & vn ) const { return (0 == strcmp(Text,vn.Text)); } inline size_t DictHash ( const VarName & vname, size_t nbuck ) { return DictHash(vname.Text,nbuck); } inline bool VarName::operator < ( const VarName & vn ) const { return (strcmp(Text,vn.Text) < 0); } //--------------------------------------------------------- // Evaluator // A class that evaluates mathematical formula strings //--------------------------------------------------------- class Evaluator { public: // default constructor Evaluator(); // copy constructor Evaluator ( const Evaluator & eval ); // assignment operator void operator = ( const Evaluator & eval ); void SetVar ( const char * vname, double vval ); double Compute ( const char * formula ); private: // types enum TokenType { TT_NUMBER, TT_VARIABLE, TT_DELIM, TT_UNKNOWN }; // constants static const char * Whitespace; static const char * Delimiters; static const char * Numbers; static const char * Letters; // variables const char * Eq; // current byte being parsed char Token[TOKEN_MAX]; // current token text TokenType Type; // current token type // stored variables map< VarName,double,less > Vars; // copy next token into Token array void GetToken(); // internal functions double AddSub(); double MultDiv(); double Power(); double Sign(); double Paren(); }; } // end namespace Coyote #endif