Reputation: 43
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.
Upvotes: 1
Views: 645
Reputation: 84620
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...
Upvotes: 1