public static object Evaluate(string expression, _7Sharp.AdditionalFunctionEventHandler handler) { ExpressionEval expressionEval = new ExpressionEval(expression); expressionEval.AdditionalFunctionEventHandler += handler; return(expressionEval.Evaluate()); }
private object EvaluateList() { ArrayList expressionlist = (ArrayList)this._expressionlist.Clone(); for (int index = 0; index < expressionlist.Count; ++index) { if (expressionlist[index] is ExpressionEval.UnaryOp) { expressionlist[index] = ExpressionEval.PerformUnaryOp((ExpressionEval.UnaryOp)expressionlist[index], expressionlist[index + 1]); expressionlist.RemoveAt(index + 1); } } ExpressionEval.BinaryOpQueue binaryOpQueue = new ExpressionEval.BinaryOpQueue(expressionlist); string str = ""; int index1 = 1; while (index1 < expressionlist.Count) { if (expressionlist[index1] is ExpressionEval.BinaryOp) { if (index1 + 1 == expressionlist.Count) { throw new ArgumentException("Expression cannot end in a binary operation: [" + expressionlist[index1].ToString() + "]"); } } else { str += string.Format("\n{0} [?] {1}", expressionlist[index1 - 1] is string?(object)("\"" + expressionlist[index1 - 1] + "\"") : expressionlist[index1 - 1], expressionlist[index1] is string?(object)("\"" + expressionlist[index1] + "\"") : expressionlist[index1]); --index1; } index1 += 2; } if (str != "") { throw new ArgumentException("Missing binary operator: " + str); } for (ExpressionEval.BinaryOp binaryOp = binaryOpQueue.Dequeue(); binaryOp != null; binaryOp = binaryOpQueue.Dequeue()) { int index2 = expressionlist.IndexOf((object)binaryOp); expressionlist[index2 - 1] = ExpressionEval.PerformBinaryOp((ExpressionEval.BinaryOp)expressionlist[index2], expressionlist[index2 - 1], expressionlist[index2 + 1]); expressionlist.RemoveAt(index2); expressionlist.RemoveAt(index2); } return(!(expressionlist[0] is IExpression) ? expressionlist[0] : ((IExpression)expressionlist[0]).Evaluate()); }
private static object PerformBinaryOp(ExpressionEval.BinaryOp op, object v1, object v2) { IExpression expression1 = v1 as IExpression; if (expression1 != null) { v1 = expression1.Evaluate(); } IExpression expression2 = v2 as IExpression; if (expression2 != null) { v2 = expression2.Evaluate(); } switch (op.Op) { case "!=": case "+": case "-": case "<": case "<=": case "=": case "==": case ">": case ">=": return(ExpressionEval.DoSpecialOperator(op, v1, v2)); case "%": return((object)(Convert.ToInt64(v1, (IFormatProvider)CultureInfo.CurrentCulture) % Convert.ToInt64(v2, (IFormatProvider)CultureInfo.CurrentCulture))); case "&": return((object)(ulong)((long)Convert.ToUInt64(v1, (IFormatProvider)CultureInfo.CurrentCulture) & (long)Convert.ToUInt64(v2, (IFormatProvider)CultureInfo.CurrentCulture))); case "&&": return((object)(bool)(!Convert.ToBoolean(v1, (IFormatProvider)CultureInfo.CurrentCulture) ? false : (Convert.ToBoolean(v2, (IFormatProvider)CultureInfo.CurrentCulture) ? true : false))); case "*": return((object)(Convert.ToDouble(v1, (IFormatProvider)CultureInfo.CurrentCulture) * Convert.ToDouble(v2, (IFormatProvider)CultureInfo.CurrentCulture))); case "/": return((object)(Convert.ToDouble(v1, (IFormatProvider)CultureInfo.CurrentCulture) / Convert.ToDouble(v2, (IFormatProvider)CultureInfo.CurrentCulture))); case "<<": return((object)(Convert.ToInt64(v1, (IFormatProvider)CultureInfo.CurrentCulture) << Convert.ToInt32(v2, (IFormatProvider)CultureInfo.CurrentCulture))); case ">>": return((object)(Convert.ToInt64(v1, (IFormatProvider)CultureInfo.CurrentCulture) >> Convert.ToInt32(v2, (IFormatProvider)CultureInfo.CurrentCulture))); case "^": return((object)(ulong)((long)Convert.ToUInt64(v1, (IFormatProvider)CultureInfo.CurrentCulture) ^ (long)Convert.ToUInt64(v2, (IFormatProvider)CultureInfo.CurrentCulture))); case "|": return((object)(ulong)((long)Convert.ToUInt64(v1, (IFormatProvider)CultureInfo.CurrentCulture) | (long)Convert.ToUInt64(v2, (IFormatProvider)CultureInfo.CurrentCulture))); case "||": return((object)(bool)(Convert.ToBoolean(v1, (IFormatProvider)CultureInfo.CurrentCulture) ? true : (Convert.ToBoolean(v2, (IFormatProvider)CultureInfo.CurrentCulture) ? true : false))); default: throw new ArgumentException("Binary Operator " + op.Op + "not defined."); } }
private int NextToken(int nIdx) { Match match1 = (Match)null; object obj = (object)null; Match match2 = DefinedRegex.WhiteSpace.Match(this.Expression, nIdx); if (match2.Success && match2.Index == nIdx) { return(nIdx + match2.Length); } Match match3 = DefinedRegex.Parenthesis.Match(this.Expression, nIdx); if (match3.Success) { match1 = match3; } if (match1 == null || match1.Index > nIdx) { Match match4 = DefinedRegex.Function.Match(this.Expression, nIdx); if (match4.Success && (match1 == null || match4.Index < match1.Index)) { match1 = match4; } } if (match1 == null || match1.Index > nIdx) { Match match4 = DefinedRegex.Variable.Match(this.Expression, nIdx); if (match4.Success && (match1 == null || match4.Index < match1.Index)) { match1 = match4; obj = (object)new ExpressionEval.Variable(match4.Groups["Variable"].Value, this._variables); } } if (match1 == null || match1.Index > nIdx) { Match match4 = DefinedRegex.UnaryOp.Match(this.Expression, nIdx); if (match4.Success && (match1 == null || match4.Index < match1.Index)) { match1 = match4; obj = (object)new ExpressionEval.UnaryOp(match4.Value); } } if (match1 == null || match1.Index > nIdx) { Match match4 = DefinedRegex.Hexadecimal.Match(this.Expression, nIdx); if (match4.Success && (match1 == null || match4.Index < match1.Index)) { match1 = match4; obj = (object)Convert.ToInt32(match4.Value, 16); } } if (match1 == null || match1.Index > nIdx) { Match match4 = DefinedRegex.Boolean.Match(this.Expression, nIdx); if (match4.Success && (match1 == null || match4.Index < match1.Index)) { match1 = match4; obj = (object)bool.Parse(match4.Value); } } if (match1 == null || match1.Index > nIdx) { Match match4 = DefinedRegex.DateTime.Match(this.Expression, nIdx); if (match4.Success && (match1 == null || match4.Index < match1.Index)) { match1 = match4; obj = (object)Convert.ToDateTime(match4.Groups["DateString"].Value, (IFormatProvider)CultureInfo.CurrentCulture); } } if (match1 == null || match1.Index > nIdx) { Match match4 = DefinedRegex.TimeSpan.Match(this.Expression, nIdx); if (match4.Success && (match1 == null || match4.Index < match1.Index)) { match1 = match4; obj = (object)new TimeSpan(int.Parse("0" + match4.Groups["Days"].Value), int.Parse(match4.Groups["Hours"].Value), int.Parse(match4.Groups["Minutes"].Value), int.Parse("0" + match4.Groups["Seconds"].Value), int.Parse("0" + match4.Groups["Milliseconds"].Value)); } } if (match1 == null || match1.Index > nIdx) { Match match4 = DefinedRegex.Numeric.Match(this.Expression, nIdx); if (match4.Success && (match1 == null || match4.Index < match1.Index)) { while (match4.Success && (match4.Value ?? "") == "") { match4 = match4.NextMatch(); } if (match4.Success) { match1 = match4; obj = (object)double.Parse(match4.Value, (IFormatProvider)CultureInfo.CurrentCulture); } } } if (match1 == null || match1.Index > nIdx) { Match match4 = DefinedRegex.String.Match(this.Expression, nIdx); if (match4.Success && (match1 == null || match4.Index < match1.Index)) { match1 = match4; obj = (object)match4.Groups["String"].Value.Replace("\\\"", "\""); } } if (match1 == null || match1.Index > nIdx) { Match match4 = DefinedRegex.BinaryOp.Match(this.Expression, nIdx); if (match4.Success && (match1 == null || match4.Index < match1.Index)) { match1 = match4; obj = (object)new ExpressionEval.BinaryOp(match4.Value); } } if (match1 == null) { throw new ArgumentException("Invalid expression construction: \"" + this.Expression + "\"."); } if (match1.Index != nIdx) { throw new ArgumentException("Invalid token in expression: [" + this.Expression.Substring(nIdx, match1.Index - nIdx).Trim() + "]"); } int index; if (match1.Value == "(" || match1.Value.StartsWith("$")) { index = match1.Index + match1.Length; int num = 1; bool flag = false; while (num > 0) { if (index >= this.Expression.Length) { throw new ArgumentException("Missing " + (flag ? "\"" : ")") + " in Expression"); } if (!flag && this.Expression[index] == ')') { --num; } if (!flag && this.Expression[index] == '(') { ++num; } if (this.Expression[index] == '"' && (index == 0 || this.Expression[index - 1] != '\\')) { flag = !flag; } ++index; } if (match1.Value == "(") { ExpressionEval expressionEval = new ExpressionEval(this.Expression.Substring(match1.Index + 1, index - match1.Index - 2)); // ISSUE: reference to a compiler-generated field if (this.AdditionalFunctionEventHandler != null) { // ISSUE: reference to a compiler-generated field expressionEval.AdditionalFunctionEventHandler += this.AdditionalFunctionEventHandler; } expressionEval._variables = this._variables; this._expressionlist.Add((object)expressionEval); } else { FunctionEval functionEval = new FunctionEval(this.Expression.Substring(match1.Index, index - match1.Index)); // ISSUE: reference to a compiler-generated field if (this.AdditionalFunctionEventHandler != null) { // ISSUE: reference to a compiler-generated field functionEval.AdditionalFunctionEventHandler += this.AdditionalFunctionEventHandler; } functionEval._variables = this._variables; this._expressionlist.Add((object)functionEval); } } else { index = match1.Index + match1.Length; this._expressionlist.Add(obj); } return(index); }
private object ExecuteFunction(string name, object[] p) { object[] parameters = (object[])null; if (p != null) { parameters = (object[])p.Clone(); for (int index = 0; index < parameters.Length; ++index) { if (parameters[index] is IExpression) { parameters[index] = ((IExpression)parameters[index]).Evaluate(); } } } switch (name.ToLower(CultureInfo.CurrentCulture)) { case "abs": return((object)Math.Abs(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture))); case "acos": return((object)Math.Acos(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture))); case "adddays": return((object)Convert.ToDateTime(parameters[0]).AddDays((double)Convert.ToInt32(parameters[1]))); case "addhours": return((object)Convert.ToDateTime(parameters[0]).AddHours((double)Convert.ToInt32(parameters[1]))); case "addminutes": return((object)Convert.ToDateTime(parameters[0]).AddMinutes((double)Convert.ToInt32(parameters[1]))); case "addmonths": return((object)Convert.ToDateTime(parameters[0]).AddMonths(Convert.ToInt32(parameters[1]))); case "addseconds": return((object)Convert.ToDateTime(parameters[0]).AddSeconds((double)Convert.ToInt32(parameters[1]))); case "addyears": return((object)Convert.ToDateTime(parameters[0]).AddYears(Convert.ToInt32(parameters[1]))); case "asin": return((object)Math.Asin(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture))); case "atan": return((object)Math.Atan(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture))); case "case": return(FunctionEval.Case(parameters)); case "cdatetime": return((object)Convert.ToDateTime(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture)); case "cdbl": return((object)Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture)); case "ciel": return((object)Math.Ceiling(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture))); case "cint": return((object)Convert.ToInt32(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture)); case "clong": return((object)Convert.ToInt64(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture)); case "cos": return((object)Math.Cos(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture))); case "cosh": return((object)Math.Cosh(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture))); case "cot": return((object)(1.0 / Math.Tan(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture)))); case "csc": return((object)(1.0 / Math.Sin(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture)))); case "cuint": return((object)Convert.ToUInt32(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture)); case "culong": return((object)Convert.ToUInt64(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture)); case "currentuserid": return((object)WindowsIdentity.GetCurrent().Name.ToLower()); case "e": return((object)Math.E); case "exp": return((object)Math.Exp(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture))); case "expr": ExpressionEval expressionEval = new ExpressionEval(string.Concat(parameters[0])); // ISSUE: reference to a compiler-generated field expressionEval.AdditionalFunctionEventHandler += this.AdditionalFunctionEventHandler; expressionEval._variables = this._variables; return(expressionEval.Evaluate()); case "floor": return((object)Math.Floor(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture))); case "fmtdate": return((object)Convert.ToDateTime(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture).ToString(string.Concat(parameters[1]), (IFormatProvider)CultureInfo.CurrentCulture)); case "fmtnum": return((object)Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture).ToString(string.Concat(parameters[1]), (IFormatProvider)CultureInfo.CurrentCulture)); case "iif": return(FunctionEval.Iif(parameters)); case "log": return((object)(parameters.Length > 1 ? Math.Log(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture), Convert.ToDouble(parameters[1], (IFormatProvider)CultureInfo.CurrentCulture)) : Math.Log(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture)))); case "log10": return((object)Math.Log10(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture))); case "max": return((object)Math.Max(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture), Convert.ToDouble(parameters[1], (IFormatProvider)CultureInfo.CurrentCulture))); case "maxdate": return((object)DateTime.MaxValue); case "min": return((object)Math.Min(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture), Convert.ToDouble(parameters[1], (IFormatProvider)CultureInfo.CurrentCulture))); case "mindate": return((object)DateTime.MinValue); case "monthname": return((object)new DateTime(2000, Convert.ToInt32(parameters[0]), 1).ToString("MMMM")); case "now": return((object)DateTime.Now); case "pi": return((object)Math.PI); case "pow": return((object)Math.Pow(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture), Convert.ToDouble(parameters[1], (IFormatProvider)CultureInfo.CurrentCulture))); case "round": return((object)(parameters.Length > 1 ? Math.Round(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture), Convert.ToInt32(parameters[1], (IFormatProvider)CultureInfo.CurrentCulture)) : Math.Round(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture)))); case "sec": return((object)(1.0 / Math.Cos(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture)))); case "sin": return((object)Math.Sin(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture))); case "sinh": return((object)Math.Sinh(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture))); case "sqrt": return((object)Math.Sqrt(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture))); case "str": return((object)parameters[0].ToString()); case "tan": return((object)Math.Tan(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture))); case "tanh": return((object)Math.Tanh(Convert.ToDouble(parameters[0], (IFormatProvider)CultureInfo.CurrentCulture))); case "today": return((object)DateTime.Today); default: return(this.AdditionalFunctionHelper(name, parameters)); } }
private object[] GetParameters(Match m) { int index1 = m.Index + m.Length; int num = 1; int startIndex = 0; bool flag1 = false; ArrayList arrayList = new ArrayList(); while (num > 0) { if (index1 >= this.Expression.Length) { throw new ArgumentException("Missing ')' in Expression"); } if (!flag1 && this.Expression[index1] == ')') { --num; } if (!flag1 && this.Expression[index1] == '(') { ++num; } if (this.Expression[index1] == '"' && (index1 == 0 || this.Expression[index1 - 1] != '\\')) { flag1 = !flag1; } if (num > 0) { ++index1; } } string str = this.Expression.Substring(m.Index + m.Length, index1 - (m.Index + m.Length)); if ((str ?? "") == "") { return((object[])null); } bool flag2 = false; int index2; for (index2 = 0; index2 < str.Length; ++index2) { if (!flag2 && str[index2] == ')') { --num; } if (!flag2 && str[index2] == '(') { ++num; } if (str[index2] == '"' && (index2 == 0 || str[index2 - 1] != '\\')) { flag2 = !flag2; } if (!flag2 && num == 0 && str[index2] == ',') { arrayList.Add((object)str.Substring(startIndex, index2 - startIndex)); startIndex = index2 + 1; } } arrayList.Add((object)str.Substring(startIndex, index2 - startIndex)); for (int index3 = 0; index3 < arrayList.Count; ++index3) { ExpressionEval expressionEval = new ExpressionEval(arrayList[index3].ToString()); // ISSUE: reference to a compiler-generated field expressionEval.AdditionalFunctionEventHandler += this.AdditionalFunctionEventHandler; expressionEval._variables = this._variables; arrayList[index3] = (object)expressionEval; } return(arrayList.ToArray()); }
public BinaryOp(string strOp) { this._strOp = strOp; this._nPrecedence = ExpressionEval.OperatorPrecedence(strOp); }