Exemplo n.º 1
0
        public override BaseExpression Visit(BaseExpression expression)
        {
            if (expression.IsConstant)
            {
                var v = expression.TryStaticEvaluate();
                if (v.HasValue)
                {
                    // Do not substitute the constant value if it is a longer string than the expression
                    if (!_allowLonger && v.ToString().Length > expression.ToString().Length)
                    {
                        return(base.Visit(expression));
                    }

                    if (v.Value.Type == Type.Number)
                    {
                        return(base.Visit(new ConstantNumber(v.Value.Number)));
                    }
                    else
                    {
                        return(base.Visit(new ConstantString(v.Value.String)));
                    }
                }
            }

            return(base.Visit(expression));
        }
Exemplo n.º 2
0
            /// <summary>
            /// Check if an expression that isn't naturally a boolean is in fact a boolean just by chance due to the values used (e.g. `2-1` is a boolean, because it's statically `1`)
            /// </summary>
            /// <param name="expr"></param>
            /// <returns></returns>
            private static bool StaticFalse(BaseExpression expr)
            {
                if (expr.IsBoolean)
                {
                    return(true);
                }

                var val = expr.TryStaticEvaluate();

                if (!val.HasValue)
                {
                    return(false);
                }

                if (val.Value.Type != Execution.Type.Number)
                {
                    return(false);
                }

                return((val.Value.Number == Number.Zero) || (val.Value.Number == Number.One));
            }
Exemplo n.º 3
0
 public static Value?TryStaticEvaluate(this BaseExpression expression)
 {
     return(expression.TryStaticEvaluate(out _));
 }