Ejemplo n.º 1
0
        public void Execute_NumberWithUnaryMinus_NegativeNumber()
        {
            FLCompiler compiler = BasicArithmeticTestCompiler();
            object     value    = compiler.Execute("-2500", new DebugContext());

            Assert.AreEqual(-2500d, value);
        }
Ejemplo n.º 2
0
        public void Execute_LongDoubleLiteral_PassedTextAsDouble()
        {
            FLCompiler compiler = BasicArithmeticTestCompiler();
            object     value    = compiler.Execute("1000350324.00000500032000", new DebugContext());

            Assert.AreEqual(1000350324.00000500032d, value);
        }
Ejemplo n.º 3
0
        public void Execute_NumberAsStringLiteral_PassedTextAsString()
        {
            FLCompiler compiler = BasicArithmeticTestCompiler();
            object     value    = compiler.Execute("'1000350324.00000500032000'", new DebugContext());

            Assert.AreEqual("1000350324.00000500032000", value);
        }
Ejemplo n.º 4
0
        public static FLCompiler BasicArithmeticTestCompiler()
        {
            FLCompilerSettings settings = BasicArithmeticTestCompilerSettings();
            FLCompiler         compiler = new FLCompiler(settings);

            return(compiler);
        }
Ejemplo n.º 5
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.º 6
0
        public void Execute_StringWithMultipleOperations_OrderedOperations()
        {
            //PEMDAS
            FLCompiler   compiler  = BasicArithmeticTestCompiler();
            DebugContext context   = new DebugContext();
            string       operators = "";

            context.Operated += (symbol, left, right) =>
            {
                operators += symbol;
            };

            compiler.Execute("10 + 10 - (15 - 15 * 25 / 25 * (25 / 10)) / 100", context);

            Assert.AreEqual("+*//*-/-", operators);
        }
Ejemplo n.º 7
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.º 8
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");
        }
Ejemplo n.º 9
0
        public void Execute_MalformedFLCode_ExceptionThrown()
        {
            FLCompiler compiler = BasicArithmeticTestCompiler();

            compiler.Execute("10 + test_ thisIsWrong", new DebugContext());
        }
Ejemplo n.º 10
0
        public void Execute_NonMinusUnaryOperator_ExceptionThrown()
        {
            FLCompiler compiler = BasicArithmeticTestCompiler();

            compiler.Execute("10 + 10 + +10", new DebugContext());
        }
Ejemplo n.º 11
0
        public void Execute_UnclosedParentheses_ExceptionThrown()
        {
            FLCompiler compiler = BasicArithmeticTestCompiler();

            compiler.Execute("10 + 10 + (15 + 15) + CALL(10, 10, CALL(10, 10, 0", new DebugContext());
        }