Example #1
0
        internal static void RegisterMethods()
        {
            if (typeof(T) == typeof(int))
            {
                calculator = new IntCalculator() as Calculator <T>;
            }
            else if (typeof(T) == typeof(uint))
            {
                calculator = new UIntCalculator() as Calculator <T>;
            }
            else if (typeof(T) == typeof(float))
            {
                calculator = new FloatCalculator() as Calculator <T>;
            }
            else if (typeof(T) == typeof(double))
            {
                calculator = new DoubleCalculator() as Calculator <T>;
            }

            RegisterLocalFunction(new Function <T>("Random", () => (T)Convert.ChangeType(random.NextDouble(), typeof(T)), false));
            RegisterLocalFunction(new Function <T, T, T>("Random", RandomMinMax, false));
            RegisterLocalFunction(new Function <T, T, T>("Max", Max));
            RegisterLocalFunction(new Function <T, T, T>("Min", Min));

            RegisterMethod(new Method <T, string>("Print", (tval) =>
            {
                if (typeof(T) == typeof(int) || typeof(T) == typeof(short))
                {
                    int ival = (int)(object)tval;
                    if (ival == 0)
                    {
                        return("zero");
                    }
                    if (ival == 1)
                    {
                        return("one");
                    }
                    if (ival == 2)
                    {
                        return("two");
                    }
                    if (ival == 3)
                    {
                        return("three");
                    }
                    if (ival == 4)
                    {
                        return("four");
                    }
                    if (ival == 5)
                    {
                        return("five");
                    }
                    if (ival == 6)
                    {
                        return("six");
                    }
                    if (ival == 7)
                    {
                        return("seven");
                    }
                    if (ival == 8)
                    {
                        return("eight");
                    }
                    if (ival == 9)
                    {
                        return("nine");
                    }
                    return(ival.ToString("N0"));
                }
                else if (typeof(T) == typeof(float) || typeof(T) == typeof(double))
                {
                    double dval = (double)(object)tval;
                    if (dval < 1.0)
                    {
                        return(dval.ToString("N5"));
                    }
                    return(dval.ToString("N2"));
                }
                return(tval.ToString());
            }));
        }