GClasses
GClasses::GFunctionParser Class Reference

Detailed Description

This class parses math equations. (This is useful, for example, for plotting tools.)

#include <GFunction.h>

Public Member Functions

 GFunctionParser ()
 szEquations is a set of equations separated by semicolons. For example, it could parse "f(x)=3*x+2" or "f(x)=(g(x)+1)/g(x); g(x)=sqrt(x)+pi" or "h(bob)=bob^2;somefunc(x)=3+blah(x,5)*h(x)-(x/foo);blah(a,b)=a*b-b;foo=3.2" Built in constants include: e, and pi. Built in functions include: +, -, *, /, %, ^, abs, acos, acosh, asin, asinh, atan, atanh, ceil, cos, cosh, erf, floor, gamma, lgamma, log, max, min, sin, sinh, sqrt, tan, and tanh. These generally have the same meaning as in C, except '^' means exponent, "gamma" is the gamma function, and max and min can support any number of parameters >= 1. "ifzero" returns its second parameter if its first parameter is zero (when rounded), otherwise returns its third parameter. "ifnegative" returns its second parameter if its first parameter is negative, otherwise returns its third parameter. (Some of these functions may not not be available on Windows, but most of them are.) You can override any built in constants or functions with your own variables or functions, so you don't need to worry too much about name collisions. Variables must begin with an alphabet character or an underscore. Multiplication is never implicit, so you must use a '*' character to multiply. Whitespace is ignored. If it can't parse something, it will throw an exception. Linking is done lazily, so it won't complain about undefined identifiers until you try to call the function. More...
 
virtual ~GFunctionParser ()
 
void add (const char *szEquations)
 Parses the given string and adds its contents to the set of known functions and values. More...
 
GFunctiongetFunction (const char *name)
 Returns a pointer to the specified function. (Don't include any parentheses in the function name.) Throws if it is not found. More...
 
GFunctiongetFunctionNoThrow (const char *name)
 Returns a pointer to the specified function. (Don't include any parentheses in the function name.) Returns NULL if it is not found. More...
 
virtual void onOverride (const char *name)
 This method is called whenever a function is added with the same name as an existing function, replacing its functionality. For example, if you want to disallow function overriding, you could override this method to throw an exception. More...
 

Static Public Member Functions

static void test ()
 Performs unit tests for this class. Throws an exception if there is a failure. More...
 

Protected Member Functions

void addFunction (const char *name, GFunctionNode *pRoot, int expectedParams)
 
int findOperatorWithLowestPrecidence (std::vector< std::string > &tokens, int start, int count)
 
void parseCommaSeparatedChildren (std::vector< std::string > &variables, GFunctionCall *pFunc, std::vector< std::string > &tokens, int start, int count, int depth)
 
GFunctionNode * parseFunction (std::vector< std::string > &tokens, int start, int count)
 
GFunctionNode * parseFunctionBody (std::vector< std::string > &variables, std::vector< std::string > &tokens, int start, int count, int depth)
 
void parseFunctionList (std::vector< std::string > &tokens)
 
GFunctionNode * parseMathOperator (std::vector< std::string > &variables, std::vector< std::string > &tokens, int start, int count, int index, int depth)
 
void parseVariableNames (std::vector< std::string > &variables, std::vector< std::string > &tokens, int start, int count)
 

Protected Attributes

std::map< std::string, GFunction * > m_functions
 
GFunctionBuiltIn * m_pDivide
 
GFunctionBuiltIn * m_pExponent
 
GFunctionBuiltIn * m_pMinus
 
GFunctionBuiltIn * m_pModulus
 
GFunctionBuiltIn * m_pNegate
 
GFunctionBuiltIn * m_pPlus
 
GFunctionBuiltIn * m_pTimes
 

Constructor & Destructor Documentation

GClasses::GFunctionParser::GFunctionParser ( )

szEquations is a set of equations separated by semicolons. For example, it could parse "f(x)=3*x+2" or "f(x)=(g(x)+1)/g(x); g(x)=sqrt(x)+pi" or "h(bob)=bob^2;somefunc(x)=3+blah(x,5)*h(x)-(x/foo);blah(a,b)=a*b-b;foo=3.2" Built in constants include: e, and pi. Built in functions include: +, -, *, /, %, ^, abs, acos, acosh, asin, asinh, atan, atanh, ceil, cos, cosh, erf, floor, gamma, lgamma, log, max, min, sin, sinh, sqrt, tan, and tanh. These generally have the same meaning as in C, except '^' means exponent, "gamma" is the gamma function, and max and min can support any number of parameters >= 1. "ifzero" returns its second parameter if its first parameter is zero (when rounded), otherwise returns its third parameter. "ifnegative" returns its second parameter if its first parameter is negative, otherwise returns its third parameter. (Some of these functions may not not be available on Windows, but most of them are.) You can override any built in constants or functions with your own variables or functions, so you don't need to worry too much about name collisions. Variables must begin with an alphabet character or an underscore. Multiplication is never implicit, so you must use a '*' character to multiply. Whitespace is ignored. If it can't parse something, it will throw an exception. Linking is done lazily, so it won't complain about undefined identifiers until you try to call the function.

virtual GClasses::GFunctionParser::~GFunctionParser ( )
virtual

Member Function Documentation

void GClasses::GFunctionParser::add ( const char *  szEquations)

Parses the given string and adds its contents to the set of known functions and values.

void GClasses::GFunctionParser::addFunction ( const char *  name,
GFunctionNode *  pRoot,
int  expectedParams 
)
protected
int GClasses::GFunctionParser::findOperatorWithLowestPrecidence ( std::vector< std::string > &  tokens,
int  start,
int  count 
)
protected
GFunction* GClasses::GFunctionParser::getFunction ( const char *  name)

Returns a pointer to the specified function. (Don't include any parentheses in the function name.) Throws if it is not found.

GFunction* GClasses::GFunctionParser::getFunctionNoThrow ( const char *  name)

Returns a pointer to the specified function. (Don't include any parentheses in the function name.) Returns NULL if it is not found.

virtual void GClasses::GFunctionParser::onOverride ( const char *  name)
inlinevirtual

This method is called whenever a function is added with the same name as an existing function, replacing its functionality. For example, if you want to disallow function overriding, you could override this method to throw an exception.

void GClasses::GFunctionParser::parseCommaSeparatedChildren ( std::vector< std::string > &  variables,
GFunctionCall *  pFunc,
std::vector< std::string > &  tokens,
int  start,
int  count,
int  depth 
)
protected
GFunctionNode* GClasses::GFunctionParser::parseFunction ( std::vector< std::string > &  tokens,
int  start,
int  count 
)
protected
GFunctionNode* GClasses::GFunctionParser::parseFunctionBody ( std::vector< std::string > &  variables,
std::vector< std::string > &  tokens,
int  start,
int  count,
int  depth 
)
protected
void GClasses::GFunctionParser::parseFunctionList ( std::vector< std::string > &  tokens)
protected
GFunctionNode* GClasses::GFunctionParser::parseMathOperator ( std::vector< std::string > &  variables,
std::vector< std::string > &  tokens,
int  start,
int  count,
int  index,
int  depth 
)
protected
void GClasses::GFunctionParser::parseVariableNames ( std::vector< std::string > &  variables,
std::vector< std::string > &  tokens,
int  start,
int  count 
)
protected
static void GClasses::GFunctionParser::test ( )
static

Performs unit tests for this class. Throws an exception if there is a failure.

Member Data Documentation

std::map<std::string, GFunction*> GClasses::GFunctionParser::m_functions
protected
GFunctionBuiltIn* GClasses::GFunctionParser::m_pDivide
protected
GFunctionBuiltIn* GClasses::GFunctionParser::m_pExponent
protected
GFunctionBuiltIn* GClasses::GFunctionParser::m_pMinus
protected
GFunctionBuiltIn* GClasses::GFunctionParser::m_pModulus
protected
GFunctionBuiltIn* GClasses::GFunctionParser::m_pNegate
protected
GFunctionBuiltIn* GClasses::GFunctionParser::m_pPlus
protected
GFunctionBuiltIn* GClasses::GFunctionParser::m_pTimes
protected