예제 #1
0
 public static KObject DoMath(KObject a, KObject b, Func <Double, Double, Double> dop, Func <long, long, long> lop)
 {
     if (!(KNumber.IsNumber(a) && KNumber.IsNumber(b)))
     {
         return(null);
     }
     if (a is KDouble || b is KDouble)
     {
         return(new KDouble(dop(KNumber.GetDouble(a), KNumber.GetDouble(b))));
     }
     else
     {
         return(new KInteger(lop((a as KInteger).Value, (b as KInteger).Value)));
     }
 }
예제 #2
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 2, "*");

            if (res != null)
            {
                return(CPS.Error(res, cont));
            }
            KObject a = First(args), b = Second(args);
            var     result = KNumber.DoMath(a, b, (x, y) => x * y, (x, y) => x * y);

            if (result == null)
            {
                return(CPS.Error("*: wrong types", cont));
            }
            return(Return(result, cont));
        }
예제 #3
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 2, "-");

            if (res != null)
            {
                return(CPS.Error(res, cont));
            }
            KObject a = First(args), b = Second(args);

            if (KNumber.IsNumber(b) && KNumber.GetDouble(b) == 0.0)
            {
                return(CPS.Error("Division by zero", cont));
            }
            var result = KNumber.DoMath(a, b, (x, y) => x / y, (x, y) => x / y);

            if (result == null)
            {
                return(CPS.Error("/: wrong types", cont));
            }
            return(Return(result, cont));
        }
예제 #4
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            var res = CheckParameter(args, 2, ">?");

            if (res != null)
            {
                return(CPS.Error(res, cont));
            }
            KObject a = First(args), b = Second(args);

            if (!(KNumber.IsNumber(a) && KNumber.IsNumber(b)))
            {
                return(CPS.Error("wrong types", cont));
            }
            if (a is KDouble || b is KDouble)
            {
                return(ReturnBool(KNumber.GetDouble(a) > KNumber.GetDouble(b), cont));
            }
            else
            {
                return(ReturnBool((a as KInteger).Value > (b as KInteger).Value, cont));
            }
        }