mparlak / Flee

Fast Lightweight Expression Evaluator
607 stars 119 forks source link

ternary operation support? #100

Open cdarrigo opened 1 year ago

cdarrigo commented 1 year ago

The conditional operator ?:, also known as the ternary conditional operator, evaluates a Boolean expression and returns the result of one of the two expressions, depending on whether the Boolean expression evaluates to true or false.

Is this type of operation supported in Flee?

shawndewet commented 1 year ago

Have you actually tried such an operation and see what you get?

shawndewet commented 1 year ago

Seems it's not supported:

 [Fact]
        public void Expression_Ternary_Operator()
        {
            ExpressionContext expressionContext = new();
            expressionContext.Imports.AddType(typeof(Math));

            expressionContext.Variables["operation"] = true; //or subtract

            IGenericExpression<int> eGeneric = expressionContext.CompileGeneric<int>("operation ? 1+3 : 1-3");
            var actual = eGeneric.Evaluate();

            actual.ShouldBe(4);
        }

The above fails with the following error: Flee.Parsing.ParserLogException : unexpected character '?', on line: 1 column: 11

viklele commented 1 year ago

When I needed a ternary function, I wrote a simple custom method named IIf (boolExpression, trueValue, falseValue) that returned trueValue when boolExpression evaluated to true, and falseValue when it evaluated to false.