Ejemplo n.º 1
0
        public void Execute_LongArithmeticExpressionWithFunctioncalls_LargeDoubleNumber()
        {
            FLCompiler compiler = FLCompilerTests.BasicArithmeticTestCompiler();
            FLRuntime  runtime  = TestRuntime();
            double     result   = (double)compiler.Execute(LongArithmeticExpressionWithFunctionCalls, runtime);

            Assert.AreEqual(LongArithmeticExpressionWithFunctionCallsResult, result);
        }
Ejemplo n.º 2
0
        public static FLRuntime TestRuntime()
        {
            FLRuntime runtime = new FLRuntime();

            runtime.RegisterFunction("TIMESTWO", (t, a) =>
            {
                return(Convert.ToDouble(a[0]) * 2d);
            });

            return(runtime);
        }
Ejemplo n.º 3
0
        public void Execute_FunctionCallOnObject_ThisObjectIsReturnValue()
        {
            FLCompiler compiler = BasicArithmeticTestCompiler();
            FLRuntime  runtime  = new FLRuntime();
            object     lastThis = null;

            runtime.RegisterFunction("add", (t, x) =>
            {
                double total = t == null ? 0f : (double)t;
                lastThis     = t;

                foreach (object obj in x)
                {
                    total += Convert.ToSingle(obj);
                }

                return(total);
            });

            object result = compiler.Execute("add(4, 8)->add(5)", runtime);

            Assert.AreEqual(12d, lastThis);
            Assert.AreEqual(17d, result);
        }
Ejemplo n.º 4
0
        public void CallFunction_NonExistentFunctionCall_ExceptionThrown()
        {
            FLRuntime runtime = TestRuntime();

            runtime.CallFunction("TIMESTHREE", null, 0.5d);
        }
Ejemplo n.º 5
0
        public void RegisterFunction_FunctionWithDuplicateName_ExceptionThrown()
        {
            FLRuntime runtime = TestRuntime();

            runtime.RegisterFunction("TIMESTWO", (t, y) => { return(null); });
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Defines the entry point of the application.
        /// </summary>
        static void Main()
        {
            //This will run these with default settings.
            FLCompilerSettings settings = new FLCompilerSettings();

            settings.PushOperator("*");
            settings.PushOperator("/", true);
            settings.PushOperator("+");
            settings.PushOperator("-", true);
            settings.PushOperator(">=");
            settings.PushOperator("<=", true);
            settings.PushOperator(">", true);
            settings.PushOperator("<", true);
            settings.PushOperator("==");
            settings.PushOperator("&&");
            settings.PushOperator("||");

            FLCompiler compiler = new FLCompiler(settings);
            FLRuntime  runtime  = new FLRuntime();

            //Register some functions into the runtime.
            runtime.RegisterFunction("concat", (t, x) =>
            {
                return(String.Concat(x));
            });

            runtime.RegisterFunction("join", (t, x) =>
            {
                if (x.Length <= 1)
                {
                    return(null);
                }

                return(String.Join(x[0].ToString(), x.Skip(1)));
            });

            runtime.RegisterFunction("repeat", (t, x) =>
            {
                if (x.Length != 2)
                {
                    return(null);
                }

                string repeatedText = "";
                int count           = Convert.ToInt32(x[1]);

                for (int i = 0; i < count; i++)
                {
                    repeatedText += x[0];
                }

                return(repeatedText);
            });

            runtime.RegisterFunction("add", (t, x) =>
            {
                double total = t == null ? 0f : (double)t;

                foreach (object obj in x)
                {
                    total += Convert.ToSingle(obj);
                }

                return(total);
            });

            runtime.RegisterFunction("subtract", (t, x) =>
            {
                double total = t == null ? 0f : (double)t;

                foreach (object obj in x)
                {
                    total -= Convert.ToDouble(obj);
                }

                return(total);
            });

            runtime.RegisterFunction("floor", (t, x) =>
            {
                return(Math.Floor(Convert.ToDouble(x[0])));
            });

            runtime.RegisterFunction("rand", (t, x) =>
            {
                if (x.Length == 2)
                {
                    return(r.Next(Convert.ToInt32(x[0]), Convert.ToInt32(x[1])));
                }

                if (x.Length == 0)
                {
                    return(r.NextDouble());
                }

                return(null);
            });

            //Set the last read line to run the test script, assuming there is one.
            string readLine = testScript;

            Console.WriteLine("Type exit to quit.");

            //Main loop.
            do
            {
                try
                {
                    if (!string.IsNullOrEmpty(readLine))
                    {
                        Console.WriteLine(compiler.Execute(readLine, runtime));
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception Caught:");
                    Console.WriteLine(e.GetType().ToString() + ": " + e.Message);
                }

                Console.Write("> ");
            }while ((readLine = Console.ReadLine()) != "exit");
        }