/// <summary> /// Evaluates a string expression of a function /// </summary> /// <param name="expression"></param> /// <param name="handler">attach a custom function handler</param> /// <returns>evauluated value</returns> public static object Evaluate(string expression, AdditionalFunctionEventHandler handler) { FunctionEval expr = new FunctionEval(expression); if (handler != null) { expr.AdditionalFunctionEventHandler += handler; } return(expr.Evaluate()); }
/// <summary> /// This routine will replace functions existing in the Expression property with thier respective values /// </summary> /// <returns>Expression string with all found functions replaced with returned values</returns> public string Replace() { StringBuilder strbRet = new StringBuilder(Expression); Match m = DefinedRegex.Function.Match(Expression); while (m.Success) { int nDepth = 1; int nIdx = m.Index + m.Length; //Get the parameter string while (nDepth > 0) { if (nIdx >= strbRet.Length) { throw new ArgumentException("Missing ')' in Expression"); } if (strbRet[nIdx] == ')') { nDepth--; } if (strbRet[nIdx] == '(') { nDepth++; } nIdx++; } string expression = strbRet.ToString(m.Index, nIdx - m.Index); FunctionEval eval = new FunctionEval(expression); eval.AdditionalFunctionEventHandler += this.AdditionalFunctionEventHandler; eval._variables = this._variables; strbRet.Replace(expression, "" + eval.Evaluate()); m = DefinedRegex.Function.Match(strbRet.ToString()); } //Replace Variable in the path! m = DefinedRegex.Variable.Match(strbRet.ToString()); while (m.Success) { strbRet.Replace(m.Value, "" + _variables[m.Groups["Variable"].Value]); m = DefinedRegex.Variable.Match(strbRet.ToString()); } return(strbRet.ToString()); }
/// <summary> /// Evaluates a string expression of a function /// </summary> /// <param name="expression"></param> /// <returns>evauluated value</returns> public static object Evaluate(string expression) { FunctionEval expr = new FunctionEval(expression); return(expr.Evaluate()); }