Пример #1
0
        /// <summary>
        /// Executes the function based upon the name of the function
        /// </summary>
        /// <param name="name">name of the function to execute</param>
        /// <param name="p">parameter list</param>
        /// <returns>returned value of executed function</returns>
        //[SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        private object ExecuteFunction(string name, object[] p)
        {
            object[] parameters = null;
            if (p != null)
            {
                parameters = (object[])p.Clone();
                for (int x = 0; x < parameters.Length; x++)
                    if (parameters[x] is IExpression)
                        parameters[x] = ((IExpression)parameters[x]).Evaluate();
            }
            switch (name.ToLower(CultureInfo.CurrentCulture))
            {
                // Math functions
                case "sin":     return Math.Sin(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "cos":     return Math.Cos(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "tan":     return Math.Tan(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "sec":     return 1 / Math.Cos(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "csc":     return 1 / Math.Sin(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "cot":     return 1 / Math.Tan(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "asin":    return Math.Asin(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "acos":    return Math.Acos(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "atan":    return Math.Atan(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "sinh":    return Math.Sinh(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "cosh":    return Math.Cosh(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "tanh":    return Math.Tanh(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "abs":     return Math.Abs(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "sqrt":    return Math.Sqrt(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "ciel":    return Math.Ceiling(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "floor":   return Math.Floor(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "exp":     return Math.Exp(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "log10":   return Math.Log10(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "log":     return (parameters.Length > 1) ?
                                        Math.Log(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture), Convert.ToDouble(parameters[1], CultureInfo.CurrentCulture)) :
                                        Math.Log(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "max":     return Math.Max(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture), Convert.ToDouble(parameters[1], CultureInfo.CurrentCulture));
                case "min":     return Math.Min(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture), Convert.ToDouble(parameters[1], CultureInfo.CurrentCulture));
                case "pow":     return Math.Pow(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture), Convert.ToDouble(parameters[1], CultureInfo.CurrentCulture));
                case "round":   return (parameters.Length > 1) ?
                                        Math.Round(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture), Convert.ToInt32(parameters[1], CultureInfo.CurrentCulture)) :
                                        Math.Round(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                //case "trunc":   return Math.Truncate(Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture));
                case "e":       return Math.E;
                case "pi":      return Math.PI;

                //DateTime functions
                case "now":     return DateTime.Now;
                case "today":   return DateTime.Today;
                case "mindate": return DateTime.MinValue;
                case "maxdate": return DateTime.MaxValue;
                case "monthname": return (new DateTime(2000, Convert.ToInt32(parameters[0]), 1)).ToString("MMMM");
                case "adddays": return Convert.ToDateTime(parameters[0]).AddDays(Convert.ToInt32(parameters[1]));
                case "addmonths": return Convert.ToDateTime(parameters[0]).AddMonths(Convert.ToInt32(parameters[1]));
                case "addyears": return Convert.ToDateTime(parameters[0]).AddYears(Convert.ToInt32(parameters[1]));
                case "addhours": return Convert.ToDateTime(parameters[0]).AddHours(Convert.ToInt32(parameters[1]));
                case "addminutes": return Convert.ToDateTime(parameters[0]).AddMinutes(Convert.ToInt32(parameters[1]));
                case "addseconds": return Convert.ToDateTime(parameters[0]).AddSeconds(Convert.ToInt32(parameters[1]));

                //Formatting Functions
                case "fmtnum":  return Convert.ToDouble(parameters[0], CultureInfo.CurrentCulture).ToString("" + parameters[1], CultureInfo.CurrentCulture);
                case "fmtdate": return Convert.ToDateTime(parameters[0], CultureInfo.CurrentCulture).ToString("" + parameters[1], CultureInfo.CurrentCulture);

                //Numerical Expression Evaluation
                case "expr":
                    {
                        ExpressionEval eval = new ExpressionEval("" + parameters[0]);
                        eval.AdditionalFunctionEventHandler += this.AdditionalFunctionEventHandler;
                        eval._variables = this._variables;
                        return eval.Evaluate();
                    }

                //Casting Functions
                case "cdbl":    return Convert.ToDouble (parameters[0], CultureInfo.CurrentCulture);
                case "cint":    return Convert.ToInt32  (parameters[0], CultureInfo.CurrentCulture);
                case "clong":   return Convert.ToInt64  (parameters[0], CultureInfo.CurrentCulture);
                case "cuint":   return Convert.ToUInt32 (parameters[0], CultureInfo.CurrentCulture);
                case "culong":  return Convert.ToUInt64 (parameters[0], CultureInfo.CurrentCulture);
                case "cdatetime": return Convert.ToDateTime(parameters[0], CultureInfo.CurrentCulture);
                case "str":     return parameters[0].ToString();

                //Logical Functions
                case "iif":     return Iif(parameters);
                case "case":    return Case(parameters);

                //security fucntions
                case "currentuserid": return WindowsIdentity.GetCurrent().Name.ToLower()                                                                        ;

                default:        return AdditionalFunctionHelper(name, parameters);
            }
        }
Пример #2
0
 /// <summary>
 /// Static version of the Expression Evaluator
 /// </summary>
 /// <param name="expression">expression to be evaluated</param>
 /// <param name="handler">attach a custom function handler</param>
 /// <returns></returns>
 public static object Evaluate(string expression, AdditionalFunctionEventHandler handler)
 {
     ExpressionEval expr = new ExpressionEval(expression);
     expr.AdditionalFunctionEventHandler += handler;
     return expr.Evaluate();
 }
Пример #3
0
        private static bool performComparison(string[] values)
        {
            string comparisonString = "";
            string tempVar;
            bool errorFlag = false;

            for (int i = 0; i < values.Length; i++)
            {
            //Console.WriteLine(values[i]);
                 if (values[i].Equals(".lt.")) { values[i] = " < "; }
            else if (values[i].Equals(".gt.")) { values[i] = " > "; }
            else if (values[i].Equals(".eq.")) { values[i] = " == "; }
            else if (values[i].Equals(".le.")) { values[i] = " <= "; }
            else if (values[i].Equals(".ge.")) { values[i] = " >= "; }
            else if (values[i].Equals(".ne.")) { values[i] = " != "; }
            else if (values[i].Equals(".and.")) { values[i] = " && "; }
            else if (values[i].Equals(".or.")) { values[i] = " || "; }
            else if (values[i].Equals(".not.")) { values[i] = " ! "; }
            else if (isVariableName(values[i])) {
                if (variables.ContainsKey(values[i])) { variables.TryGetValue(values[i], out tempVar); values[i] = tempVar; }
                else { errorFlag = true; }
            }
                 else if (isString(values[i])) { values[i] = values[i].Replace('"', '\''); } //.Trim('\'')
                 //Console.WriteLine(values[i]);
            }

            foreach (string s in values) { comparisonString += s; }
            //Console.WriteLine(comparisonString);

            if (!errorFlag)
            {

            ExpressionEval expr = new ExpressionEval(comparisonString);
            object result = expr.Evaluate();
            //Console.WriteLine(result);
            return (bool)result;
            }
            else
            {
            return false;
            }
        }
Пример #4
0
 /// <summary>
 /// Static version of the Expression Evaluator
 /// </summary>
 /// <param name="expression">expression to be evaluated</param>
 /// <returns></returns>
 public static object Evaluate(string expressionString)
 {
     ExpressionEval expression = new ExpressionEval(expressionString);
     return expression.Evaluate();
 }