示例#1
0
 public override MSAst TransformCore(ScriptGenerator generator)
 {
     return(MSAst.Condition(
                Test.Transform(generator),
                IfTrue.Transform(generator),
                IfFalse.Transform(generator)));
 }
示例#2
0
        public override void Print(IndentTextWriter writer)
        {
            writer.WriteIndent();
            writer.WriteLine("Conditional");

            writer.WriteIndent();
            writer.WriteLine("-Expression");

            writer.Indent += 2;
            Condition.Print(writer);
            writer.Indent -= 2;

            writer.WriteIndent();
            writer.WriteLine("-True");

            writer.Indent += 2;
            IfTrue.Print(writer);
            writer.Indent -= 2;

            writer.WriteIndent();
            writer.WriteLine("-False");

            writer.Indent += 2;
            IfFalse.Print(writer);
            writer.Indent -= 2;
        }
示例#3
0
 public override void Dump(SourceWriter sw, int indentChange)
 {
     Test.Dump(sw, indentChange);
     sw.Write(" ? ");
     IfTrue.Dump(sw, indentChange);
     sw.Write(" : ");
     IfFalse.Dump(sw, indentChange);
 }
示例#4
0
        public override Expression Simplify()
        {
            Condition = Condition.Simplify();
            IfTrue    = IfTrue.Simplify();
            IfFalse   = IfFalse.Simplify();

            return(this);
        }
示例#5
0
 internal override void ToStream(ZetboxStreamWriter binStream, StreamSerializationContext ctx)
 {
     binStream.Write((byte)SerializableExpressionType.Conditional);
     base.ToStream(binStream, ctx);
     Test.ToStream(binStream, ctx);
     IfTrue.ToStream(binStream, ctx);
     IfFalse.ToStream(binStream, ctx);
 }
示例#6
0
        public override System.Linq.Expressions.Expression Transform()
        {
            var condition = Condition.TransformReadAsBoolean();
            var ifTrue    = IfTrue.Transform();
            var ifFalse   = IfFalse != null?IfFalse.Transform() : System.Linq.Expressions.Expression.Empty();

            return(System.Linq.Expressions.Expression.IfThenElse(condition, ifTrue, ifFalse));
        }
示例#7
0
        public override void SetParent(Expression parent)
        {
            base.SetParent(parent);

            Condition.SetParent(this);
            IfTrue.SetParent(this);
            IfFalse.SetParent(this);
        }
        public Expression Update(Expression test, Expression ifTrue, Expression ifFalse = null)
        {
            if (Test.Equals(test) && IfTrue.Equals(ifTrue) && IfFalse == ifFalse)
            {
                return(this);
            }

            return(CreateConditionalExpression());
        }
示例#9
0
 protected override void AppendTo(SqlStringBuilder builder)
 {
     builder.Append("CASE WHEN ");
     Test.AppendTo(builder);
     builder.Append(" THEN ");
     IfTrue.AppendTo(builder);
     builder.Append(" ELSE ");
     IfFalse.AppendTo(builder);
     builder.Append(" END");
 }
示例#10
0
        public override LazyValue Evaluate(Environment env)
        {
            var condition = Condition.Evaluate(env.Local());
            var ifTrue    = IfTrue.Evaluate(env.Local());
            var ifFalse   = IfFalse.Evaluate(env.Local());

            return(new ApplicationValue(
                       new FunctionValue(cond => Defaults.True.CompareTo(cond.WHNFValue) == 0 ? ifTrue : ifFalse),
                       condition
                       ));
        }
示例#11
0
        public override double GetValue(double x, double y)
        {
            double a = SourceModule1.GetValue(x, y);
            double b = SourceModule2.GetValue(x, y);

            if (a != b)
            {
                return(IfTrue.GetValue(x, y));
            }

            return(IfFalse.GetValue(x, y));
        }
        public override ICollection <Type> GetKnownTypes([Optional] Container container)
        {
            Console.WriteLine("Conditional Expression Node KnownType");

            var totalTypes = base.GetKnownTypes(container).Concat(new [] { this.GetType() })
                             .Concat(Test?.GetKnownTypes(container) ?? Enumerable.Empty <Type>())
                             .Concat(IfTrue?.GetKnownTypes(container) ?? Enumerable.Empty <Type>())
                             .Concat(IfFalse?.GetKnownTypes(container) ?? Enumerable.Empty <Type>())
                             .ToList();

            return(totalTypes);
        }
示例#13
0
        public override UnityEngine.Color GetColour(double x, double y)
        {
            // Read colour:
            UnityEngine.Color col1 = SourceModule1.GetColour(x, y);

            // Read colour:
            UnityEngine.Color col2 = SourceModule2.GetColour(x, y);

            // T:
            UnityEngine.Color t = IfTrue.GetColour(x, y);

            // False:
            UnityEngine.Color f = IfFalse.GetColour(x, y);

            // Pick:
            if (col1.r != col2.r)
            {
                col1.r = t.r;
            }
            else
            {
                col1.r = f.r;
            }

            if (col1.g != col2.g)
            {
                col1.g = t.g;
            }
            else
            {
                col1.g = f.g;
            }

            if (col1.b != col2.b)
            {
                col1.b = t.b;
            }
            else
            {
                col1.b = f.b;
            }

            if (col1.a != col2.a)
            {
                col1.a = t.a;
            }
            else
            {
                col1.a = f.a;
            }

            return(col1);
        }
示例#14
0
        public override double GetWrapped(double x, double y, int wrap)
        {
            double a = SourceModule1.GetWrapped(x, y, wrap);
            double b = SourceModule2.GetWrapped(x, y, wrap);

            if (a != b)
            {
                return(IfTrue.GetWrapped(x, y, wrap));
            }

            return(IfFalse.GetWrapped(x, y, wrap));
        }
示例#15
0
 /// <summary>
 /// Builds a <see langword="string"/> representing the <see cref="Expression"/>.
 /// </summary>
 /// <param name="builder">A <see cref="System.Text.StringBuilder"/> to add the created <see langword="string"/>.</param>
 internal override void BuildString(StringBuilder builder)
 {
     if (builder == null)
     {
         throw new ArgumentNullException("builder");
     }
     builder.Append("IIF(");
     Test.BuildString(builder);
     builder.Append(", ");
     IfTrue.BuildString(builder);
     builder.Append(", ");
     IfFalse.BuildString(builder);
     builder.Append(")");
 }
示例#16
0
 /// <summary>
 /// 添加到表达式池
 /// </summary>
 internal override void pushPool()
 {
     Test.PushCountPool();
     Test = null;
     if (IfTrue != null)
     {
         IfTrue.PushCountPool();
         IfTrue = null;
     }
     if (Test != null)
     {
         IfFalse.PushCountPool();
         IfFalse = null;
     }
     typePool <ConditionalExpression> .PushNotNull(this);
 }
示例#17
0
        public override async Task <SqlExpression> ReduceAsync(IContext context)
        {
            var returnType = Test.GetSqlType(context);

            if (!(returnType is SqlBooleanType))
            {
                throw new InvalidOperationException("The expression test has not a BOOLEAN result");
            }

            var ifTrueType  = IfTrue.GetSqlType(context);
            var ifFalseType = IfFalse.GetSqlType(context);

            if (!ifTrueType.IsComparable(ifFalseType))
            {
                throw new SqlExpressionException("The value returned in case of TRUE and in case of FALSE must be compatible");
            }

            var testResult = await Test.ReduceAsync(context);

            if (testResult.ExpressionType != SqlExpressionType.Constant)
            {
                throw new InvalidOperationException();
            }

            var value = ((SqlConstantExpression)testResult).Value;

            if (value.IsNull || value.IsUnknown)
            {
                return(Constant(value));
            }

            if (value.IsTrue)
            {
                return(await IfTrue.ReduceAsync(context));
            }
            if (value.IsFalse)
            {
                if (IfFalse != null)
                {
                    return(await IfFalse.ReduceAsync(context));
                }

                return(Constant(SqlObject.Unknown));
            }

            return(await base.ReduceAsync(context));
        }
示例#18
0
        public async Task <IReadOnlyList <OnCondition> > Select(ActionContext actionContext, CancellationToken cancel = default)
        {
            var(eval, _) = Condition.TryGetValue(actionContext.State);
            ITriggerSelector selector;

            if (eval)
            {
                selector = IfTrue;
                IfTrue.Initialize(_conditionals, _evaluate);
            }
            else
            {
                selector = IfFalse;
                IfFalse.Initialize(_conditionals, _evaluate);
            }

            return(await selector.Select(actionContext, cancel).ConfigureAwait(false));
        }
示例#19
0
        /// <summary>
        /// 每次获取的结果极有可能不一样。
        /// </summary>
        public bool Assert(IfElseResult result)
        {
            IfElseResult r;

            try {
                var v = Condition?.Invoke() == Expect;
                if (v == true)
                {
                    try {
                        IfTrue?.Invoke();
                        r = IfElseResult.TC0;
                    } catch {
                        r = IfElseResult.TC1;
                    }
                }
                else
                {
                    try {
                        IfFalse?.Invoke();
                        r = IfElseResult.FC0;
                    } catch {
                        r = IfElseResult.FC1;
                    }
                }
            } catch {
                try {
                    if (IfError == null)
                    {
                        if (IfFalseAsIfError)
                        {
                            IfFalse?.Invoke();
                        }
                    }
                    else
                    {
                        IfError?.Invoke();
                    }
                    r = IfElseResult.E0;
                } catch {
                    r = IfElseResult.E1;
                }
            }
            return((r | result) == r);
        }
示例#20
0
        public override int Compile(FunctionContext context)
        {
            context.Line(FileName, Line);

            var stack      = 0;
            var falseLabel = context.MakeLabel("ternaryFalse");
            var endLabel   = context.MakeLabel("ternaryEnd");

            stack += Condition.Compile(context);
            stack += context.JumpFalse(falseLabel);
            CheckStack(IfTrue.Compile(context), 1);
            stack += context.Jump(endLabel);
            stack += context.Bind(falseLabel);
            CheckStack(IfFalse.Compile(context), 1);
            stack += context.Bind(endLabel);

            CheckStack(stack, 0);
            return(1);
        }
示例#21
0
        public async Task <IReadOnlyList <int> > Select(SequenceContext context, CancellationToken cancel = default(CancellationToken))
        {
            var(value, error) = condition.TryEvaluate(context.State);
            var eval = error == null && (bool)value;
            ITriggerSelector selector;

            if (eval)
            {
                selector = IfTrue;
                IfTrue.Initialize(_conditionals, _evaluate);
            }
            else
            {
                selector = IfFalse;
                IfFalse.Initialize(_conditionals, _evaluate);
            }

            return(await selector.Select(context, cancel).ConfigureAwait(false));
        }
示例#22
0
        public override Type Infer(Environment env, Substitution subst)
        {
            try
            {
                var condition = Condition.Infer(env.Local(), subst);
                condition.Unify(TypeDefaults.Bool, subst);

                var ifTrue  = IfTrue.Infer(env.Local(), subst);
                var ifFalse = IfFalse.Infer(env.Local(), subst);
                ifTrue.Unify(ifFalse, subst);

                return(ifTrue.Perform(subst));
            }
            catch (StackedException ex)
            {
                ex.Push("In an if expression: " + this);
                throw;
            }
        }
示例#23
0
        public override bool SatisfiesConstraint(T input)
        {
            var result = Operand.SatisfiesConstraint(input);

            if (result)
            {
                if (IfTrue != null)
                {
                    return(IfTrue.SatisfiesConstraint(input));
                }
            }
            else
            {
                if (IfFalse != null)
                {
                    return(IfFalse.SatisfiesConstraint(input));
                }
            }

            return(result);
        }
示例#24
0
        /// <see cref="IExpr.PrintTo"/>
        public void PrintTo(Printer p)
        {
            if (p.Pretty)
            {
                p.LineBreak(true);
            }

            p.Write("if ");
            Condition.PrintTo(p);

            if (p.Pretty)
            {
                p.LineBreak();
            }
            else
            {
                p.Write(' ');
            }

            p.Write("then ");
            IfTrue.PrintTo(p);

            if (p.Pretty)
            {
                p.LineBreak();
            }
            else
            {
                p.Write(' ');
            }

            p.Write("else ");
            IfFalse.PrintTo(p);

            if (p.Pretty)
            {
                p.Dedent();
            }
        }
 /// <summary>
 /// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
 /// </summary>
 /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
 /// <returns>
 ///   <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
 /// </returns>
 public override bool Equals(object obj)
 {
     if (ReferenceEquals(this, obj))
     {
         return(true);
     }
     if (obj is TernaryOperatorNode tn)
     {
         if (!Condition.Equals(tn.Condition))
         {
             return(false);
         }
         if (!IfTrue.Equals(tn.IfTrue))
         {
             return(false);
         }
         if (!IfFalse.Equals(tn.IfFalse))
         {
             return(false);
         }
         return(true);
     }
     return(false);
 }
示例#26
0
        protected override object DoEvaluate(ScriptThread thread)
        {
            thread.CurrentNode = this;  //standard prolog
            object result = null;
            var    test   = Test.Evaluate(thread);
            var    isTrue = thread.Runtime.IsTrue(test);

            if (isTrue)
            {
                if (IfTrue != null)
                {
                    result = IfTrue.Evaluate(thread);
                }
            }
            else
            {
                if (IfFalse != null)
                {
                    result = IfFalse.Evaluate(thread);
                }
            }
            thread.CurrentNode = Parent; //standard epilog
            return(result);
        }
 /// <summary>
 /// Converts this instance to an expression.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <returns></returns>
 public override Expression ToExpression(IExpressionContext context)
 {
     return(Expression.Condition(Test.ToExpression(context), IfTrue.ToExpression(context), IfFalse.ToExpression(context)));
 }
 public override ConditionalExpression DoToExpression()
 {
     return(Expression.Condition(Test.ToExpression(), IfTrue.ToExpression(), IfFalse.ToExpression()));
 }
示例#29
0
 public override Type GetExpressionType()
 {
     // TOOD: check same type as IfFalse?
     return(IfTrue.GetExpressionType());
 }
 public override Expression FromNode([Optional] Container container)
 {
     Console.WriteLine(NodeType);
     return(Expression.Condition(Test.FromNode(container), IfTrue.FromNode(container), IfFalse.FromNode(container), Type.FromNode()));
 }