Answers for "Conditional expressions parser? How to manage conditions out of a C# script?"
http://answers.unity.com/questions/1632882/conditional-expressions-parser-how-to-manage-condi.html
The latest answers for the question "Conditional expressions parser? How to manage conditions out of a C# script?"Answer by Bunny83
http://answers.unity.com/answers/1632907/view.html
Just use my [logic expression parser][1] which is an extension of my [expression parser][2]. Here's a [webgl example][3].
<br>
***edit***
Here's an example usage:
using B83.LogicExpressionParser;
// [ ... ]
Parser parser = new Parser();
// parse the string expression and create an expression tree for it
LogicExpression exp = parser.Parse("wood >= 5 && stone >= 5 && haveFactory");
// Each parsed expression will use the shared expression context object which was used
// when the expression was parsed. This will hold all variables.
exp["wood"].Set(15);
exp["stone"].Set(15);
exp["haveFactory"].Set(true);
// this is usually the same as doing:
parser.ExpressionContext["wood"].Set(15);
parser.ExpressionContext["stone"].Set(15);
parser.ExpressionContext["haveFactory"].Set(true);
// To evaluate the logic expression with the current variables use GetResult()
if (exp.GetResult())
{
// do somwthing
}
<br>
Apart from logic / boolean expressions you can also parse number expressions:
NumberExpression num = parser.ParseNumber("wood * 5 + 3");
// To get the current value of the number expression use GetNumber()
double val = num.GetNumber();
Numbers are always double values. If you need / want to use float or int values you have to cast the result. Apart from setting variables to certain values you can also assign a delegate / lambda expression to a variable:
double myVariable = 42;
exp["someVar"].Set(()=>myVariable);
Now when exp is evaluated (GetResult for logic expressions, GetNumber for number expressions) it will actually execute the lambda expression whenever "someVar" is used in the expression.
<br>
Note that when parsing an expression you can provide a custom ExpressionContext for this expression only as second parameter. You can also create your own expression context object and pass it to the Parser constructir or exchange the ExpressionContext for all following parsed expressions. That way you can create multiple seperate variable contexts if needed.
<br>
You can also provide custom functions which can be used in an expression. Custom functions are registrated in the "ParsingContext". You can add a new function by using the AddFunction method. The actual method / delegate you provide has to have the following signature:
double Method(ParameterList p)
ParameterList is an array / List like structure but less restrictive. If a certain index doesn't exist it will simply return "0".
<br>
If you want to know anything else you may just have a look at the source code or just post another comment with more specific topics you want me to cover.
[1]: https://github.com/Bunny83/LSystem/blob/master/LogicExpressionParser.cs
[2]: https://wiki.unity3d.com/index.php/ExpressionParser
[3]: https://bunny83.github.io/UnityWebExamples/ExpressionParserWebGL/Sun, 19 May 2019 00:30:36 GMTBunny83