2

i want evaluate at runtime some string expression like:

((foo = true) or (bar <> 'test')) and (baz >= 1)

The string are inputted by user. The user can create a rule by coupling a property choised from a set (eg. foo, bar, baz), inputting the target value to evaluate (string, number and boolean) and choising the operator (=, <>, >, <), eg.:

| Id | Property | Operator | Value  |   Expression                                        |
-------------------------------------------------------------------------------------------
| $1 |   foo    |    =     |  true  | (foo = true)                                        |
-------------------------------------------------------------------------------------------
| $2 |    bar   |    <>    | 'test' | (bar <> 'test')                                     |
-------------------------------------------------------------------------------------------
| $3 |    baz   |    >=    |   1    | (baz >= 1)                                          |
-------------------------------------------------------------------------------------------

the single rule can be coupled and nested in child/parent rule by choosing an operator like and, or, eg.:

| Id | Property | Operator | Value  |   Expression                                        |
-------------------------------------------------------------------------------------------
| $1 |   foo    |    =     |  true  | (foo = true)                                        |
-------------------------------------------------------------------------------------------
| $2 |    bar   |    <>    | 'test' | (bar <> 'test')                                     |
-------------------------------------------------------------------------------------------
| $3 |    baz   |    >=    |   1    | (baz >= 1)                                          |
-------------------------------------------------------------------------------------------
| $4 |    $1    |    or    |  $2    | ((foo = true) or (bar <> 'test'))                   |
-------------------------------------------------------------------------------------------
| $5 |    $4    |   and    |  $3    | ((foo = true) or (bar <> 'test')) and (baz >= 1)    |
-------------------------------------------------------------------------------------------

in peseudo code, the idea is:

aExpressionEngine := TExpressionEngine.Create;
try
    // Adds to the evaluation scope all the properties with the
    // inputted value. AddToScope accept string, variant
    aExpressionEngine.AddToScope('foo', false);
    aExpressionEngine.AddToScope('bar', 'qux');
    aExpressionEngine.AddToScope('baz', 10);

    // evaluate the expression, the result is always a boolean
    Result := aExpressionEngine.eval('(((foo = true) or (bar <> ''test'')) and (baz >= 1))');
finally
    aExpressionEngine.free;
end;

in this pseudo code example, the expression to evaluate become (after replacing the properties with the scope value):

(((false = true) or ('qux' <> 'test')) and (10 >= 1)) // TRUE

by googling, i have found a bit of library for evaluate math expression, but nothing for logical condition evaluating.

Has delphi something for evaluate string expression?

16
  • 4
    These languages are interpreted, and they simply pass the condition to be evaluated through their interpreter. Delphi is a compiled language, and has no interpreter. Of course you could write an interpreter for simple expressions, but I don't think there is an existing one. It would be similar to the ones that evaluate arithmetic expressions, but it would have to be extended to evaluate string expressions. Not really a "simple operator". And ('baz' <> 0) would be invalid anyway. Commented Jul 29, 2017 at 15:12
  • 2
    Perhaps you could build such an expression using Variants. It would still be some work. It would have to parse and interpret such expressions, in other words, you would be building a simple interpreter. Commented Jul 29, 2017 at 15:17
  • 1
    Maybe TBindingExpression. Commented Jul 29, 2017 at 20:20
  • 3
    Have you checked this out ? stackoverflow.com/questions/572796/… I know the title says c#, but the accepted answer is for Delphi. Commented Jul 29, 2017 at 20:29
  • 2
    Delphi does indeed have the ability to compile and evaluate expressions- see theroadtodelphi.com/2012/07/06/… and this code by Nick Hodges - bitbucket.org/NickHodges/nickdemocode/src/842eb3fc74d7/… Commented Jul 30, 2017 at 1:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.