DynamicExpresso icon indicating copy to clipboard operation
DynamicExpresso copied to clipboard

Intermediate resolve variables before final result

Open ole1986 opened this issue 2 years ago • 2 comments

Does there exist any intermediate option to resolve the variables by the given parameters brefore the final result? For Example I have

var parsedAmount = interpreter.Parse("x * y  + 2", parameters);

And I would like to have the result before the final result returned. E.g.

"4 * 5  + 2"

My current workaround is to do a string replacement, like so:

var expr = "x * y + 2";
var parsedAmount = interpreter.Parse(expr, parameters);;

parsedAmount.UsedParameters.ToList().ForEach(x =>
                                    {
                                        expr = expr .Replace(x.Name, x.Name + $" [{x.Value}]");
                                    });

ole1986 avatar Aug 25 '22 14:08 ole1986

Interesting idea, something like a "linking" phase ... But no, for now I don't think it is possible. As usual contributions are welcome!

davideicardi avatar Aug 26 '22 15:08 davideicardi

My idea seems pretty limited in scope, but based on this request it would be possible to add in a ParameterInterceptor style functionality. The end user would receive the ParameterExpression, its current value, and if we wanted they could return the value or a different value (this could give them the means to do some extra validation inline).

var expr = "x * y + 2";

var target = new Interpreter();
var updatedText = expr;
target.ParameterInterceptor += (Parameter expression, int? textIndex, object currentValue) =>
{
updatedText = updatedText.Replace(expression.Name, currentValue?.ToString());
return currentValue;
};
var parsedAmount = interpreter.Parse(expr, parameters);
parsedAmount.Invoke(parameters);

It's a pretty limited example that would need a LOT more code on the end user's side if parameter names were used multiple times, but it would give the flexibility needed to see exactly what is being ran.

holdenmai avatar Nov 05 '22 19:11 holdenmai