I've got a logical string read in from a configuration file written by someone else that contains expressions such as the following:
(VALUE_1)OR((NOT(VALUE_2))AND(NOT(VALUE_3)))
However, I'm a little stumped as to where to start parsing this and comparing the values of the variables that I have stored as the same string name elsewhere. Am I correct in thinking LambdaExpression is the thing that needs to be used? Does the string need splitting in some way and to be analysed as the constituent parts rather than as a whole?
EDIT:
It seems as though Flee does what I need it to do, I can define the names of the VALUE_x as true or false before evaluating the expression using that library.
Typical approach for evaluating string expressions in C# is building expression tree and compiling it into delegate (this job is covered by .NET framework). In most cases dynamic linq library is recommended but it has several drawbacks: it is not supported as reusable library (actually it is just illustration of LINQ capabilities published by Scott Gu) and it can evaluate only strongly typed expressions which is bad in most real life applications.
I suggest better alternative: lambda expressions parser from NReco Commons (this is free and open source library). It also builds expression tree but uses quite different approach to expression parsing and evaluating it as expression tree: it performs all types harmonization and invocations at runtime (like dynamic languages), supports property and methods calls, arrays construction and conditional operator. Some examples:
var lambdaParser = new NReco.LambdaParser();
var varContext = new Dictionary<string,object>();
varContext["pi"] = 3.14M;
varContext["one"] = 1M;
varContext["two"] = 2M;
varContext["test"] = "test";
varContext["arr1"] = new double[] { 1.5, 2.5 };
Console.WriteLine( lambdaParser.Eval("pi>one && 0<one ? (1+8)/3+1*two : 0", varContext) ); // --> 5
Console.WriteLine( lambdaParser.Eval(" arr1[0]+arr1[1] ", varContext) ); // -> 4
Console.WriteLine( lambdaParser.Eval(" (new[]{1,2})[1] ", varContext) ); // -> 2
(more examples and documentation could be found at NReco Commons library page)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With