コード例 #1
0
 public override object VisitConditional(BoundConditionalEx x)
 {
     return(x.Update(
                (BoundExpression)Accept(x.Condition),
                (BoundExpression)Accept(x.IfTrue),
                (BoundExpression)Accept(x.IfFalse)));
 }
コード例 #2
0
        public override object VisitConditional(BoundConditionalEx x)
        {
            x = (BoundConditionalEx)base.VisitConditional(x);

            if (x.IfTrue != null) // otherwise it is (A ?: B) operator
            {
                if (x.Condition.ConstantValue.TryConvertToBool(out var condVal))
                {
                    TransformationCount++;
                    return((condVal ? x.IfTrue : x.IfFalse).WithAccess(x));
                }

                if (x.IfTrue.ConstantValue.IsBool(out bool trueVal) &&
                    x.IfFalse.ConstantValue.IsBool(out bool falseVal))
                {
                    if (trueVal && !falseVal)
                    {
                        // A ? true : false => (bool)A
                        TransformationCount++;
                        return(new BoundConversionEx(x.Condition, BoundTypeRefFactory.BoolTypeRef).WithAccess(x));
                    }
                    else if (!trueVal && falseVal)
                    {
                        // A ? false : true => !A
                        TransformationCount++;
                        return(new BoundUnaryEx(x.Condition, Ast.Operations.LogicNegation).WithAccess(x));
                    }
                }
            }

            return(x);
        }
コード例 #3
0
        public override BoundOperation VisitConditional(BoundConditionalEx x)
        {
            base.VisitConditional(x);

            if (x.IfTrue != null &&
                x.IfTrue.ConstantValue.IsBool(out bool trueVal) &&
                x.IfFalse.ConstantValue.IsBool(out bool falseVal))
            {
                if (trueVal && !falseVal)
                {
                    // A ? true : false => (bool)A
                    TransformationCount++;
                    return
                        (new BoundUnaryEx(x.Condition, Devsense.PHP.Syntax.Ast.Operations.BoolCast)
                         .WithContext(x));
                }

                // TODO: Other possibilities
            }

            return(x);
        }