Ejemplo n.º 1
0
        public override bool TryEvaluate(IValue lhs, IValue rhs, out IValue result)
        {
            if (!(lhs.ValueKind == JsonValueKind.Number && rhs.ValueKind == JsonValueKind.Number))
            {
                result = JsonConstants.Null;
                return(false);
            }

            Decimal decVal1;
            double  dblVal1;
            Decimal decVal2;
            double  dblVal2;

            if (lhs.TryGetDecimal(out decVal1) && rhs.TryGetDecimal(out decVal2))
            {
                if (decVal2 == 0)
                {
                    result = JsonConstants.Null;
                    return(false);
                }
                Decimal val = decVal1 % decVal2;
                result = new DecimalValue(val);
                return(true);
            }
            else if (lhs.TryGetDouble(out dblVal1) && rhs.TryGetDouble(out dblVal2))
            {
                if (dblVal2 == 0)
                {
                    result = JsonConstants.Null;
                    return(false);
                }
                double val = dblVal1 % dblVal2;
                result = new DoubleValue(val);
                return(true);
            }
            else
            {
                result = JsonConstants.Null;
                return(false);
            }
        }
Ejemplo n.º 2
0
        public override bool TryEvaluate(IList <IValue> args,
                                         out IValue result)
        {
            Debug.Assert(this.Arity.HasValue && args.Count == this.Arity !.Value);

            var arg0 = args[0];

            switch (arg0.ValueKind)
            {
            case JsonValueKind.Number:
                result = arg0;
                return(true);

            case JsonValueKind.String:
            {
                var     s = arg0.GetString();
                Decimal dec;
                double  dbl;
                if (Decimal.TryParse(s, out dec))
                {
                    result = new DecimalValue(dec);
                    return(true);
                }
                else if (Double.TryParse(s, out dbl))
                {
                    result = new DoubleValue(dbl);
                    return(true);
                }
                else
                {
                    result = JsonConstants.Null;
                    return(false);
                }
            }

            default:
                result = JsonConstants.Null;
                return(false);
            }
        }
Ejemplo n.º 3
0
        public override bool TryEvaluate(IList <IValue> args,
                                         out IValue result)
        {
            Debug.Assert(this.Arity.HasValue && args.Count == this.Arity !.Value);
            var arg0 = args[0];

            if (arg0.ValueKind != JsonValueKind.Array || arg0.GetArrayLength() == 0)
            {
                result = JsonConstants.Null;
                return(false);
            }

            IValue sum;

            if (!SumFunction.Instance.TryEvaluate(args, out sum))
            {
                result = JsonConstants.Null;
                return(false);
            }

            Decimal decVal;
            double  dblVal;

            if (sum.TryGetDecimal(out decVal))
            {
                result = new DecimalValue(decVal / arg0.GetArrayLength());
                return(true);
            }
            else if (sum.TryGetDouble(out dblVal))
            {
                result = new DoubleValue(dblVal / arg0.GetArrayLength());
                return(true);
            }
            else
            {
                result = JsonConstants.Null;
                return(false);
            }
        }
Ejemplo n.º 4
0
        public override bool TryEvaluate(IList <IValue> args,
                                         out IValue result)
        {
            Debug.Assert(this.Arity.HasValue && args.Count == this.Arity !.Value);

            var arg0 = args[0];

            if (arg0.ValueKind != JsonValueKind.Array || arg0.GetArrayLength() == 0)
            {
                result = JsonConstants.Null;
                return(false);
            }
            foreach (var item in arg0.EnumerateArray())
            {
                if (item.ValueKind != JsonValueKind.Number)
                {
                    result = JsonConstants.Null;
                    return(false);
                }
            }

            double prod = 1;

            foreach (var item in arg0.EnumerateArray())
            {
                double dbl;
                if (!item.TryGetDouble(out dbl))
                {
                    result = JsonConstants.Null;
                    return(false);
                }
                prod *= dbl;
            }
            result = new DoubleValue(prod);

            return(true);
        }