Example #1
0
        public static object Div(BinaryOpStorage /*!*/ divideStorage, ConversionStorage <double> /*!*/ tofStorage, object self, object other)
        {
            var divide = divideStorage.GetCallSite("/");
            var tof    = tofStorage.GetSite(ConvertToFAction.Make(tofStorage.Context));

            return(ClrFloat.Floor(tof.Target(tof, divide.Target(divide, self, other))));
        }
Example #2
0
        public static RubyArray /*!*/ Coerce(ConversionStorage <double> /*!*/ tof1, ConversionStorage <double> /*!*/ tof2,
                                             object self, object other)
        {
            var context = tof1.Context;

            if (context.GetClassOf(self) == context.GetClassOf(other))
            {
                return(RubyOps.MakeArray2(other, self));
            }

            var site1 = tof1.GetSite(ConvertToFAction.Make(context));
            var site2 = tof2.GetSite(ConvertToFAction.Make(context));

            return(RubyOps.MakeArray2(site1.Target(site1, other), site2.Target(site2, self)));
        }
Example #3
0
        public static double CastToFloat(ConversionStorage <double> /*!*/ floatConversion, object value)
        {
            var site = floatConversion.GetSite(ConvertToFAction.Make(floatConversion.Context));

            return(site.Target(site, value));
        }
Example #4
0
        public double CastToDouble(object value)
        {
            var site = RubyUtils.GetCallSite(ref _tofConversion, ConvertToFAction.Make(Context));

            return(site.Target(site, value));
        }
Example #5
0
        public static object Step(
            BinaryOpStorage /*!*/ equals,
            BinaryOpStorage /*!*/ greaterThanStorage,
            BinaryOpStorage /*!*/ lessThanStorage,
            BinaryOpStorage /*!*/ addStorage,
            ConversionStorage <double> /*!*/ tofStorage,
            BlockParam block, object self, object limit, [Optional] object step)
        {
            if (step == Missing.Value)
            {
                step = ClrInteger.One;
            }

            if (self is double || limit is double || step is double)
            {
                var site = tofStorage.GetSite(ConvertToFAction.Make(tofStorage.Context));
                // At least one of the arguments is double so convert all to double and run the Float version of Step
                double floatSelf  = self is double?(double)self : site.Target(site, self);
                double floatLimit = limit is double?(double)self : site.Target(site, limit);
                double floatStep  = step is double?(double)self : site.Target(site, step);
                return(Step(block, floatSelf, floatLimit, floatStep));
            }
            else
            {
                #region The generic step algorithm:
                // current = self
                // if step is postive then
                //   while current < limit do
                //     yield(current)
                //     current = current + step
                //   end
                // else
                //   while current > limit do
                //     yield(current)
                //     current = current + step
                //   end
                // return self
                #endregion

                bool isStepZero = Protocols.IsEqual(equals, step, 0);
                if (isStepZero)
                {
                    throw RubyExceptions.CreateArgumentError("step can't be 0");
                }

                var  greaterThan    = greaterThanStorage.GetCallSite(">");
                bool isStepPositive = RubyOps.IsTrue(greaterThan.Target(greaterThan, step, 0));
                var  compare        = isStepPositive ? greaterThan : lessThanStorage.GetCallSite("<");

                object current = self;
                while (!RubyOps.IsTrue(compare.Target(compare, current, limit)))
                {
                    object result;
                    if (YieldStep(block, current, out result))
                    {
                        return(result);
                    }

                    var add = addStorage.GetCallSite("+");
                    current = add.Target(add, current, step);
                }
                return(self);
            }
        }