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
eval delphi


source to share


No one has answered this question yet

See similar questions:

13
The best algorithm for evaluating a mathematical expression?
0
Building and Evaluating Expressions Using Delphi RTTI

or similar:

243
Evaluate expression given as string
6
Are there any known issues with s> = and <= and the eval function in JS?
five
Why is delphi BoolToStr true represented as -1
4
C # resolve "(true and true) or (true or false)"
3
How can I get the underlying raw Variant value for a Delphi 6 indexed property that references this Variant?
1
The default boolean in an array of records is Delphi
1
A faster method for evaluating a boolean expression as a string in Python
1
parse string with boolean condition
0
Hack is required for undefined == true in eval
0
VB.NET Boolean Expression Evaluator



All Articles
Loading...
X
Show
Funny
Dev
Pics