Delphi: interpreter for string expression with operators

I want to evaluate at runtime some string expression like:

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

      

The string is entered by the user. The user can set up a rule by associating property selected from the set (e.g., foo

, bar

, baz

), introducing a target value for the evaluation ( string

, number

, and boolean

) and the choice of the operator ( =

, <>

, >

, <

), for example:

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

      

One rule may be connected and embedded in the rule child / parent, selecting the type of operator and

, or

such as:

| 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 the peseudo code, the idea is this:

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 example pseudocode, the expression to evaluate becomes (after replacing the properties with the scope value):

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

      

by googling, I found a little library for evaluating a math expression, but nothing for evaluating a boolean state.

Does delphi have something to evaluate a string expression?

+3


source to share





All Articles