JKQTPlotter trunk/v5.0.0
an extensive Qt5+Qt6 Plotter framework (including a feature-richt plotter widget, a speed-optimized, but limited variant and a LaTeX equation renderer!), written fully in C/C++ and without external dependencies
Loading...
Searching...
No Matches

A simple function parser to parse (build memory tree representation) and evaluate simple mathematical expressions. More...

#include <jkqtpmathparser.h>

Collaboration diagram for JKQTPMathParser:
[legend]

Classes

class  jkmpBinaryArithmeticNode
 This class represents a binary arithmetic operation: add (+), subtract (-), multiply (*), divide (/), a to the power of b (a^b) More...
 
class  jkmpBinaryBoolNode
 This class represents a binary boolean operation: and, or, xor, nor, nand. More...
 
class  jkmpCompareNode
 This class represents a binary compare operation: !=, ==, >=, <=, >, <
More...
 
class  jkmpConstantNode
 This class represents a number, a string contant or a boolean contant (true / false ) More...
 
class  jkmpException
 error handling: exceptions of the type of this class will be thrown if an error occurs More...
 
struct  jkmpFunctionDescriptor
 description of a user registered function More...
 
class  jkmpFunctionNode
 This class represents an arbitrary function. More...
 
class  jkmpNode
 This class is the abstract base class for nodes. All allowed node types must inherit from jkmpNode. More...
 
class  jkmpNodeList
 This class represents a list of jkmpNode. More...
 
struct  jkmpResult
 result of any expression More...
 
struct  jkmpTempVariable
 This struct is for managing temporary variables. It is generally like jkmpVariable. More...
 
class  jkmpUnaryNode
 This class represents a unary operations: ! (bool negation), - (arithmetic negation) More...
 
struct  jkmpVariable
 This struct is for managing variables. Unlike jkmpResult this struct only contains pointers to the data. More...
 
class  jkmpVariableAssignNode
 This class represents a variable assignment (a = expression)
More...
 
class  jkmpVariableNode
 This class represents a variable. More...
 

Public Types

Abstract Syntax Tree / Memory Representation of Expressions
enum  jkmpResultType {
  jkmpDouble ,
  jkmpString ,
  jkmpBool
}
 possible result types
More...
 
enum class  jkmpLOP {
  LOPand ='a' ,
  LOPor ='o' ,
  LOPxor ='x' ,
  LOPnor ='n' ,
  LOPnand ='A'
}
 internal names for logic operations More...
 
enum class  jkmpCOMP {
  equal ='=' ,
  nequal ='!' ,
  lesser ='<' ,
  greater ='>' ,
  lesserequal ='a' ,
  greaterequal ='b'
}
 jkmpCOMPdefs internal names for compare operations More...
 
typedef jkmpResult(* jkmpEvaluateFunc) (jkmpResult *, unsigned char, JKQTPMathParser *)
 This is a function prototype for adding new mathematical functions to the parser.
 

Public Member Functions

 JKQTPMathParser ()
 class constructor
 
virtual ~JKQTPMathParser ()
 class destructor
 
void addFunction (const std::string &name, jkmpEvaluateFunc function)
 register a new function
 
void addStandardFunctions ()
 registers standard functions
 
void addStandardVariables ()
 registers standard variables

 
void addVariable (const std::string &name, jkmpResult result)
 register a new internal variable of type boolean
 
void addVariableBoolean (const std::string &name, bool *v)
 register a new external variable of type boolean
 
void addVariableBoolean (const std::string &name, bool v)
 register a new internal variable of type boolean
 
void addVariableDouble (const std::string &name, double *v)
 register a new external variable of type double
 
void addVariableDouble (const std::string &name, double v)
 register a new internal variable of type double
 
void addVariableString (const std::string &name, const std::string &v)
 register a new internal variable of type string
 
void addVariableString (const std::string &name, std::string *v)
 register a new external variable of type string
 
void clearFunctions ()
 clears the list of internal functions
 
void clearVariables ()
 deletes all defined variables. the memory of internal variables will be released. the external memory will not be released.
 
void deleteVariable (const std::string &name)
 delete the specified variabale and releases its internal memory.
 
jkmpResult evaluate (const std::string &prog)
 evaluate the given expression
 
jkmpResult evaluateFunction (const std::string &name, jkmpResult *params, unsigned char n)
 evaluates a registered function
 
bool functionExists (const std::string &name)
 tests whether a function exists
 
std::string getArgCVParam (const std::string &name, const std::string &defaultResult)
 return one of programs command-line arguments, or defaultResult if it is not present, used by the standard functions cmdparam and argv
 
jkmpEvaluateFunc getFunctionDef (const std::string &name)
 returns the defining structure of the given function
 
jkmpResult getVariable (const std::string &name)
 returns the value of the given variable
 
jkmpVariable getVariableDef (const std::string &name)
 returns the defining structure of the given variable
 
jkmpResult getVariableOrInvalid (const std::string &name)
 returns the value of the given variable
 
std::vector< std::pair< std::string, jkmpVariable > > getVariables ()
 returns all registered variables
 
jkmpNodeparse (const std::string &prog)
 parses the given expression
 
void printVariables ()
 prints a list of all registered variables
 
void setArgCV (int argc, char **argv)
 store programs command-line arguments, so they are available in the parser, used by the standard functions cmdparam and argv
 
bool tempvariableExists (const std::string &name)
 tests whether a temporary variable exists
 
bool variableExists (const std::string &name)
 tests whether a variable exists
 

Protected Member Functions

void addTempVariable (const std::string &name, jkmpResult value)
 adds a temporary variable
 
void setVariable (const std::string &name, jkmpResult value)
 set the defining struct of the given variable
 
void setVariableDouble (const std::string &name, double value)
 set the defining struct of the given variable
 
Parser
jkmpNodecompExpression (bool get)
 recognizes an compExpression while parsing. If get ist true, this function first retrieves a new token by calling getToken()
 
jkmpNodelogicalExpression (bool get)
 recognizes a logicalExpression while parsing. If get ist true, this function first retrieves a new token by calling getToken()
 
jkmpNodelogicalTerm (bool get)
 recognizes a logicalTerm while parsing. If get ist true, this function first retrieves a new token by calling getToken()
 
jkmpNodemathExpression (bool get)
 recognizes a mathExpression while parsing. If get ist true, this function first retrieves a new token by calling getToken()
 
jkmpNodemathTerm (bool get)
 recognizes a term while parsing. If get ist true, this function first retrieves a new token by calling getToken()
 
jkmpNodeprimary (bool get)
 recognizes a primary while parsing. If get ist true, this function first retrieves a new token by calling getToken()
 

Protected Attributes

int argc
 storage for program argument cound, used by the standard functions cmdparam and argv
 
char ** argv
 storage for program arguments, used by the standard functions cmdparam and argv
 
std::map< std::string, jkmpFunctionDescriptorfunctions
 map to manage all currently rtegistered functions
 
std::vector< jkmpTempVariabletempvariables
 vector containing all temporary variables
 
std::map< std::string, jkmpVariablevariables
 map to manage all currently defined variables
 

Error Handling

typedef void(* jkmpexceptionf) (std::string)
 type for a custom error handler. This an alternative error handling

 
jkmpexceptionf jkmathparser_exception_function
 if this is nullptr then an exception may be thrown otherwise this should point to an error handler that will be called.
 
void jkmpError (const std::string &st)
 function that throws an exception or calls an error handler
 
void setException_function (jkmpexceptionf exception_function)
 activate error handling by use of an exception function

 
void resetException_function ()
 deactivate error handling by use of an exception function
 

Tokenizer

enum  jkmpTokenType {
  END ,
  PRINT ,
  PARAMETER_DIV ,
  STRING_DELIM ,
  NAME ,
  NUMBER ,
  PLUS ,
  MINUS ,
  MUL ,
  DIV ,
  MODULO ,
  ASSIGN ,
  LBRACKET ,
  RBRACKET ,
  POWER ,
  FACTORIAL_LOGIC_NOT ,
  LOGIC_NOT ,
  LOGIC_AND ,
  LOGIC_OR ,
  LOGIC_XOR ,
  LOGIC_NOR ,
  LOGIC_NAND ,
  LOGIC_TRUE ,
  LOGIC_FALSE ,
  COMP_EQUALT ,
  COMP_UNEQUAL ,
  COMP_GREATER ,
  COMP_SMALLER ,
  COMP_GEQUAL ,
  COMP_SEQUAL
}
 the possible tokens that can be recognized by the tokenizer in JKQTPMathParser::getToken()
More...
 
jkmpTokenType CurrentToken
 the current token while parsing a string
 
std::string StringValue
 the string value of the current token (when applicable) during the parsing step
 
double NumberValue
 the string value of the current token (when applicable) during the parsing step
 
std::istringstream * program
 this stream is used to read in the program. An object is created and assigned (and destroyed) by the parse()-function
 
std::string tokentostring (jkmpTokenType token)
 return the given token as human-readable string
 
std::string currenttokentostring ()
 return the current token as human-readable string
 
jkmpTokenType getToken ()
 Tokenizer: extract the next token from the input.
 
std::string readDelim (char delimiter)
 return a delimited text, i.e. extract the texte between the delimiters "</code> in: of <code>"Hallo!", i.e. returns Hallo! This is used to parse string constants.
 

Detailed Description

A simple function parser to parse (build memory tree representation) and evaluate simple mathematical expressions.

This class implements a simple function parser which can parse mathematical expressions like z=a*3+2.34^2*sin(pi*sqrt(x)) . More than one expression can be separated by semicolon ';'. The result of a parse operation will be a memory structure (tree) representing the given expression. The parser can cope with constants and (user defined) variables and supports the data types number (double precision floating point), boolean (true/false) and string. It already contains a lot of fundamental mathematical functions (i.e. nearly all the functions from C StdLib).

constants and variables:

This class provides management utilities for constants and variables. Variables can also be defined as external variables, when a pointer to corresponding memory is provided by the calling program. The parser supports a variable assign operation

a=<expression>

which allows to define new variables during evaluation. There are some mathematical standard constants registered by calling JKQTPMathParser::addStandardVariables():

  • pi = $ \pi $
  • e = $ \exp(1) $
  • sqrt2 = $ \sqrt{2} $
  • version = the parser version
  • log2e = $ \log_2(e) $
  • log10e = $ \log_{10}(e) $
  • ln2 = $ \ln(2)=\log_{e}(2) $
  • ln10 = $ \ln(10)=\log_{e}(10) $
  • h = $ 6.6260689633\cdot 10^{-34}\;\mathrm{J\cdot s} $ (planck constant)
  • hbar = $ \hbar=\frac{h}{2\pi}= 1.05457162853\cdot 10^{-34}\;\mathrm{J\cdot s} $ (planck constant)
  • epsilon0 = $ \epsilon_0= 8.854187817\cdot 10^{-12}\;\mathrm{\frac{F}{m}} $ (electric constant)
  • mu0 = $ \mu_0=2\pi\cdot 10^{-7}= 12.566370614\cdot 10^{-7}\;\mathrm{\frac{N}{A^2}} $ (magnetic constant)
  • c = $ 299792458\;\mathrm{\frac{m}{s}} $ (speed of light in vacuum)
  • ce = $ 1.60217648740\cdot 10^{-19}\;\mathrm{C}$ (elementary charge)
  • muB = $ \mu_B= 927.40091523\cdot 10^{-26}\;\mathrm{\frac{J}{T}} $ (Bohr magneton)
  • muB_eV = $ \mu_B=5.788381755579\cdot 10^{-5}\;\mathrm{\frac{eV}{T}}  $ (Bohr magneton)
  • muN = $ \mu_N=5.0507832413\cdot 10^{-27}\;\mathrm{\frac{J}{T}}  $ (nuclear magneton)
  • muN_eV = $ \mu_N=3.152451232645\cdot 10^{-8}\;\mathrm{\frac{eV}{T}}  $ (nuclear magneton)
  • me = $ m_e= 9.1093821545\cdot 10^{-31}\;\mathrm{kg} $ (mass of electron)
  • mp = $ m_p= 1.67262163783\cdot 10^{-27}\;\mathrm{kg} $ (mass of proton)
  • mn = $ m_n= 1.67492721184\cdot 10^{-27}\;\mathrm{kg} $ (mass of neutron)
  • NA = $ N_A= 6.0221417930\cdot 10^{23} $ (Avogadro constant = particles in 1 mol)
  • kB = $ k_B=1.380650424\cdot 10^{-23}\;\mathrm{\frac{J}{K}}  $ (Boltzman constant)
  • kB_eV = $ k_B=8.61734315\cdot 10^{-5}\;\mathrm{\frac{eV}{K}}  $ (Boltzman constant)

You can add user-defined contants by calling JKQTPMathParser::addVariableDouble() JKQTPMathParser::addVariableBoolean() or JKQTPMathParser::addVariableString()

functions:

this class provides a wide range of (mathematical) functions:

  • sinc, gauss, slit, theta, tanc, sigmoid
  • asin, acos, atan, atan2,
  • sin, cos, tan,
  • sinh, cosh, tanh
  • log, log2, log10,
  • exp, sqr, sqrt
  • abs, sign
  • if
  • erf, erfc, lgamma, tgamma, j0, j1, jn, y0, y1, yn
  • rand, srand
  • ceil, floor, trunc, round,
  • fmod, min, max
  • floattostr, booltostr/bool2str, inttostr, num2str
  • tosystempathseparator
  • setdefault
  • strdate
  • cmdparam, argv

these functions are registered by calling JKQTPMathParser::addStandardFunctions(). you can add new functions by calling JKQTPMathParser::addFunction();

result of parsing and evaluation:

The result of calling JKQTPMathParser::parse() will be a tree-like structure in memory. The parse() function will return a pointer to the root node of this structure. All nodes inherit from jkmpNode class. To evaluate such a structure simply call jkmpNode::evaluate() of the root node. This will then return a jkmpResult structure which contains the result. This scheme allows for once parsing and multiply evaluating an expression. So if you want to define a function by an expression you can provide an external variable x as the argument and then evaluate the function for each x.

EBNF definition of the parsed expressions

 logical_expression ->  logical_term
                      | logical_expression or logical_term
                      | logical_expression || logical_term
 logical_term    ->  comp_expression
                      | logical_term and comp_expression
                      | logical_term && comp_expression
 comp_expression  ->  math_expression
                      | expression == math_expression
                      | expression != math_expression
                      | expression >= math_expression
                      | expression <= math_expression
                      | expression > math_expression
                      | expression < math_expression
  math_expression ->  term
                      | math_expression + math_term
                      | math_expression - math_term
  math_term       ->  primary
                      | term * primary
                      | term / primary
                      | term ( % | mod ) primary
  primary         ->  true | false
                      | string_constant
                      | NUMBER
                      | NAME
                      | NAME = logical_expression
                      | + primary | - primary | ! primary | not primary
                      | ( logical_expression )
                      | NAME( parameter_list )
                      | primary ^ primary
  string_constant -> " STRING " | ' STRING '
  parameter_list  -> $ \lambda $ | logical_expression | logical_expression , parameter_list

Simple Example of Usage

try {
JKQTPMathParser mp; // instanciate
// parse some numeric expression
n=mp.parse("pi^2+4*sin(65*pi/exp(3.45))");
r=n->evaluate();
cout<<r.num<<endl;
//delete n;
// parse some boolean expression
n=mp.parse("true==false");
r=n->evaluate();
if (r.type==jkmpBool) {
if (r.boolean) cout<<"true";
else cout<<"false";
}
if (r.type==jkmpDouble) cout<<r.num<<endl;
if (r.type==jkmpString) cout<<r.str<<endl;
delete n;
// parse some string expression
n=mp.parse("var1='false'; var1+'true'");
r=n->evaluate();
if (r.type==jkmpString) cout<<r.str<<endl;
delete n;
} catch(std::exception& E) {
cout<<"ERROR!!!\n "<<E.what()<<endl<<endl;
}
This class is the abstract base class for nodes. All allowed node types must inherit from jkmpNode.
Definition jkqtpmathparser.h:316
virtual jkmpResult evaluate()=0
evaluate this node
A simple function parser to parse (build memory tree representation) and evaluate simple mathematical...
Definition jkqtpmathparser.h:220
@ jkmpString
a string of characters
Definition jkqtpmathparser.h:228
@ jkmpDouble
a floating-point number with double precision. This is also used to deal with integers
Definition jkqtpmathparser.h:227
@ jkmpBool
Definition jkqtpmathparser.h:229
jkmpNode * parse(const std::string &prog)
parses the given expression
result of any expression
Definition jkqtpmathparser.h:234
std::string str
contains result if type==jkmpString
Definition jkqtpmathparser.h:239
jkmpResultType type
type of the result
Definition jkqtpmathparser.h:238
bool boolean
contains result if type==jkmpBool
Definition jkqtpmathparser.h:241
double num
contains result if type==jkmpDouble
Definition jkqtpmathparser.h:240

Error Handling

In the above example we use error handling by use of exception (default behauviour).

It is also possible to change the error handling from using exceptions to calling a specific error handling function. This can be usefull in programs that don't support exceptions. To do so, use this cod:

void error(const std::string& message) {
cout<<"error: "+message;
system("PAUSE");
abort();
}
int main() {
mp.setException_function(error); // make error ahndler known
...
}
void setException_function(jkmpexceptionf exception_function)
activate error handling by use of an exception function

Member Typedef Documentation

◆ jkmpEvaluateFunc

typedef jkmpResult(* JKQTPMathParser::jkmpEvaluateFunc) (jkmpResult *, unsigned char, JKQTPMathParser *)

This is a function prototype for adding new mathematical functions to the parser.

If you want to add more math functions (like sin, cos , abs ...) to the parser, you will have to implement it with this prototype and then register it with JKQTPMathParser::addFunction(). The first parameter points to an array containing the input parameters while the second one specifies the number of supplied parameters. The result has to be of type jkmpResult.

All error handling has to be done inside the function definition. Here is a simple example:

jkmpResult Abs(jkmpResult* params, unsigned char n){
if (n!=1) jkmpError("abs accepts 1 argument");
if (params[0].type!=jkmpDouble) jkmpError("abs needs double argument");
r.num=fabs(params[0].num);
return r;
}
void jkmpError(const std::string &st)
function that throws an exception or calls an error handler

◆ jkmpexceptionf

typedef void(* JKQTPMathParser::jkmpexceptionf) (std::string)

type for a custom error handler. This an alternative error handling

Member Enumeration Documentation

◆ jkmpCOMP

enum class JKQTPMathParser::jkmpCOMP
strong

jkmpCOMPdefs internal names for compare operations

Enumerator
equal 
nequal 
lesser 
greater 
lesserequal 
greaterequal 

◆ jkmpLOP

enum class JKQTPMathParser::jkmpLOP
strong

internal names for logic operations

Enumerator
LOPand 
LOPor 
LOPxor 
LOPnor 
LOPnand 

◆ jkmpResultType

possible result types

Enumerator
jkmpDouble 

a floating-point number with double precision. This is also used to deal with integers

jkmpString 

a string of characters

jkmpBool 

◆ jkmpTokenType

the possible tokens that can be recognized by the tokenizer in JKQTPMathParser::getToken()

Enumerator
END 

end token

PRINT 

a semicolon ';'

PARAMETER_DIV 

a comma ',' between two function parameters

STRING_DELIM 

a string delimiter ' or "

NAME 

a name (consisting of characters) of a variable or function

NUMBER 

a number in scientific notation

PLUS 

a plus operator '+'

MINUS 

a minus operator '-'

MUL 

a multiplication operator '*'

DIV 

a division operator '/'

MODULO 

a modulo operator ''

ASSIGN 

a variable assignment =

LBRACKET 

left brackets '('

RBRACKET 

right brackets ')'

POWER 

a power operator '^'

FACTORIAL_LOGIC_NOT 

a factorial operator or a logical NOT '!'

LOGIC_NOT 

a logical NOT '!' / 'not'

LOGIC_AND 

a logical AND operator '&&' / 'and'

LOGIC_OR 

a logical OR operator '||' / 'or'

LOGIC_XOR 

a logical XOR operator 'xor'

LOGIC_NOR 

a logical NOR operator 'nor'

LOGIC_NAND 

a logical NAND operator 'nand'

LOGIC_TRUE 

'true'

LOGIC_FALSE 

'false'

COMP_EQUALT 

equals operation '=='

COMP_UNEQUAL 

unequal operation '!='

COMP_GREATER 

greater than operation '>'

COMP_SMALLER 

smaller than operation '<'

COMP_GEQUAL 

greater than or equal operation '>='

COMP_SEQUAL 

smaller than or equal operation '<='

Constructor & Destructor Documentation

◆ JKQTPMathParser()

JKQTPMathParser::JKQTPMathParser ( )

class constructor

Note
This also registers all standatd functions and constants by calling addStandardFunctions() and addStandardVariables()

◆ ~JKQTPMathParser()

virtual JKQTPMathParser::~JKQTPMathParser ( )
virtual

class destructor

Member Function Documentation

◆ addFunction()

void JKQTPMathParser::addFunction ( const std::string &  name,
jkmpEvaluateFunc  function 
)

register a new function

Parameters
namename of the new function
functiona pointer to the implementation

◆ addStandardFunctions()

void JKQTPMathParser::addStandardFunctions ( )

registers standard functions

◆ addStandardVariables()

void JKQTPMathParser::addStandardVariables ( )

registers standard variables

◆ addTempVariable()

void JKQTPMathParser::addTempVariable ( const std::string &  name,
jkmpResult  value 
)
protected

adds a temporary variable

◆ addVariable()

void JKQTPMathParser::addVariable ( const std::string &  name,
jkmpResult  result 
)

register a new internal variable of type boolean

Parameters
namename of the new variable
resultinitial value of this variable

◆ addVariableBoolean() [1/2]

void JKQTPMathParser::addVariableBoolean ( const std::string &  name,
bool *  v 
)

register a new external variable of type boolean

Parameters
namename of the new variable
vpointer to the variable memory

◆ addVariableBoolean() [2/2]

void JKQTPMathParser::addVariableBoolean ( const std::string &  name,
bool  v 
)

register a new internal variable of type boolean

Parameters
namename of the new variable
vinitial value of this variable

◆ addVariableDouble() [1/2]

void JKQTPMathParser::addVariableDouble ( const std::string &  name,
double *  v 
)

register a new external variable of type double

Parameters
namename of the new variable
vpointer to the variable memory

◆ addVariableDouble() [2/2]

void JKQTPMathParser::addVariableDouble ( const std::string &  name,
double  v 
)

register a new internal variable of type double

Parameters
namename of the new variable
vinitial value of this variable

◆ addVariableString() [1/2]

void JKQTPMathParser::addVariableString ( const std::string &  name,
const std::string &  v 
)

register a new internal variable of type string

Parameters
namename of the new variable
vinitial value of this variable

◆ addVariableString() [2/2]

void JKQTPMathParser::addVariableString ( const std::string &  name,
std::string *  v 
)

register a new external variable of type string

Parameters
namename of the new variable
vpointer to the variable memory

◆ clearFunctions()

void JKQTPMathParser::clearFunctions ( )

clears the list of internal functions

◆ clearVariables()

void JKQTPMathParser::clearVariables ( )

deletes all defined variables. the memory of internal variables will be released. the external memory will not be released.

◆ compExpression()

jkmpNode * JKQTPMathParser::compExpression ( bool  get)
protected

recognizes an compExpression while parsing. If get ist true, this function first retrieves a new token by calling getToken()

◆ currenttokentostring()

std::string JKQTPMathParser::currenttokentostring ( )
protected

return the current token as human-readable string

◆ deleteVariable()

void JKQTPMathParser::deleteVariable ( const std::string &  name)

delete the specified variabale and releases its internal memory.

◆ evaluate()

jkmpResult JKQTPMathParser::evaluate ( const std::string &  prog)

evaluate the given expression

◆ evaluateFunction()

jkmpResult JKQTPMathParser::evaluateFunction ( const std::string &  name,
jkmpResult params,
unsigned char  n 
)

evaluates a registered function

Parameters
namename of the (registered function) to be evaluated
paramsarray of the input parameters
nnumber of input parameters (<=8)

◆ functionExists()

bool JKQTPMathParser::functionExists ( const std::string &  name)

tests whether a function exists

◆ getArgCVParam()

std::string JKQTPMathParser::getArgCVParam ( const std::string &  name,
const std::string &  defaultResult 
)

return one of programs command-line arguments, or defaultResult if it is not present, used by the standard functions cmdparam and argv

◆ getFunctionDef()

jkmpEvaluateFunc JKQTPMathParser::getFunctionDef ( const std::string &  name)

returns the defining structure of the given function

◆ getToken()

jkmpTokenType JKQTPMathParser::getToken ( )
protected

Tokenizer: extract the next token from the input.

◆ getVariable()

jkmpResult JKQTPMathParser::getVariable ( const std::string &  name)

returns the value of the given variable

◆ getVariableDef()

jkmpVariable JKQTPMathParser::getVariableDef ( const std::string &  name)

returns the defining structure of the given variable

◆ getVariableOrInvalid()

jkmpResult JKQTPMathParser::getVariableOrInvalid ( const std::string &  name)

returns the value of the given variable

◆ getVariables()

std::vector< std::pair< std::string, jkmpVariable > > JKQTPMathParser::getVariables ( )

returns all registered variables

◆ jkmpError()

void JKQTPMathParser::jkmpError ( const std::string &  st)

function that throws an exception or calls an error handler

◆ logicalExpression()

jkmpNode * JKQTPMathParser::logicalExpression ( bool  get)
protected

recognizes a logicalExpression while parsing. If get ist true, this function first retrieves a new token by calling getToken()

◆ logicalTerm()

jkmpNode * JKQTPMathParser::logicalTerm ( bool  get)
protected

recognizes a logicalTerm while parsing. If get ist true, this function first retrieves a new token by calling getToken()

◆ mathExpression()

jkmpNode * JKQTPMathParser::mathExpression ( bool  get)
protected

recognizes a mathExpression while parsing. If get ist true, this function first retrieves a new token by calling getToken()

◆ mathTerm()

jkmpNode * JKQTPMathParser::mathTerm ( bool  get)
protected

recognizes a term while parsing. If get ist true, this function first retrieves a new token by calling getToken()

◆ parse()

jkmpNode * JKQTPMathParser::parse ( const std::string &  prog)

parses the given expression

◆ primary()

jkmpNode * JKQTPMathParser::primary ( bool  get)
protected

recognizes a primary while parsing. If get ist true, this function first retrieves a new token by calling getToken()

◆ printVariables()

void JKQTPMathParser::printVariables ( )

prints a list of all registered variables

◆ readDelim()

std::string JKQTPMathParser::readDelim ( char  delimiter)
protected

return a delimited text, i.e. extract the texte between the delimiters "</code> in: of <code>"Hallo!", i.e. returns Hallo! This is used to parse string constants.

This functions actually reads pascal style delimited string constants. So if you want to use the delimiter as part of the string you will have to write it as doubled character. So 'Jan''s Test' stands for Jan's Test.

◆ resetException_function()

void JKQTPMathParser::resetException_function ( )

deactivate error handling by use of an exception function

◆ setArgCV()

void JKQTPMathParser::setArgCV ( int  argc,
char **  argv 
)

store programs command-line arguments, so they are available in the parser, used by the standard functions cmdparam and argv

◆ setException_function()

void JKQTPMathParser::setException_function ( jkmpexceptionf  exception_function)

activate error handling by use of an exception function

◆ setVariable()

void JKQTPMathParser::setVariable ( const std::string &  name,
jkmpResult  value 
)
protected

set the defining struct of the given variable

◆ setVariableDouble()

void JKQTPMathParser::setVariableDouble ( const std::string &  name,
double  value 
)
protected

set the defining struct of the given variable

◆ tempvariableExists()

bool JKQTPMathParser::tempvariableExists ( const std::string &  name)

tests whether a temporary variable exists

◆ tokentostring()

std::string JKQTPMathParser::tokentostring ( jkmpTokenType  token)
protected

return the given token as human-readable string

◆ variableExists()

bool JKQTPMathParser::variableExists ( const std::string &  name)

tests whether a variable exists

Member Data Documentation

◆ argc

int JKQTPMathParser::argc
protected

storage for program argument cound, used by the standard functions cmdparam and argv

See also
setArgCV() and getArgCVParam()

◆ argv

char** JKQTPMathParser::argv
protected

storage for program arguments, used by the standard functions cmdparam and argv

See also
setArgCV() and getArgCVParam()

◆ CurrentToken

jkmpTokenType JKQTPMathParser::CurrentToken
protected

the current token while parsing a string

◆ functions

std::map<std::string, jkmpFunctionDescriptor> JKQTPMathParser::functions
protected

map to manage all currently rtegistered functions

◆ jkmathparser_exception_function

jkmpexceptionf JKQTPMathParser::jkmathparser_exception_function
private

if this is nullptr then an exception may be thrown otherwise this should point to an error handler that will be called.

◆ NumberValue

double JKQTPMathParser::NumberValue
protected

the string value of the current token (when applicable) during the parsing step

◆ program

std::istringstream* JKQTPMathParser::program
protected

this stream is used to read in the program. An object is created and assigned (and destroyed) by the parse()-function

◆ StringValue

std::string JKQTPMathParser::StringValue
protected

the string value of the current token (when applicable) during the parsing step

◆ tempvariables

std::vector<jkmpTempVariable> JKQTPMathParser::tempvariables
protected

vector containing all temporary variables

◆ variables

std::map<std::string, jkmpVariable> JKQTPMathParser::variables
protected

map to manage all currently defined variables


The documentation for this class was generated from the following file: