# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

NLMISC::CEvalNumExpr Class Reference

This class preforms numerical expression parsing. More...

#include <eval_num_expr.h>

Inheritance diagram for NLMISC::CEvalNumExpr:

NLGEORGES::CMyEvalNumExpr List of all members.

Public Types

enum  TReturnState {
  NoError, UnkownValue, ValueError, UnkownFunction,
  FunctionError, NumberSyntaxError, UnkownOperator, MustBeOpen,
  MustBeClose, MustBeComa, MustBeExpression, NotUnaryOperator,
  MustBeEnd, MustBeDoubleQuote, ReturnValueCount
}
 Eval return error. More...


Public Methods

virtual ~CEvalNumExpr ()
TReturnState evalExpression (const char *expression, double &result, int *errorIndex, uint32 userData=0)
 Evaluate a numerical expression. More...

const char * getErrorString (TReturnState state) const
 Get error string. More...

bool internalCheck ()

Protected Methods

virtual TReturnState evalValue (const char *value, double &result, uint32 userData)
 Eval a user defined value. More...

virtual TReturnState evalFunction (const char *funcName, double arg0, double &result)
 Eval a user defined function. More...

virtual TReturnState evalFunction (const char *funcName, double arg0, double arg1, double &result)

Private Types

enum  TToken {
  Number, Function1, Function2, String,
  Operator, Open, Close, Coma,
  End
}
 Implementation Expression tokens. More...

enum  TOperator {
  Not = 0, Tilde, Mul, Div,
  Remainder, Plus, Minus, ULeftShift,
  URightShift, SLeftShift, SRightShift, Inferior,
  InferiorEqual, Superior, SuperiorEqual, Equal,
  NotEqual, And, Or, Xor,
  LogicalAnd, LogicalOr, LogicalXor, OperatorCount,
  NotOperator, ExtOperator
}
enum  TReservedWord {
  Abs = 0, Acos, Asin, Atan,
  Atan2, Ceil, Cos, Cosh,
  Exp, Exponent, Floor, Int,
  Log, Log10, Mantissa, Max,
  Min, Pow, Rand, Round,
  Sin, Sinh, Sq, Sqrt,
  Tan, Tanh, ReservedWordCount
}
enum  { InternalStringLen = 32, InternalOperator = 4 }

Private Methods

TReturnState readDecimal (double &value)
 Read a decimal double. More...

void readIntegerNumberDecimal (double &value)
 Read an integer. More...

TReturnState getNextToken (TToken &token)
 Internal functions Get the next token. More...

TReturnState evalExpression (double &result, TToken &nextToken, uint32 userData)
 Evaluate an expression. More...


Private Attributes

TReturnState _State
 Members. More...

const char * _ExprPtr
TReservedWord _ReservedWordFound
 Reserved word. More...

char _InternalString [InternalStringLen]
 Current string. More...

std::string _InternalStlString
const char * _InternalStringPtr
double _Value
 Current value. More...

TOperator _Op
 Current operator. More...


Static Private Attributes

const TToken _ReservedWordToken [ReservedWordCount]
 Static values Char to operator array. More...

const char * _ReservedWord [ReservedWordCount]
const TOperator _OperatorArray [128]
const bool _StringChar [128]
const char * _ErrorString [ReturnValueCount]
const int _OperatorPrecedence []

Detailed Description

This class preforms numerical expression parsing.

Definition at line 37 of file eval_num_expr.h.


Member Enumeration Documentation

anonymous enum [private]
 

Enumeration values:
InternalStringLen 
InternalOperator 

Definition at line 288 of file eval_num_expr.h.

enum NLMISC::CEvalNumExpr::TOperator [private]
 

Enumeration values:
Not 
Tilde 
Mul 
Div 
Remainder 
Plus 
Minus 
ULeftShift 
URightShift 
SLeftShift 
SRightShift 
Inferior 
InferiorEqual 
Superior 
SuperiorEqual 
Equal 
NotEqual 
And 
Or 
Xor 
LogicalAnd 
LogicalOr 
LogicalXor 
OperatorCount 
NotOperator 
ExtOperator 

Definition at line 225 of file eval_num_expr.h.

Referenced by evalExpression.

enum NLMISC::CEvalNumExpr::TReservedWord [private]
 

Enumeration values:
Abs 
Acos 
Asin 
Atan 
Atan2 
Ceil 
Cos 
Cosh 
Exp 
Exponent 
Floor 
Int 
Log 
Log10 
Mantissa 
Max 
Min 
Pow 
Rand 
Round 
Sin 
Sinh 
Sq 
Sqrt 
Tan 
Tanh 
ReservedWordCount 

Definition at line 256 of file eval_num_expr.h.

Referenced by evalExpression.

enum NLMISC::CEvalNumExpr::TReturnState
 

Eval return error.

Enumeration values:
NoError 
UnkownValue 
ValueError 
UnkownFunction 
FunctionError 
NumberSyntaxError 
UnkownOperator 
MustBeOpen 
MustBeClose 
MustBeComa 
MustBeExpression 
NotUnaryOperator 
MustBeEnd 
MustBeDoubleQuote 
ReturnValueCount 

Definition at line 42 of file eval_num_expr.h.

Referenced by evalExpression, and getNextToken.

enum NLMISC::CEvalNumExpr::TToken [private]
 

Implementation Expression tokens.

Enumeration values:
Number 
Function1 
Function2 
String 
Operator 
Open 
Close 
Coma 
End 

Definition at line 211 of file eval_num_expr.h.

Referenced by evalExpression.


Constructor & Destructor Documentation

virtual NLMISC::CEvalNumExpr::~CEvalNumExpr   [inline, virtual]
 

Definition at line 40 of file eval_num_expr.h.


Member Function Documentation

CEvalNumExpr::TReturnState NLMISC::CEvalNumExpr::evalExpression double &    result,
TToken   nextToken,
uint32    userData
[private]
 

Evaluate an expression.

Definition at line 616 of file eval_num_expr.cpp.

References _InternalStringPtr, _Op, _OperatorPrecedence, _ReservedWordFound, _Value, Abs, Acos, And, arg1, Asin, Atan, Atan2, Ceil, Close, Coma, Cos, Cosh, Div, Equal, evalExpression, evalFunction, evalValue, Exp, Exponent, Floor, Function1, Function2, getNextToken, index, Inferior, InferiorEqual, Int, InternalOperator, InternalStringLen, Log, Log10, LogicalAnd, LogicalOr, LogicalXor, Mantissa, Max, Min, Minus, Mul, MustBeClose, MustBeComa, MustBeExpression, MustBeOpen, nlassert, nlstop, NoError, Not, NotEqual, NotOperator, NotUnaryOperator, Number, Open, Operator, Or, Plus, Pow, Rand, Remainder, Round, Sin, Sinh, SLeftShift, Sq, Sqrt, SRightShift, String, Superior, SuperiorEqual, Tan, Tanh, Tilde, TOperator, TReservedWord, TReturnState, TToken, ULeftShift, URightShift, value, and Xor.

CEvalNumExpr::TReturnState NLMISC::CEvalNumExpr::evalExpression const char *    expression,
double &    result,
int *    errorIndex,
uint32    userData = 0
 

Evaluate a numerical expression.

Doesn't allocate heap memory for common complexity expression.

Parameters:
expression  is an expression string. See the expression grammar.
result  is filled with the result if the function returns "NoError".
errorIndex  is a pointer on an integer value filled with the index of the parsing error in the input string if function doesn't return "NoError". This value can be NULL.
userData  is a user data used by user eval function
Returns:
NoError if the expression has been parsed. Result is filled with the evaluated value.
This expression must follow the following grammar with the following evaluation priority:

expression: '-' expression | '!' expression // Returns true if a equal false, else false (logical not) | '~' expression // Returns ~ round(a) (bitwise not) | '(' expression ')' | expression operator expression | function1 '(' expression ')' | function2 '(' expression ',' expression ')' | number | constant | string // User defined value, evaluated by the evalValue() callback

operator: '*' // Calculates (a * b) | '/' // Calculates (a / b) | '%' // Calculates the remainder of (a / b) | '+' // Calculates (a + b) | '-' // Calculates (a - b) | '<<' // Returns round(a) left 32 bits unsigned shift by round(b) | '>>' // Returns round(a) right 32 bits unsigned shift by round(b) | '<-' // Returns round(a) left 32 bits signed shift by round(b) | '->' // Returns round(a) right 32 bits signed shift by round(b) | '<' // Returns true if a is strictly smaller than b | '<=' // Returns true if a is smaller or equal than b | '>' // Returns true if a is strictly bigger than b | '>=' // Returns true if a is bigger or equal than b | '==' // Returns true if a equal b, else returns false (warning, performs a floating point comparison) | '!=' // Returns false if a equal b, else returns true (warning, performs a floating point comparison) | '&' // Returns round(a) & round(b) over 32 bits | '|' // Returns round(a) | round(b) over 32 bits | '^' // Returns round(a) ^ round(b) over 32 bits | '&&' // Returns true if a equal true and b equal true else returns false | '||' // Returns true if a equal true or b equal true else returns false | '^^' // Returns true if a equal true and b equal false, or, a equal false and b equal 1.0, else returns false

function1: abs // Calculates the absolute value | acos // Calculates the arccosine | asin // Calculates the arcsine | atan // Calculates the arctangent | ceil // Calculates the ceiling of a value ( ceil(-1.1) = -1, ceil(1.1) = 2 ) | cos // Calculates the cosine | cosh // Calculates the hyperbolic cosine | exp // Calculates the exponential | exponent // Calculates the exponent of a floating point value | floor // Calculates the floor of a value ( floor(-1.1) = -2, floor(1.1) = 1 ) | int // Calculates the C style integer value ( int(-1.6) = -1, int(1.6) = 1 ) | log // Calculates logarithms | log10 // Calculates base-10 logarithms | mantissa // Calculates the mantissa of a floating point value | round // Calculates the nearest integer value ( round(-1.6) = -2, round(1.1) = 1 ) | sin // Calculate sines | sinh // Calculate hyperbolic sines | sq // Calculates the square | sqrt // Calculates the square root | tan // Calculates the tangent | tanh // Calculates the hyperbolic tangent | string // User defined one arg function, evaluated by the evalfunction() callback

function2: max // Returns the larger of two values | min // Returns the smaller of two values | atan2 // Calculates the arctangent of arg0/arg1 | pow // Calculates a raised at the power of b | rand // Calculates a pseudo random value (arg0 <= randomValue < arg1) | string // User defined two args function, evaluated by the evalfunction() callback

number: [0-9]+ // Unsigned decimal integer | "0x"[0-9a-fA-F]+ // Unsigned hexadecimal integer | "0"[0-7]+ // Unsigned octal integer | [0-9]*.[0-9]+ // Unsigned floating point value | [0-9]*.[0-9]+[eE]-?[0-9]*.[0-9]+ // Unsigned floating point value with signed exponent

constant: e // 2.7182818284590452353602874713527 | pi // 3.1415926535897932384626433832795

string: [^ 0-9\t
/\*-+=<>&|\^!%~\(\)\.,\"][^ \t
/\*-+=<>&|\^!%~\(\)\.,\"]* // Labels ($foo, foo01, _001) | \"[]+\" // All kind of labels between double quotes "123456" "foo.foo[12]"

Operator precedence:

0 - unary operator (-, ~, !) 1 - *, /, % 2 - +, -, 3 - <<, >>, <-, -> 4 - <, <=, >, >= 5 - ==, != 6 - & 7 - | 8 - ^ 9 - && 10 - || 11 - ^^

Definition at line 586 of file eval_num_expr.cpp.

References _ExprPtr, End, MustBeEnd, NoError, TReturnState, and TToken.

Referenced by evalExpression.

CEvalNumExpr::TReturnState NLMISC::CEvalNumExpr::evalFunction const char *    funcName,
double    arg0,
double    arg1,
double &    result
[protected, virtual]
 

Definition at line 1190 of file eval_num_expr.cpp.

References arg1, and UnkownFunction.

CEvalNumExpr::TReturnState NLMISC::CEvalNumExpr::evalFunction const char *    funcName,
double    arg0,
double &    result
[protected, virtual]
 

Eval a user defined function.

Default implementation returns UnkownFunction. The user can parse the function name and fill the result double and return NoError, UnkownFunction or FunctionError.

To convert double argu in boolean argu, use (round (value) != 0.0) ? true : false

Parameters:
funcName  is the name of the function to evaluate.
arg0  is the first parameter passed to the function.
arg1  is the second parameter passed to the function.
result  is the result to fill if the value has been succesfully parsed.
Returns:
UnkownFunction if the function doesn't exist, FunctionError if the function evaluation failed, NoError if it has been parsed.

Definition at line 1183 of file eval_num_expr.cpp.

References UnkownFunction.

Referenced by evalExpression.

CEvalNumExpr::TReturnState NLMISC::CEvalNumExpr::evalValue const char *    value,
double &    result,
uint32    userData
[protected, virtual]
 

Eval a user defined value.

Default implementation returns UnkownValue. The user can parse the value and fill the result double and return NoError, UnkownValue or ValueError.

Parameters:
value  is the value to parse.
result  is the result to fill if the value has been succesfully parsed.
userData  is a user data used by user eval function.
Returns:
UnkownValue if the value is not known, ValueError is the value evaluation failed or NoError if it has been parsed.

Reimplemented in NLGEORGES::CMyEvalNumExpr.

Definition at line 1176 of file eval_num_expr.cpp.

References UnkownValue, and value.

Referenced by evalExpression.

const char * NLMISC::CEvalNumExpr::getErrorString TReturnState    state const
 

Get error string.

Definition at line 1281 of file eval_num_expr.cpp.

References _ErrorString.

CEvalNumExpr::TReturnState NLMISC::CEvalNumExpr::getNextToken TToken   token [private]
 

Internal functions Get the next token.

Definition at line 96 of file eval_num_expr.cpp.

References _ExprPtr, _InternalStlString, _InternalString, _InternalStringPtr, _Op, _OperatorArray, _ReservedWord, _ReservedWordFound, _ReservedWordToken, _StringChar, _Value, And, Close, Coma, End, Equal, ExtOperator, Inferior, InferiorEqual, InternalStringLen, LogicalAnd, LogicalOr, LogicalXor, Minus, MustBeDoubleQuote, NoError, Not, NotEqual, NotOperator, Number, NumberSyntaxError, Open, Operator, Or, readDecimal, ReservedWordCount, SLeftShift, SRightShift, String, Superior, SuperiorEqual, TReturnState, ULeftShift, UnkownOperator, URightShift, and Xor.

Referenced by evalExpression.

bool NLMISC::CEvalNumExpr::internalCheck  
 

Definition at line 1120 of file eval_num_expr.cpp.

References _ReservedWord, nlassert, and ReservedWordCount.

CEvalNumExpr::TReturnState NLMISC::CEvalNumExpr::readDecimal double &    value [private]
 

Read a decimal double.

Definition at line 37 of file eval_num_expr.cpp.

References _ExprPtr, NoError, NumberSyntaxError, readIntegerNumberDecimal, and value.

Referenced by getNextToken.

void NLMISC::CEvalNumExpr::readIntegerNumberDecimal double &    value [private]
 

Read an integer.

Definition at line 65 of file eval_num_expr.cpp.

References _ExprPtr, and value.

Referenced by readDecimal.


Member Data Documentation

const char * NLMISC::CEvalNumExpr::_ErrorString [static, private]
 

Initial value:

{
        "No error",
        "Unkown value",
        "Error during user defined value evaluation",
        "Unkown function",
        "Error during user defined function evaluation",
        "Syntax error in a number expression",
        "Unkown operator",
        "Should be a open parentesis",
        "Should be a close parentesis",
        "Should be a coma character",
        "Should be an expression",
        "Should not be an unary operator",
        "Should be the end of the expression",
        "Should be a double quote",
}

Definition at line 1261 of file eval_num_expr.cpp.

Referenced by getErrorString.

const char* NLMISC::CEvalNumExpr::_ExprPtr [private]
 

Definition at line 296 of file eval_num_expr.h.

Referenced by evalExpression, getNextToken, readDecimal, and readIntegerNumberDecimal.

std::string NLMISC::CEvalNumExpr::_InternalStlString [private]
 

Definition at line 317 of file eval_num_expr.h.

Referenced by getNextToken.

char NLMISC::CEvalNumExpr::_InternalString[InternalStringLen] [private]
 

Current string.

Definition at line 316 of file eval_num_expr.h.

Referenced by getNextToken.

const char* NLMISC::CEvalNumExpr::_InternalStringPtr [private]
 

Definition at line 318 of file eval_num_expr.h.

Referenced by evalExpression, and getNextToken.

TOperator NLMISC::CEvalNumExpr::_Op [private]
 

Current operator.

Definition at line 324 of file eval_num_expr.h.

Referenced by evalExpression, and getNextToken.

const CEvalNumExpr::TOperator NLMISC::CEvalNumExpr::_OperatorArray [static, private]
 

Initial value:

 
{
        NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator,
        NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator,
        NotOperator, ExtOperator, NotOperator, NotOperator, NotOperator, Remainder,   ExtOperator, NotOperator, NotOperator, NotOperator, Mul,             Plus,                NotOperator, ExtOperator, NotOperator, Div,
        NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, ExtOperator, ExtOperator, ExtOperator, NotOperator,
        NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator,
        NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, ExtOperator, NotOperator,
        NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator,
        NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, NotOperator, ExtOperator, NotOperator, Tilde,       NotOperator,
}

Definition at line 1148 of file eval_num_expr.cpp.

Referenced by getNextToken.

const int NLMISC::CEvalNumExpr::_OperatorPrecedence [static, private]
 

Initial value:

{
        0,      
        0,      
        1,      
        1,      
        1,      
        2,      
        2,      
        3,      
        3,      
        3,      
        3,      
        4,      
        4,      
        4,      
        4,      
        5,      
        5,      
        6,      
        7,      
        8,      
        9,      
        10,     
        11,     
        0xffffffff,     
        20,     
}

Definition at line 1288 of file eval_num_expr.cpp.

Referenced by evalExpression.

const char * NLMISC::CEvalNumExpr::_ReservedWord [static, private]
 

Initial value:

{
        "abs", 
        "acos", 
        "asin", 
        "atan", 
        "atan2", 
        "ceil", 
        "cos",  
        "cosh", 
        "exp", 
        "exponent", 
        "floor", 
        "int", 
        "log", 
        "log10", 
        "mantissa", 
        "max", 
        "min", 
        "pow", 
        "rand", 
        "round", 
        "sin", 
        "sinh", 
        "sq", 
        "sqrt", 
        "tan", 
        "tanh", 
}

Definition at line 1197 of file eval_num_expr.cpp.

Referenced by getNextToken, and internalCheck.

TReservedWord NLMISC::CEvalNumExpr::_ReservedWordFound [private]
 

Reserved word.

Definition at line 313 of file eval_num_expr.h.

Referenced by evalExpression, and getNextToken.

const CEvalNumExpr::TToken NLMISC::CEvalNumExpr::_ReservedWordToken [static, private]
 

Initial value:

Static values Char to operator array.

Definition at line 1229 of file eval_num_expr.cpp.

Referenced by getNextToken.

TReturnState NLMISC::CEvalNumExpr::_State [private]
 

Members.

Definition at line 295 of file eval_num_expr.h.

const bool NLMISC::CEvalNumExpr::_StringChar [static, private]
 

Initial value:

 
{
        false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
        false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
        false, false, true,  true,  true,  false, false, true,  false, false, false, false, false, false, false, false,
        true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  false, false, false, true,
        true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,
        true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  false,  true,
        true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,
        true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  true,  false, true,  false, true,
}

Definition at line 1162 of file eval_num_expr.cpp.

Referenced by getNextToken.

double NLMISC::CEvalNumExpr::_Value [private]
 

Current value.

Definition at line 321 of file eval_num_expr.h.

Referenced by evalExpression, and getNextToken.


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