Ejemplo n.º 1
0
        /// <summary>
        /// Retrieves the value of the expression
        /// </summary>
        /// <param name="evalContext">Evaluation context</param>
        /// <returns>Evaluated expression value</returns>
        public override ExpressionValue Evaluate(IEvaluationContext evalContext)
        {
            var cond = Condition.Evaluate(evalContext);

            if (Condition.EvaluationError != null)
            {
                EvaluationError = Condition.EvaluationError;
                return(ExpressionValue.Error);
            }
            if (cond.AsBool())
            {
                var trueValue = TrueExpression.Evaluate(evalContext);
                if (TrueExpression.EvaluationError == null)
                {
                    return(trueValue);
                }
                EvaluationError = TrueExpression.EvaluationError;
                return(ExpressionValue.Error);
            }

            var falseValue = FalseExpression.Evaluate(evalContext);

            if (FalseExpression.EvaluationError == null)
            {
                return(falseValue);
            }
            EvaluationError = FalseExpression.EvaluationError;
            return(ExpressionValue.Error);
        }
        /// <summary>
        /// Retrieves the value of the expression
        /// </summary>
        /// <param name="evalContext">Evaluation context</param>
        /// <returns>Evaluated expression value</returns>
        public override ushort Evaluate(IEvaluationContext evalContext)
        {
            var cond = Condition.Evaluate(evalContext);

            if (Condition.EvaluationError != null)
            {
                EvaluationError = Condition.EvaluationError;
                return(0);
            }
            if (cond != 0)
            {
                var trueValue = TrueExpression.Evaluate(evalContext);
                if (TrueExpression.EvaluationError == null)
                {
                    return(trueValue);
                }
                EvaluationError = TrueExpression.EvaluationError;
                return(0);
            }

            var falseValue = FalseExpression.Evaluate(evalContext);

            if (FalseExpression.EvaluationError == null)
            {
                return(falseValue);
            }
            EvaluationError = FalseExpression.EvaluationError;
            return(0);
        }
Ejemplo n.º 3
0
        public override void Compile(ICompiler cmp, bool reverse)
        {
            int     gid  = group.Number;
            LinkRef tail = cmp.NewLink();

            if (FalseExpression == null)
            {
                //    IfDefined :1
                //      <yes_exp>
                // 1: <tail>

                cmp.EmitIfDefined(gid, tail);
                TrueExpression.Compile(cmp, reverse);
            }
            else
            {
                //    IfDefined :1
                //      <yes_expr>
                //      Jump :2
                // 1:   <no_expr>
                // 2: <tail>

                LinkRef false_expr = cmp.NewLink();
                cmp.EmitIfDefined(gid, false_expr);
                TrueExpression.Compile(cmp, reverse);
                cmp.EmitJump(tail);
                cmp.ResolveLink(false_expr);
                FalseExpression.Compile(cmp, reverse);
            }

            cmp.ResolveLink(tail);
        }
Ejemplo n.º 4
0
        public override void SetParent(Expression parent)
        {
            Parent = parent;

            Condition.SetParent(this);
            TrueExpression.SetParent(this);
            FalseExpression.SetParent(this);
        }
 public override IEnumerable <BindingParserNode> EnumerateNodes()
 {
     return
         (base.EnumerateNodes()
          .Concat(ConditionExpression.EnumerateNodes())
          .Concat(TrueExpression.EnumerateNodes())
          .Concat(FalseExpression.EnumerateNodes()));
 }
Ejemplo n.º 6
0
        public override bool IsEquivalentTo(AstNode otherNode)
        {
            var otherConditional = otherNode as Conditional;

            return(otherConditional != null &&
                   Condition.IsEquivalentTo(otherConditional.Condition) &&
                   TrueExpression.IsEquivalentTo(otherConditional.TrueExpression) &&
                   FalseExpression.IsEquivalentTo(otherConditional.FalseExpression));
        }
Ejemplo n.º 7
0
        public override void Clone(JsNode node)
        {
            base.Clone(node);
            var node2 = (JsConditionalExpression)node;

            node2.Condition       = Condition.Clone();
            node2.TrueExpression  = TrueExpression.Clone();
            node2.FalseExpression = FalseExpression.Clone();
        }
Ejemplo n.º 8
0
 public override void Walk(PythonWalker walker)
 {
     if (walker.Walk(this))
     {
         Test?.Walk(walker);
         TrueExpression?.Walk(walker);
         FalseExpression?.Walk(walker);
     }
     walker.PostWalk(this);
 }
        public override ExpressionCode GenerateCode()
        {
            var stringCode = Condition.GenerateCode().Code;

            stringCode += " ? " + TrueExpression.GenerateCode().Code;
            stringCode += " : " + FalseExpression.GenerateCode().Code;
            return(new ExpressionCode {
                Code = stringCode, Type = TrueExpression.GenerateCode().Type
            });
        }
Ejemplo n.º 10
0
        protected virtual void VisitWhereNotIn(WhereNotInExpression expression)
        {
            IWhereExpression exp = new TrueExpression();

            foreach (var value in expression.Values)
            {
                exp = new AndAlsoExpression(exp, new WhereNotEqualsExpression(null, expression.FieldName, value));
            }
            this.VisitWhere(exp);
        }
Ejemplo n.º 11
0
 public override dynamic Evaluate(SymbolTable table)
 {
     if (EvaluationExpression.Evaluate(table))
     {
         return(TrueExpression.Evaluate(table));
     }
     else
     {
         return(FalseExpression.Evaluate(table));
     }
 }
Ejemplo n.º 12
0
 private Expression FindTrueExpression()
 {
     for (int i = 0; i < mExpressions.Count; i++)
     {
         if (mExpressions[i].mType == Expression.eType.TYPE_TRUE)
         {
             TrueExpression expression = (TrueExpression)mExpressions[i];
             return(expression);
         }
     }
     return(null);
 }
        internal override void AppendTo(SqlStringBuilder builder)
        {
            builder.Append("CASE WHEN ");
            TestExpression.AppendTo(builder);
            builder.Append(" THEN ");
            TrueExpression.AppendTo(builder);

            if (FalseExpression != null)
            {
                builder.Append(" ELSE ");
                FalseExpression.AppendTo(builder);
            }
        }
Ejemplo n.º 14
0
        public void CreateAndEvaluateTrueExpression()
        {
            TrueExpression expression = new TrueExpression();

            Assert.IsNotNull(expression);
            Assert.IsTrue((bool)expression.Value);

            Machine machine = new Machine();

            expression.Evaluate(machine);

            Assert.AreEqual(1, machine.StackCount);
            Assert.IsTrue((bool)machine.Pop());
        }
Ejemplo n.º 15
0
        public override string ToString()
        {
            StringBuilder buf = new StringBuilder();

            buf.Append("(");
            buf.Append(ConditionExpression == null ? DSDefinitions.Keyword.Null : ConditionExpression.ToString());
            buf.Append(" ? ");
            buf.Append(TrueExpression == null ? DSDefinitions.Keyword.Null : TrueExpression.ToString());
            buf.Append(" : ");
            buf.Append(FalseExpression == null ? DSDefinitions.Keyword.Null : FalseExpression.ToString());
            buf.Append(")");

            return(buf.ToString());
        }
Ejemplo n.º 16
0
        public override bool IsComplex()
        {
            bool comp = false;

            if (TrueExpression != null)
            {
                comp |= TrueExpression.IsComplex();
            }
            if (FalseExpression != null)
            {
                comp |= FalseExpression.IsComplex();
            }

            return(comp | GetFixedWidth() <= 0);
        }
Ejemplo n.º 17
0
        public override PrimitiveType FindPrimitiveType()
        {
            if (TrueExpression != null && FalseExpression != null)
            {
                // if the primitive type of both true and false expressions is the same, then
                // we know the primitive type. Otherwise we do not.
                PrimitiveType trueType = TrueExpression.FindPrimitiveType();
                if (trueType == FalseExpression.FindPrimitiveType())
                {
                    return(trueType);
                }
            }

            // nope -- they don't match, so we don't know
            return(PrimitiveType.Other);
        }
Ejemplo n.º 18
0
 public override bool IsComplex()
 {
     if (group == null)
     {
         return(Alternate.IsComplex());
     }
     if (TrueExpression != null && TrueExpression.IsComplex())
     {
         return(true);
     }
     if (FalseExpression != null && FalseExpression.IsComplex())
     {
         return(true);
     }
     return(GetFixedWidth() <= 0);
 }
Ejemplo n.º 19
0
        public override bool Equals(object other)
        {
            if (null == ConditionExpression || null == TrueExpression || null == FalseExpression)
            {
                return(false);
            }

            var otherNode = other as InlineConditionalNode;

            if (null == otherNode)
            {
                return(false);
            }

            return(ConditionExpression.Equals(otherNode.ConditionExpression) &&
                   TrueExpression.Equals(otherNode.TrueExpression) &&
                   FalseExpression.Equals(otherNode.FalseExpression));
        }
        /// <summary>
        /// Retrieves the value of the expression
        /// </summary>
        /// <param name="evalContext">Evaluation context</param>
        /// <param name="checkOnly"></param>
        /// <returns>Evaluated expression value</returns>
        public override ExpressionValue Evaluate(IExpressionEvaluationContext evalContext, bool checkOnly = false)
        {
            // --- Check for condition expression errors
            var cond = Condition.Evaluate(evalContext);

            if (Condition.EvaluationError != null)
            {
                EvaluationError = Condition.EvaluationError;
                return(ExpressionValue.Error);
            }

            if (cond.Type == ExpressionValueType.ByteArray)
            {
                EvaluationError = "A byte array cannot be used as a condition";
                return(ExpressionValue.Error);
            }

            if (checkOnly)
            {
                return(ExpressionValue.NonEvaluated);
            }

            if (cond.AsBool())
            {
                // --- Condition is true
                var trueValue = TrueExpression.Evaluate(evalContext);
                if (TrueExpression.EvaluationError == null)
                {
                    return(trueValue);
                }
                EvaluationError = TrueExpression.EvaluationError;
                return(ExpressionValue.Error);
            }

            // --- Condition is false
            var falseValue = FalseExpression.Evaluate(evalContext);

            if (FalseExpression.EvaluationError == null)
            {
                return(falseValue);
            }
            EvaluationError = FalseExpression.EvaluationError;
            return(ExpressionValue.Error);
        }
Ejemplo n.º 21
0
 public override async Task WalkAsync(PythonWalkerAsync walker, CancellationToken cancellationToken = default)
 {
     if (await walker.WalkAsync(this, cancellationToken))
     {
         if (Test != null)
         {
             await Test.WalkAsync(walker, cancellationToken);
         }
         if (TrueExpression != null)
         {
             await TrueExpression.WalkAsync(walker, cancellationToken);
         }
         if (FalseExpression != null)
         {
             await FalseExpression.WalkAsync(walker, cancellationToken);
         }
     }
     await walker.PostWalkAsync(this, cancellationToken);
 }
Ejemplo n.º 22
0
 internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format)
 {
     TrueExpression.AppendCodeString(res, ast, format);
     res.Append(this.GetPreceedingWhiteSpace(ast));
     res.Append("if");
     if (!ast.HasVerbatim)
     {
         res.Append(' ');
     }
     Test.AppendCodeString(res, ast, format);
     res.Append(this.GetSecondWhiteSpace(ast));
     if (!this.IsIncompleteNode(ast))
     {
         res.Append("else");
         if (!ast.HasVerbatim)
         {
             res.Append(' ');
         }
         FalseExpression.AppendCodeString(res, ast, format);
     }
 }
        protected override void GenerateInner(CodeGenerator generator)
        {
            if (Condition.Precedence > PrecedenceGroup.Ternary)
            {
                generator.Write(TokenType.Punctuation, '(');
            }
            Condition.Generate(generator);
            if (Condition.Precedence > PrecedenceGroup.Ternary)
            {
                generator.Write(TokenType.Punctuation, ')');
            }

            generator.Write(TokenType.Space, ' ');
            generator.Write(TokenType.Punctuation, '?');
            generator.Write(TokenType.Space, ' ');

            if (TrueExpression.Precedence >= PrecedenceGroup.Ternary)
            {
                generator.Write(TokenType.Punctuation, '(');
            }
            TrueExpression.Generate(generator);
            if (TrueExpression.Precedence >= PrecedenceGroup.Ternary)
            {
                generator.Write(TokenType.Punctuation, ')');
            }

            generator.Write(TokenType.Space, ' ');
            generator.Write(TokenType.Punctuation, ':');
            generator.Write(TokenType.Space, ' ');

            if (FalseExpression.Precedence >= PrecedenceGroup.Ternary)
            {
                generator.Write(TokenType.Punctuation, '(');
            }
            FalseExpression.Generate(generator);
            if (FalseExpression.Precedence >= PrecedenceGroup.Ternary)
            {
                generator.Write(TokenType.Punctuation, ')');
            }
        }
        /// <summary>
        /// Retrieves the value of the expression
        /// </summary>
        /// <param name="evalContext">Evaluation context</param>
        /// <returns>Evaluated expression value</returns>
        public override ExpressionValue Evaluate(IExpressionEvaluationContext evalContext)
        {
            var cond = Condition.Evaluate(evalContext);

            if (!cond.IsValid)
            {
                return(ExpressionValue.Error);
            }

            if (cond.Value != 0)
            {
                var result = TrueExpression.Evaluate(evalContext);
                SuggestTypeOf(TrueExpression);
                return(result);
            }
            else
            {
                var result = FalseExpression.Evaluate(evalContext);
                SuggestTypeOf(FalseExpression);
                return(result);
            }
        }
Ejemplo n.º 25
0
        public override ValueExpression Evaluate(IParserContext context)
        {
            bool result = context.ToBoolean(Condition.Evaluate(context).Value);

            ValueExpression expression = null;

            if (result)
            {
                expression = TrueExpression.Evaluate(context);
            }
            else if (FalseExpression != null)
            {
                expression = FalseExpression.Evaluate(context);
            }

            if (expression is ReturnValueExpression)
            {
                return(expression);
            }

            return(Exp.NullValue(TokenPosition));
        }
            protected override CType __GetCType(IIdentifierTypeResolver Resolver)
            {
                var TrueType  = TrueExpression.GetCachedCType(Resolver);
                var FalseType = FalseExpression.GetCachedCType(Resolver);

                if (TrueType == FalseType)
                {
                    return(TrueType);
                }
                else
                {
                    var TrueSize  = TrueType.GetSize(null);
                    var FalseSize = FalseType.GetSize(null);
                    if (TrueSize > FalseSize)
                    {
                        return(TrueType);
                    }
                    if (FalseSize > TrueSize)
                    {
                        return(FalseType);
                    }
                    throw (new NotImplementedException());
                }
            }
Ejemplo n.º 27
0
 protected override void VisitTrue(TrueExpression expression)
 {
     this.LinqExpression = PredicateBuilder.And(LinqExpression, it => true);
 }
Ejemplo n.º 28
0
 public override object Eval(RuntimeContext context)
 {
     return((bool)Test.Eval(context) ? TrueExpression.Eval(context) : FalseExpression.Eval(context));
 }
 public override string ToDisplayString() => $"{ConditionExpression.ToDisplayString()}?{TrueExpression.ToDisplayString()}:{FalseExpression.ToDisplayString()}";
Ejemplo n.º 30
0
 protected virtual void VisitWhereNotIn(WhereNotInExpression expression)
 {
     IWhereExpression exp = new TrueExpression();
     foreach (var value in expression.Values)
     {
         exp = new AndAlsoExpression(exp, new WhereNotEqualsExpression(null, expression.FieldName, value));
     }
     this.VisitWhere(exp);
 }
Ejemplo n.º 31
0
 protected override void VisitTrue(TrueExpression expression)
 {
     this.LinqExpression = PredicateBuilder.And(LinqExpression, it => true);
 }
Ejemplo n.º 32
0
 protected override void VisitTrue(TrueExpression expression)
 {
     ThrowNotSupported();
 }
Ejemplo n.º 33
0
 protected abstract void VisitTrue(TrueExpression expression);
Ejemplo n.º 34
0
 protected override void VisitTrue(TrueExpression expression)
 {
     clauseText.AppendFormat("(true)");
 }