Beispiel #1
0
        public static string Replace(string input, _7Sharp.AdditionalFunctionEventHandler handler)
        {
            FunctionEval functionEval = new FunctionEval(input);

            if (handler != null)
            {
                functionEval.AdditionalFunctionEventHandler += handler;
            }
            return(functionEval.Replace());
        }
Beispiel #2
0
        public static object Evaluate(string expression, _7Sharp.AdditionalFunctionEventHandler handler)
        {
            FunctionEval functionEval = new FunctionEval(expression);

            if (handler != null)
            {
                functionEval.AdditionalFunctionEventHandler += handler;
            }
            return(functionEval.Evaluate());
        }
Beispiel #3
0
        public string Replace()
        {
            StringBuilder stringBuilder = new StringBuilder(this.Expression);

            for (Match match = DefinedRegex.Function.Match(this.Expression); match.Success; match = DefinedRegex.Function.Match(stringBuilder.ToString()))
            {
                int num   = 1;
                int index = match.Index + match.Length;
                while (num > 0)
                {
                    if (index >= stringBuilder.Length)
                    {
                        throw new ArgumentException("Missing ')' in Expression");
                    }
                    if (stringBuilder[index] == ')')
                    {
                        --num;
                    }
                    if (stringBuilder[index] == '(')
                    {
                        ++num;
                    }
                    ++index;
                }
                string       str          = stringBuilder.ToString(match.Index, index - match.Index);
                FunctionEval functionEval = new FunctionEval(str);
                // ISSUE: reference to a compiler-generated field
                functionEval.AdditionalFunctionEventHandler += this.AdditionalFunctionEventHandler;
                functionEval._variables = this._variables;
                stringBuilder.Replace(str, string.Concat(functionEval.Evaluate()));
            }
            for (Match match = DefinedRegex.Variable.Match(stringBuilder.ToString()); match.Success; match = DefinedRegex.Variable.Match(stringBuilder.ToString()))
            {
                stringBuilder.Replace(match.Value, string.Concat(this._variables[(object)match.Groups["Variable"].Value]));
            }
            return(stringBuilder.ToString());
        }
Beispiel #4
0
        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);
        }
Beispiel #5
0
        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));
            }
        }