ncalc / ncalc

NCalc is a fast and lightweight expression evaluator library for .NET, designed for flexibility and high performance. It supports a wide range of mathematical and logical operations.
https://ncalc.github.io/ncalc/
MIT License
568 stars 81 forks source link

Getting evaluated expression as string #48

Open mkalinski93 opened 2 years ago

mkalinski93 commented 2 years ago

Hello,

I just wanted to get to know if it is possible to get the evaluated expression as a string.

ValueA = 10 ValueB = 15

For example: [ValueA] + [ValueB] Results in: "10 + 15"

This can be really helpful if you want to track down the calculation to see what happened there.

petli commented 2 years ago

There is no ready-made function for it, but you can achieve this by implementing a https://github.com/ncalc/ncalc/blob/master/src/NCalc/Domain/LogicalExpressionVisitor.cs that does it. You could take most of https://github.com/ncalc/ncalc/blob/master/src/NCalc/Domain/SerializationVisitor.cs but modify Visit(Identifier) to evaluate the parameter and add that to the string instead. The parameter evaluation can be based on the corresponding code in the evaluation visitor: https://github.com/ncalc/ncalc/blob/master/src/NCalc/Domain/EvaluationVisitor.cs#L663-L700