31#include "jkqtmath/jkqtmath_imexport.h"
33#ifndef JKQTPMATHPARSER_H
34#define JKQTPMATHPARSER_H
607 virtual const char*
what() const noexcept override;
611 typedef
void (*jkmpexceptionf)(std::
string);
615 void jkmpError(const std::
string& st);
619 jkmpexceptionf jkmathparser_exception_function;
623 void setException_function(jkmpexceptionf exception_function);
626 void resetException_function();
732 std::map<std::string, jkmpFunctionDescriptor>
functions;
764 void addFunction(
const std::string& name, jkmpEvaluateFunc function);
872 std::string
getArgCVParam(
const std::string& name,
const std::string& defaultResult);
This class represents a binary arithmetic operation: add (+), subtract (-), multiply (*),...
Definition jkqtpmathparser.h:346
virtual jkmpResult evaluate() override
evaluate this node
jkmpBinaryArithmeticNode(char op, jkmpNode *l, jkmpNode *r, JKQTPMathParser *p, jkmpNode *par)
constructor for a jkmpBinaryArithmeticNode
jkmpNode * left
Definition jkqtpmathparser.h:348
char operation
Definition jkqtpmathparser.h:349
virtual ~jkmpBinaryArithmeticNode() override
standard destructor, also destroy the children (recursively)
This class represents a binary boolean operation: and, or, xor, nor, nand.
Definition jkqtpmathparser.h:378
jkmpBinaryBoolNode(jkmpLOP op, jkmpNode *l, jkmpNode *r, JKQTPMathParser *p, jkmpNode *par)
constructor for a jkmpBinaryBoolNode
virtual ~jkmpBinaryBoolNode() override
standard destructor, also destroy the children (recursively)
jkmpNode * left
Definition jkqtpmathparser.h:380
virtual jkmpResult evaluate() override
evaluate this node
jkmpLOP operation
Definition jkqtpmathparser.h:381
This class represents a binary compare operation: !=, ==, >=, <=, >, <
Definition jkqtpmathparser.h:411
jkmpCOMP operation
Definition jkqtpmathparser.h:414
virtual jkmpResult evaluate() override
evaluate this node
jkmpNode * left
Definition jkqtpmathparser.h:413
jkmpCompareNode(jkmpCOMP op, jkmpNode *l, jkmpNode *r, JKQTPMathParser *p, jkmpNode *par)
constructor for a jkmpCompareNode
virtual ~jkmpCompareNode() override
standard destructor, also destroy the children (recursively)
This class represents a number, a string contant or a boolean contant (true / false )
Definition jkqtpmathparser.h:477
jkmpResult data
Definition jkqtpmathparser.h:479
jkmpConstantNode(jkmpResult d, JKQTPMathParser *p, jkmpNode *par)
constructor for a jkmpConstantNode
virtual jkmpResult evaluate() override
evaluate this node
error handling: exceptions of the type of this class will be thrown if an error occurs
Definition jkqtpmathparser.h:589
std::string getMessage() const
returns the assigned errormessage
jkmpException()
class constructors
std::string errormessage
the error message
Definition jkqtpmathparser.h:592
virtual ~jkmpException() override
class destructors
jkmpException(const std::string &msg)
constructor with supplied error message
virtual const char * what() const noexcept override
returns the error description as C string
This class represents an arbitrary function.
Definition jkqtpmathparser.h:516
virtual jkmpResult evaluate() override
evaluate this node
std::string fun
Definition jkqtpmathparser.h:518
virtual ~jkmpFunctionNode() override
standard destructor, also destroy the children (recursively)
jkmpNode ** child
Definition jkqtpmathparser.h:519
jkmpEvaluateFunc function
Definition jkqtpmathparser.h:521
jkmpFunctionNode(const std::string &name, jkmpNode **c, unsigned char num, JKQTPMathParser *p, jkmpNode *par)
constructor for a jkmpFunctionNode
unsigned char n
Definition jkqtpmathparser.h:520
This class is the abstract base class for nodes. All allowed node types must inherit from jkmpNode.
Definition jkqtpmathparser.h:316
JKQTPMathParser * parser
points to the parser object that is used to evaluate this node
Definition jkqtpmathparser.h:318
jkmpNode * getParent()
returns a pointer to the parent node
void setParent(jkmpNode *par)
sets the parent node
jkmpNode * parent
points to the parent node
Definition jkqtpmathparser.h:319
virtual ~jkmpNode()
virtual class destructor
void setParser(JKQTPMathParser *mp)
set the JKQTPMathParser
virtual jkmpResult evaluate()=0
evaluate this node
jkmpNode(JKQTPMathParser *parser_=nullptr, jkmpNode *parent_=nullptr)
JKQTPMathParser * getParser()
return a pointer to the JKQTPMathParser
This class represents a list of jkmpNode.
Definition jkqtpmathparser.h:544
virtual jkmpResult evaluate() override
evaluate the node
virtual ~jkmpNodeList() override
standard destructor, also destroy the children (recursively)
void add(jkmpNode *n)
add a jkmpNode n to the list
int getCount()
get the number of nodes in the list
std::vector< jkmpNode * > list
Definition jkqtpmathparser.h:546
jkmpNodeList(JKQTPMathParser *p)
constructor for a jkmpNodeList
This class represents a unary operations: ! (bool negation), - (arithmetic negation)
Definition jkqtpmathparser.h:433
virtual ~jkmpUnaryNode() override
standard destructor, also destroy the children (recursively)
jkmpNode * child
Definition jkqtpmathparser.h:435
char operation
Definition jkqtpmathparser.h:436
jkmpUnaryNode(char op, jkmpNode *c, JKQTPMathParser *p, jkmpNode *par)
constructor for a jkmpUnaryNode
virtual jkmpResult evaluate() override
evaluate this node
This class represents a variable assignment (a = expression)
Definition jkqtpmathparser.h:455
virtual ~jkmpVariableAssignNode() override
standard destructor, also destroy the children (recursively)
std::string variable
Definition jkqtpmathparser.h:458
jkmpVariableAssignNode(const std::string &var, jkmpNode *c, JKQTPMathParser *p, jkmpNode *par)
constructor for a jkmpVariableAssignNode
virtual jkmpResult evaluate() override
evaluate this node
jkmpNode * child
Definition jkqtpmathparser.h:457
This class represents a variable.
Definition jkqtpmathparser.h:493
std::string var
Definition jkqtpmathparser.h:495
virtual jkmpResult evaluate() override
evaluate this node
jkmpVariableNode(const std::string &name, JKQTPMathParser *p, jkmpNode *par)
constructor for a jkmpVariableNode
A simple function parser to parse (build memory tree representation) and evaluate simple mathematical...
Definition jkqtpmathparser.h:220
bool variableExists(const std::string &name)
tests whether a variable exists
jkmpResult evaluate(const std::string &prog)
evaluate the given expression
jkmpLOP
internal names for logic operations
Definition jkqtpmathparser.h:368
std::istringstream * program
this stream is used to read in the program. An object is created and assigned (and destroyed) by the ...
Definition jkqtpmathparser.h:696
void addVariableBoolean(const std::string &name, bool *v)
register a new external variable of type boolean
jkmpVariable getVariableDef(const std::string &name)
returns the defining structure of the given variable
void printVariables()
prints a list of all registered variables
void deleteVariable(const std::string &name)
delete the specified variabale and releases its internal memory.
void addVariableBoolean(const std::string &name, bool v)
register a new internal variable of type boolean
void addVariable(const std::string &name, jkmpResult result)
register a new internal variable of type boolean
std::string currenttokentostring()
return the current token as human-readable string
virtual ~JKQTPMathParser()
class destructor
jkmpNode * compExpression(bool get)
recognizes an compExpression while parsing. If get ist true, this function first retrieves a new toke...
void addStandardFunctions()
registers standard functions
jkmpResultType
possible result types
Definition jkqtpmathparser.h:227
@ 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
std::string readDelim(char delimiter)
return a delimited text, i.e. extract the texte between the delimiters "</code> in: of <code>"Hallo!...
jkmpNode * logicalExpression(bool get)
recognizes a logicalExpression while parsing. If get ist true, this function first retrieves a new to...
jkmpEvaluateFunc getFunctionDef(const std::string &name)
returns the defining structure of the given function
std::vector< std::pair< std::string, jkmpVariable > > getVariables()
returns all registered variables
char ** argv
storage for program arguments, used by the standard functions cmdparam and argv
Definition jkqtpmathparser.h:747
void setVariable(const std::string &name, jkmpResult value)
set the defining struct of the given variable
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,...
jkmpCOMP
jkmpCOMPdefs internal names for compare operations
Definition jkqtpmathparser.h:401
bool functionExists(const std::string &name)
tests whether a function exists
jkmpTokenType
the possible tokens that can be recognized by the tokenizer in JKQTPMathParser::getToken()
Definition jkqtpmathparser.h:634
@ PRINT
a semicolon ';'
Definition jkqtpmathparser.h:636
@ LOGIC_AND
a logical AND operator '&&' / 'and'
Definition jkqtpmathparser.h:652
@ FACTORIAL_LOGIC_NOT
a factorial operator or a logical NOT '!'
Definition jkqtpmathparser.h:650
@ END
end token
Definition jkqtpmathparser.h:635
@ MUL
a multiplication operator '*'
Definition jkqtpmathparser.h:643
@ PARAMETER_DIV
a comma ',' between two function parameters
Definition jkqtpmathparser.h:637
@ LOGIC_NAND
a logical NAND operator 'nand'
Definition jkqtpmathparser.h:656
@ LOGIC_TRUE
'true'
Definition jkqtpmathparser.h:657
@ RBRACKET
right brackets ')'
Definition jkqtpmathparser.h:648
@ LOGIC_FALSE
'false'
Definition jkqtpmathparser.h:658
@ COMP_EQUALT
equals operation '=='
Definition jkqtpmathparser.h:659
@ LOGIC_NOT
a logical NOT '!' / 'not'
Definition jkqtpmathparser.h:651
@ NAME
a name (consisting of characters) of a variable or function
Definition jkqtpmathparser.h:639
@ LOGIC_OR
a logical OR operator '||' / 'or'
Definition jkqtpmathparser.h:653
@ LBRACKET
left brackets '('
Definition jkqtpmathparser.h:647
@ COMP_SEQUAL
smaller than or equal operation '<='
Definition jkqtpmathparser.h:664
@ COMP_GEQUAL
greater than or equal operation '>='
Definition jkqtpmathparser.h:663
@ ASSIGN
a variable assignment =
Definition jkqtpmathparser.h:646
@ POWER
a power operator '^'
Definition jkqtpmathparser.h:649
@ PLUS
a plus operator '+'
Definition jkqtpmathparser.h:641
@ COMP_UNEQUAL
unequal operation '!='
Definition jkqtpmathparser.h:660
@ COMP_GREATER
greater than operation '>'
Definition jkqtpmathparser.h:661
@ MINUS
a minus operator '-'
Definition jkqtpmathparser.h:642
@ LOGIC_XOR
a logical XOR operator 'xor'
Definition jkqtpmathparser.h:654
@ COMP_SMALLER
smaller than operation '<'
Definition jkqtpmathparser.h:662
@ LOGIC_NOR
a logical NOR operator 'nor'
Definition jkqtpmathparser.h:655
@ NUMBER
a number in scientific notation
Definition jkqtpmathparser.h:640
@ MODULO
a modulo operator ''
Definition jkqtpmathparser.h:645
@ DIV
a division operator '/'
Definition jkqtpmathparser.h:644
@ STRING_DELIM
a string delimiter ' or "
Definition jkqtpmathparser.h:638
void addTempVariable(const std::string &name, jkmpResult value)
adds a temporary variable
jkmpNode * mathTerm(bool get)
recognizes a term while parsing. If get ist true, this function first retrieves a new token by callin...
void addVariableDouble(const std::string &name, double *v)
register a new external variable of type double
int argc
storage for program argument cound, used by the standard functions cmdparam and argv
Definition jkqtpmathparser.h:745
jkmpResult getVariableOrInvalid(const std::string &name)
returns the value of the given variable
double NumberValue
the string value of the current token (when applicable) during the parsing step
Definition jkqtpmathparser.h:692
jkmpNode * parse(const std::string &prog)
parses the given expression
jkmpTokenType getToken()
Tokenizer: extract the next token from the input.
void addFunction(const std::string &name, jkmpEvaluateFunc function)
register a new function
bool tempvariableExists(const std::string &name)
tests whether a temporary variable exists
JKQTPMathParser()
class constructor
void setVariableDouble(const std::string &name, double value)
set the defining struct of the given variable
void addVariableString(const std::string &name, const std::string &v)
register a new internal variable of type string
jkmpNode * logicalTerm(bool get)
recognizes a logicalTerm while parsing. If get ist true, this function first retrieves a new token by...
jkmpResult evaluateFunction(const std::string &name, jkmpResult *params, unsigned char n)
evaluates a registered function
std::vector< jkmpTempVariable > tempvariables
vector containing all temporary variables
Definition jkqtpmathparser.h:726
jkmpResult getVariable(const std::string &name)
returns the value of the given variable
void addVariableDouble(const std::string &name, double v)
register a new internal variable of type double
jkmpNode * primary(bool get)
recognizes a primary while parsing. If get ist true, this function first retrieves a new token by cal...
void addStandardVariables()
registers standard variables
std::map< std::string, jkmpFunctionDescriptor > functions
map to manage all currently rtegistered functions
Definition jkqtpmathparser.h:732
void addVariableString(const std::string &name, std::string *v)
register a new external variable of type string
jkmpTokenType CurrentToken
the current token while parsing a string
Definition jkqtpmathparser.h:686
jkmpNode * mathExpression(bool get)
recognizes a mathExpression while parsing. If get ist true, this function first retrieves a new token...
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...
std::string StringValue
the string value of the current token (when applicable) during the parsing step
Definition jkqtpmathparser.h:689
void setArgCV(int argc, char **argv)
store programs command-line arguments, so they are available in the parser, used by the standard func...
std::map< std::string, jkmpVariable > variables
map to manage all currently defined variables
Definition jkqtpmathparser.h:729
std::string tokentostring(jkmpTokenType token)
return the given token as human-readable string
#define jkqtmath_LIB_EXPORT
Definition jkqtmath_imexport.h:87
description of a user registered function
Definition jkqtpmathparser.h:301
jkmpFunctionDescriptor(jkmpEvaluateFunc function_=nullptr)
jkmpEvaluateFunc function
a pointer to the function implementation
Definition jkqtpmathparser.h:303
std::string name
name of the function
Definition jkqtpmathparser.h:304
result of any expression
Definition jkqtpmathparser.h:234
std::string toString()
convert the value this struct representens into a std::string
std::string str
contains result if type==jkmpString
Definition jkqtpmathparser.h:239
jkmpResultType type
type of the result
Definition jkqtpmathparser.h:238
std::string toTypeString()
convert the value this struct representens into a std::string and adds the name of the datatype in [....
bool isValid
Definition jkqtpmathparser.h:237
bool boolean
contains result if type==jkmpBool
Definition jkqtpmathparser.h:241
double num
contains result if type==jkmpDouble
Definition jkqtpmathparser.h:240
This struct is for managing temporary variables. It is generally like jkmpVariable.
Definition jkqtpmathparser.h:263
std::string name
name of the variable
Definition jkqtpmathparser.h:265
bool * boolean
this points to the variable data if type==jkmpBool
Definition jkqtpmathparser.h:270
bool internal
this is an internal variable
Definition jkqtpmathparser.h:267
jkmpResultType type
type of the variable
Definition jkqtpmathparser.h:266
double * num
this points to the variable data if type==jkmpDouble
Definition jkqtpmathparser.h:269
std::string * str
this points to the variable data if type==jkmpString
Definition jkqtpmathparser.h:268
This struct is for managing variables. Unlike jkmpResult this struct only contains pointers to the da...
Definition jkqtpmathparser.h:253
bool * boolean
this points to the variable data if type==jkmpBool
Definition jkqtpmathparser.h:259
double * num
this points to the variable data if type==jkmpDouble
Definition jkqtpmathparser.h:258
jkmpResultType type
type of the variable
Definition jkqtpmathparser.h:255
bool internal
this is an internal variable
Definition jkqtpmathparser.h:256
std::string * str
this points to the variable data if type==jkmpString
Definition jkqtpmathparser.h:257