Esempio n. 1
0
 static public void OnInputExpression(Event <HTMLInputElement> ev)
 {
     try
     {
         RusticContext context = new RusticContext()
         {
             availableTypeCasts = new System.Collections.Generic.Dictionary <string, Type>()
             {
                 { "int", typeof(int) },
                 { "float", typeof(float) },
                 { "double", typeof(double) },
             },
             variables = new System.Collections.Generic.Dictionary <string, object>()
             {
                 { "PI", Math.PI },
             },
         };
         RusticExpr expr = new RusticExpr(InputExpression.Value, context);
         expr.Execute();
         ResultBox.InnerHTML = expr.PrintToHtml();
     } catch (Exception e)
     {
         ResultBox.InnerHTML = $"Fix your formula: {e.Message}";
     }
     //expr.PrintDebug(); // Console
 }
Esempio n. 2
0
        void _Main(string[] args)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            RusticContext context = new RusticContext()
            {
                availableTypeCasts = new Dictionary <string, Type>()
                {
                    { "int", typeof(int) },
                    { "float", typeof(float) },
                    { "double", typeof(double) },
                },

                variables = new Dictionary <string, object>()
                {
                    { "PI", Math.PI },
                },
            };

            bool           simplify = false;
            ConsoleKeyInfo key      = new ConsoleKeyInfo((char)0, (ConsoleKey)0, false, false, false);

            do
            {
                switch (key.Key)
                {
                case ConsoleKey.UpArrow: selectedExpression--; simplify = false; break;

                case ConsoleKey.DownArrow: selectedExpression++; simplify = false; break;

                case ConsoleKey.Enter:
                case ConsoleKey.Spacebar: simplify = !simplify; break;

                case ConsoleKey.Home: selectedExpression = 0; break;

                case ConsoleKey.End: selectedExpression = expressions.Count - 1; break;
                }

                if (selectedExpression < 0)
                {
                    selectedExpression = 0;
                }
                else
                if (selectedExpression >= expressions.Count)
                {
                    selectedExpression = expressions.Count - 1;
                }

                Console.ForegroundColor = ConsoleColor.White;
                Console.BackgroundColor = ConsoleColor.Black;
                Console.Clear();
                Console.WriteLine("Test expressions:");
                for (int expId = 0; expId < expressions.Count; expId++)
                {
                    Console.BackgroundColor = selectedExpression == expId ? ConsoleColor.DarkBlue : ConsoleColor.Black;
                    Console.WriteLine($"{(selectedExpression == expId ? " > " : "   ")}{expressions[expId].input} ".PadRight(40));
                }
                Console.WriteLine();
                Console.BackgroundColor = ConsoleColor.Black;

                TestExpr   test = expressions[selectedExpression];
                RusticExpr expr = new RusticExpr(test.input, context);

                if (simplify)
                {
                    RusticExprBuilder.SimplifyExpression(expr);
                }

                object result = expr.Execute();
                Console.WriteLine($"Expectation = {test.result ?? "null"} [{test.result?.GetType().Name}]{(test.explain != null ? $", {test.explain}" : " (no further explanation)")}");
                Console.WriteLine($"Result (R0) = {result} [{result.GetType().Name}]");

                if (result is IComparable && test.result is IComparable)
                {
                    try
                    {
                        Console.WriteLine(((IComparable)result).CompareTo(test.result) == 0 ? "Match" : "/!\\ Values are not equal.");
                    }
                    catch (System.Exception)
                    {
                        Console.WriteLine("Could not compare values automatically");
                    }
                }

                Console.WriteLine();
                expr.PrintDebug();
                if (simplify)
                {
                    Console.WriteLine("Showing simplified version.");
                }
            } while ((key = Console.ReadKey(true)).Key != ConsoleKey.Escape);
        }