1

In Delphi XE I have a routine that breaks down some complicated formulas to a binary formula result like this:

(1&((1|0)|1)&1)

Since my application is on SQL Server I can easily do a SELECT (1&((1|0)|1)&1) As Result and SQL Server will evaluate the logic to give me a 1 or 0 result.

I cannot seem to find a native Delphi way of taking a string representation of a binary formula and evaluating it.

I looked into the Live Bindings Evaluators added to XE but any attempt at the and/or operators throws parser errors so I believe that to be more of a mathematical process.

I also looked at a few parsers but none really seemed to jump out at me.

So how can I do something similar to:

iSomeInt := Evaluate('(1&((1|0)|1)&1)');

BTW.. I can totally rework the formula and use and/or instead of &|.. that was just to make it work with SQL Server.

3
  • Take a look here: blogs.embarcadero.com/ao/2012/04/13/39249 Commented Aug 14, 2013 at 18:57
  • JCL TEvaluator looks good. refer to this previous SO question stackoverflow.com/questions/13592136/… Commented Aug 15, 2013 at 3:39
  • JCL expression evaluator is indeed good. Although it handles decimal separators badly, and lacks a power operator. Both of these omissions I have fixed in my own branch, but can I persuade any of the JEDI devs to do the necessary? Can I heck! Frustrating when open source projects die. And there seems no obvious way to bring it back to life. Commented Aug 15, 2013 at 6:17

1 Answer 1

1

The problem isn't that it's "a mathematical process" so much as that the LiveBindings parser expects your mathematical process to be expressed with Pascal operators, but & and | are C operators.

Try something like this, assuming you have a function called LiveBindingsEval which evaluates a string with the LiveBindings parser:

function PreprocessExpression(const expr: string): string;
begin
   result := StringReplace(expr, '|', ' or ', [rfReplaceAll]);
   result := StringReplace(result, '&', ' and ', [rfReplaceAll]);
end;

iSomeInt := LiveBindingsEval(PreprocessExpression('(1&((1|0)|1)&1)'));

You may need to improve on this a little, but this should be enough to get you started on the right path...

Sign up to request clarification or add additional context in comments.

1 Comment

Mason ... I actually used the Delphi and or with the Evaluator and it didn't care for the operators at all.. it wasn't really obvious but I mentioned I was only using the & and | for SQL Server compatibility.I'll go back and give it another shot though.

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.