GClasses

GClasses::GFunctionParser Class Reference

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

#include <GFunction.h>

List of all members.

Public Member Functions

 GFunctionParser (const char *szEquations)
 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. (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.
 ~GFunctionParser ()
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.
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.

Static Public Member Functions

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

Protected Member Functions

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

Protected Attributes

std::vector< GFunctionStub * > m_stubs
std::vector< std::string > m_tokens
std::map< const char
*, GFunction *, strCmp > 
m_functions
GFunctionBuiltIn * m_pNegate
GFunctionBuiltIn * m_pPlus
GFunctionBuiltIn * m_pMinus
GFunctionBuiltIn * m_pTimes
GFunctionBuiltIn * m_pDivide
GFunctionBuiltIn * m_pModulus
GFunctionBuiltIn * m_pExponent

Detailed Description

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


Constructor & Destructor Documentation

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

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. (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.

GClasses::GFunctionParser::~GFunctionParser ( )

Member Function Documentation

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]
void GClasses::GFunctionParser::flushStubs ( ) [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.

GFunctionCall* GClasses::GFunctionParser::makeStubbedOperator ( const char *  szName) [protected]
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<const char*, GFunction*, strCmp> 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]
std::vector<GFunctionStub*> GClasses::GFunctionParser::m_stubs [protected]
std::vector<std::string> GClasses::GFunctionParser::m_tokens [protected]