示例#1
0
        public static void OnTxtInputTextChange(object sender, EventArgs e)
        {
            TextBox           txt  = sender as TextBox;
            Tuple <int, Type> pair = (Tuple <int, Type>)txt.Tag;

            if (txt.Text == "")      // TEXT BOX EMPTY
            {
                argBoxes[pair.Item1].LblRHS.ForeColor = Color.Gray;
                argBoxes[pair.Item1].LblRHS.Text      = pair.Item2.Name;
            }
            else
            {
                object evaluated = JSEval.Eval(txt.Text);
                if (evaluated.ToString() != "" && (evaluated.GetType().Name == pair.Item2.Name || pair.Item2 == typeof(object)))
                {
                    argBoxes[pair.Item1].LblRHS.ForeColor = Color.Gray;
                    argBoxes[pair.Item1].LblRHS.Text      = evaluated.ToString();
                    EvaluateFormula();
                }
                else
                {
                    argBoxes[pair.Item1].LblRHS.ForeColor = Color.Red;
                    argBoxes[pair.Item1].LblRHS.Text      = "Invalid Input";
                }
            }
        }
示例#2
0
        public static void EvaluateFormula()
        {
            int loopCount = argBoxes.Count;

            if (currentFunction.Arguments.Count == 0)
            {
                loopCount -= 1;
            }
            object[] args = new object[loopCount];
            for (int i = 0; i < loopCount; i++)
            {
                object value = JSEval.Eval(argBoxes[i].TxtInput.Text);
                if (value == null || value.ToString() == "")
                {
                    lblResult.Text = "Formula Result = ";
                    return;
                }
                else
                {
                    args[i] = value;
                }
            }
            dynamic func = currentFunction.BindedFunction;

            lblResult.Text = "Formula Result = " + func(args).ToString();
        }
示例#3
0
        public object Eval(Func <string, string[], object> onFormula, Dictionary <string, object> parameters, List <Tuple <FormulaNode, object> > trace = null)
        {
            object v = null;

            if (Right != null)
            {
                var r = Right.Eval(onFormula, parameters, trace);

                if (Operation == "f" && Left.Formula == "!")
                {
                    v = !Convert.ToBoolean(r);                                         //("!" + r).ToLower().GetValue<bool>();
                }
                else if (Operation == "f" && Left.Formula == "+")
                {
                    v = Convert.ToDecimal(r);
                }
                else if (Operation == "f" && Left.Formula == "-")
                {
                    v = -Convert.ToDecimal(r);
                }
                else if (Operation == "f" && onFormula != null)
                {
                    v = onFormula(Left.Formula, (r + "").Split(","));
                }
                else if (Operation == "f")
                {
                    v = new JSEval().Eval("System.Math." + CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Left.Formula) + "(" + r + ")");                       //("System.Math." + CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Left.Formula) + "(" + r + ")").GetValue<decimal>();   //hack просто Math. нельзя, т.к. будет использоваться JScript.Math
                }
                if (v == null)
                {
                    var l = Left.Eval(onFormula, parameters, trace);

                    if (Operation == "+")
                    {
                        if (r is string || l is string)
                        {
                            v = "" + l + r;
                        }
                        else
                        {
                            v = Convert.ToDecimal(l) + Convert.ToDecimal(r);
                        }
                    }
                    else if (Operation == "-")
                    {
                        v = Convert.ToDecimal(l) - Convert.ToDecimal(r);
                    }
                    else if (Operation == "*")
                    {
                        v = Convert.ToDecimal(l) * Convert.ToDecimal(r);
                    }
                    else if (Operation == "/")
                    {
                        v = Convert.ToDecimal(l) / Convert.ToDecimal(r);
                    }
                    else if (Operation == "^")
                    {
                        v = Math.Pow(Convert.ToDouble(l), Convert.ToDouble(r));
                    }
                    //для Eval сойдёт и строковое представление параметроов
                    else if (Operation == ",")
                    {
                        v = (((l is bool || l is string) ? l : Convert.ToDecimal(l).ToString(CultureInfo.InvariantCulture)) + ","
                             + ((r is bool || r is string) ? r : Convert.ToDecimal(r).ToString(CultureInfo.InvariantCulture))).ToLower();
                    }

                    if (v == null)
                    {
                        var lr = Left.GetRightVal(onFormula, parameters, trace);
                        var rl = Right.GetLeftVal(onFormula, parameters, trace);

                        if (lr is string || rl is string)
                        {
                            v = new JSEval().Eval("'" + lr + "'" + (Operation == "=" ? "==" : (Operation == "<>" ? "!=" : Operation)) + "'" + rl + "'");
                        }
                        else if (Operation == "<")
                        {
                            v = Convert.ToDecimal(lr) < Convert.ToDecimal(rl) && (!(l is bool) || (bool)l) && (!(r is bool) || (bool)r);
                        }
                        else if (Operation == ">")
                        {
                            v = Convert.ToDecimal(lr) > Convert.ToDecimal(rl) && (!(l is bool) || (bool)l) && (!(r is bool) || (bool)r);
                        }
                        else if (Operation == "<=")
                        {
                            v = Convert.ToDecimal(lr) <= Convert.ToDecimal(rl) && (!(l is bool) || (bool)l) && (!(r is bool) || (bool)r);
                        }
                        else if (Operation == ">=")
                        {
                            v = Convert.ToDecimal(lr) >= Convert.ToDecimal(rl) && (!(l is bool) || (bool)l) && (!(r is bool) || (bool)r);
                        }
                        else if (Operation == "=")
                        {
                            v = l is bool && r is bool?Convert.ToBoolean(l) == Convert.ToBoolean(r) : Convert.ToDecimal(lr) == Convert.ToDecimal(rl) && (!(l is bool) || (bool)l) && (!(r is bool) || (bool)r);
                        }
                        else if (Operation == "!=" || Operation == "<>")
                        {
                            v = l is bool && r is bool?Convert.ToBoolean(l) != Convert.ToBoolean(r) : Convert.ToDecimal(lr) != Convert.ToDecimal(rl) && (!(l is bool) || (bool)l) && (!(r is bool) || (bool)r);
                        }
                        else if (Operation == "&&" || Operation == "&")
                        {
                            v = Convert.ToBoolean(l) && Convert.ToBoolean(r);
                        }
                        else if (Operation == "||" || Operation == "|")
                        {
                            v = Convert.ToBoolean(l) || Convert.ToBoolean(r);
                        }
                    }
                }
            }
            if (v == null)
            {
                if (parameters != null && parameters.ContainsKey(Formula))
                {
                    return(parameters[Formula]);
                }
                var c = Formula[0]; if ((c == '\'' || c == '"') && Formula[Formula.Length - 1] == c)
                {
                    return(Formula.Substring(1, Formula.Length - 2));
                }
                try { return(decimal.Parse(Formula, CultureInfo.InvariantCulture)); }
                catch
                {
                    try { return(bool.Parse(Formula)); }
                    catch (Exception ex) { throw new Exception("'" + Formula + "' is undefined.", ex); }
                }
            }
            if (trace != null)
            {
                trace.Add(new Tuple <FormulaNode, object>(this, v));
            }
            return(v);
        }