ExpressionEval expressions samples

1. Introduction

Expression are provided to the component as a simple string. An expression content can have many shapes. It can contains parenthesis, constant values: bool, int, string or double, variables, function call. It can be a boolean expression, a comparison expression or a calculation expression. Of course you can set complex expressions with parenthesis and have a mix of boolean, comparison or calculation expressions.

// Input an expression to evaluate it
evaluator.Parse("a=b");
  

The component first parse (decode) it, after that, you have to define variables (type and value), then attach function code to function call. The last stage is to evaluate the expression. The result can be an boolean, a integer, a string or a double value, depending on the expression content.

1. Logical/boolean expression

You can provide logical expression to evaluate it. Managed operators are:

  • and
  • or
  • xor
  • Not

In English, the default language of the component, keywords are: and, or, xor, not. ExpressionEval is case insensitive for keywords, variables and function call name. You can use parenthesis or not.

Some examples of logical expressions:

  • a and b
  • (a and b)
  • (a or b) And (c or d)
  • (a xor b)
  • not(a)
  • not a
  • (not a)
  • (not(a))

These type of expressions return a boolean value.

You can change the language of expressions to french:

evaluator.SetLang(Language.Fr);

And so you can now use logical french keywords:

  • Et (And)
  • Ou (Or)
  • Oux (Xor)
  • Non (Not)

So, previous examples are now in french language:

  • a et b
  • (a et b)
  • (a ou b) et (c ou d)
  • (a oux b)
  • non(a)
  • non a
  • (non a)
  • (non(a))

2. Comparison expression

You can provide comparison expression to evaluate it. Managed operators are:

  • Equals, =
  • Different <>
  • Greater >
  • Lesser <
  • Greater or Equal >=
  • Lesser or Equal <=

Some examples of comparison expressions:

  • a > b
  • (a = b)
  • (a <> 12)
  • a >= b

These type of expressions return a boolean value.

3. Calculation expression

You can provide calculation expression to evaluate it. Managed operators are:

  • + Plus
  • – Minus
  • * Multiply
  • / Divide

You can use calculation expressions with two operands or several. These type of expressions return an integer or a double value.

Some examples of calculation expressions:

  • a+5, return an int if a is defined as an int or a double.
  • a-b, return an int or a double, depending on variables definition.
  • a*b,
  • a/10, return an int or a double.
  • (a+3)*b
  • a+b*c

If an expression has more than two operands, so more than one operator, the component will apply the default mathematical operators priority: First multiplication and division will be calculated, then addition and substraction will be calculated.

Example: 2+3*4 -> becomes for the component: 2+(3*4) so the result will be: 14 (and not 24).

4. Mixed expression type

It’s possible to mix different expressions and have inner expressions. Of course, the different sub-expressions type should match to evaluate it. Here are some examples of mixed and inner expressions:

  • (a > b) and (c<>d)
  • (a = b) or c
  • (a+5) >b

These expressions are wrong because of type of sub-expressions that are different :

  • (a and b) > 10
  • (a + b) or c

When an expression is wrong, a parse error will occurs.