Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intermediate resolve variables before final result #254

Open
ole1986 opened this issue Aug 25, 2022 · 2 comments
Open

Intermediate resolve variables before final result #254

ole1986 opened this issue Aug 25, 2022 · 2 comments

Comments

@ole1986
Copy link

ole1986 commented Aug 25, 2022

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}]");
                                    });

@davideicardi
Copy link
Member

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

@holdenmai
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants