Exemplo n.º 1
0
 protected override ExpressionResult Evaluate(object left, object right)
 {
     if (left is IComparable lc && right is IComparable rc)
     {
         return(Value(lc.CompareTo(rc) > 0));
     }
     return(ExpressionResult.TypeError(Op, left?.GetType(), right?.GetType()));
 }
Exemplo n.º 2
0
Arquivo: In.cs Projeto: bexpr/bexpr-cs
 protected override ExpressionResult Evaluate(object left, object right)
 {
     if (right is IList <object> rl)
     {
         return(Value(rl.Contains(left)));
     }
     return(ExpressionResult.TypeError(Op, left?.GetType(), right?.GetType()));
 }
Exemplo n.º 3
0
        protected override ExpressionResult Evaluate(object left, object right)
        {
            if (right is Type type)
            {
                return(Value(right != null && type.IsAssignableFrom(right.GetType())));
            }

            return(ExpressionResult.TypeError(Op, left?.GetType(), right?.GetType()));
        }
Exemplo n.º 4
0
 protected override ExpressionResult Evaluate(object left, object right)
 {
     if (left is string ls && right is string rs)
     {
         var regex = new Regex(rs);
         return(Value(regex.IsMatch(ls)));
     }
     return(ExpressionResult.TypeError(Op, left?.GetType(), right?.GetType()));
 }
Exemplo n.º 5
0
        public override ExpressionResult Evaluate(T target, EvaluationContext context)
        {
            var leftRes  = Left.Evaluate(target, context);
            var rightRes = Right.Evaluate(target, context);

            if (leftRes.IsError || rightRes.IsError)
            {
                return(ExpressionResult.Error(
                           leftRes.Errors.Concat(rightRes.Errors).ToArray()));
            }
            var left  = leftRes.Value;
            var right = rightRes.Value;

            if (left is string || right is string)
            {
                return(EvalResult(left, right, left.ToString(), right.ToString(), Evaluate));
            }

            if (left is bool ln || right is bool)
            {
                return(EvalValueResult(left, right, left as bool?, right as bool?, Evaluate));
            }

            if (left is decimal || right is decimal)
            {
                var l = left.UpCastDecimal();
                var r = right.UpCastDecimal();
                return(EvalValueResult(left, right, l, r, Evaluate));
            }

            if (left is double ||
                right is double ||
                left is float ||
                right is float)
            {
                var l = left.UpCastDouble();
                var r = right.UpCastDouble();
                return(EvalValueResult(left, right, l, r, Evaluate));
            }

            if (left is long || right is long)
            {
                var l = left.UpCastLong();
                var r = right.UpCastLong();
                return(EvalValueResult(left, right, l, r, Evaluate));
            }

            if (left is int || right is int)
            {
                var l = left.UpCastInt();
                var r = right.UpCastInt();
                return(EvalValueResult(left, right, l, r, Evaluate));
            }

            return(ExpressionResult.TypeError(Op, left?.GetType(), right?.GetType()));
        }
Exemplo n.º 6
0
 protected override ExpressionResult Evaluate(object left, object right)
 {
     if (left is string ls && right is string rs)
     {
         var likeRegex = "^" + Regex.Escape(rs).Replace("%", ".*") + "$";
         var regex     = new Regex(likeRegex);
         return(Value(regex.IsMatch(ls)));
     }
     return(ExpressionResult.TypeError(Op, left?.GetType(), right?.GetType()));
 }
Exemplo n.º 7
0
        public ExpressionResult Evaluate(T target, EvaluationContext context)
        {
            var args = Args.Evaluate(target, context);

            if (args.IsError)
            {
                return(ExpressionResult.Error(args));
            }

            if (args.Value is IReadOnlyList <object> funcArgs)
            {
                return(EvalFunc(target, funcArgs, context));
            }

            return(ExpressionResult.TypeError("FunctionArgs", args.Value?.GetType()));
        }
Exemplo n.º 8
0
        public ExpressionResult Evaluate(T target, EvaluationContext context)
        {
            var baseRes     = Base.Evaluate(target, context);
            var exponentRes = Exponent.Evaluate(target, context);

            if (baseRes.IsError || exponentRes.IsError)
            {
                return(ExpressionResult.Error(baseRes, exponentRes));
            }

            var baseValue     = baseRes.Value.CastDouble();
            var exponentValue = exponentRes.Value.CastDouble();

            if (baseValue == null || exponentValue == null)
            {
                return(ExpressionResult.TypeError("^", baseRes.Value?.GetType(), exponentRes.Value?.GetType()));
            }

            var result = Math.Pow(baseValue.Value, exponentValue.Value);

            return(new ExpressionResult(result));
        }
Exemplo n.º 9
0
        protected override ExpressionResult Evaluate(object left, object right)
        {
            if (right is IList <object> rl && left is IComparable lc)
            {
                if (rl.Count != 2)
                {
                    return(ExpressionResult.Error(
                               "ParameterMismatch",
                               "Between operator expects an array of exactly two elements on the right side"));
                }

                var first  = rl[0];
                var second = rl[1];
                if (first is IComparable fc && second is IComparable sc)
                {
                    return(Value(lc.CompareTo(fc) >= 0 && lc.CompareTo(sc) <= 0));
                }
                return(ExpressionResult.TypeError(Op, first?.GetType(), second?.GetType()));
            }

            return(ExpressionResult.TypeError(Op, left?.GetType(), right?.GetType()));
        }
Exemplo n.º 10
0
 protected virtual ExpressionResult Evaluate(bool left, bool right)
 {
     return(ExpressionResult.TypeError(Op, typeof(bool)));
 }
Exemplo n.º 11
0
 protected virtual ExpressionResult Evaluate(string left, string right)
 {
     return(ExpressionResult.TypeError(Op, typeof(string)));
 }
Exemplo n.º 12
0
 protected virtual ExpressionResult Evaluate(long left, long right)
 {
     return(ExpressionResult.TypeError(Op, typeof(int), typeof(long)));
 }
Exemplo n.º 13
0
 protected virtual ExpressionResult Evaluate(double left, double right)
 {
     return(ExpressionResult.TypeError(Op, typeof(double)));
 }
Exemplo n.º 14
0
 protected virtual ExpressionResult Evaluate(decimal left, decimal right)
 {
     return(ExpressionResult.TypeError(Op, typeof(decimal)));
 }