Ejemplo 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
 }
Ejemplo n.º 2
0
        static public string PrintToHtml(this RusticExpr expr)
        {
            string html = $"<div class=\"expr-result\">Result: {expr.stacks[0].ResultValue}</div>";

            html += "<table width=100%>";

            // Table Head
            html += "<thead>";
            html += "<tr>";
            html += "<th>Stack Register</th>";
            html += "<th width=50%>Operation</th>";
            html += "<th width=20%>Result</th>";
            html += "<th>Type</th>";
            html += "</tr>";
            html += "</thead>";

            // Table Body
            html += "<tbody>";

            foreach (var stack in expr.stacks)
            {
                bool   isFirst     = true;
                Type   prevType    = null;
                object resultValue = null;
                foreach (var operation in stack.operations)
                {
                    html += "<tr>";
                    if (isFirst)
                    {
                        // Stack Register column
                        html += $"<td rowspan='{stack.operations.Count + 1}'>R{stack.displayId}</td>";
                    }

                    Type opResultType = operation.PreviewResultType(prevType);
                    resultValue = operation.Execute(resultValue);

                    // Operations column
                    html += $"<td>{operation.GetType().Name}{(operation.parameter != null ? " " + operation.parameter : "")}</td>";

                    // Result column
                    html += $"<td>R{stack.displayId}: {resultValue}</td>";

                    // Type column
                    html += $"<td style='white-space: nowrap'>{operation.GetType().Name}({prevType?.Name ?? "null"}, {(operation.parameter != null ? " " + operation.parameterType.Name : "null")}): {opResultType.Name}</td>";

                    html    += "</tr>";
                    isFirst  = false;
                    prevType = opResultType;
                }

                html += $"<tr style='font-weight:bold'><td>Return</td><td>R{stack.displayId}: {stack.ResultValue ?? "null"}</td><td>{stack.ResultValue?.GetType().Name ?? "null"}</td></tr>";

                html += "</tr>";
            }

            html += "</tbody>";
            html += "</table>";
            return(html);
        }
Ejemplo n.º 3
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);
        }