Example #1
0
 public CustomFunction(Func <ParameterList, double> aFunc, ParameterList aParams)
 {
     m_Func   = aFunc;
     m_Params = aParams;
 }
Example #2
0
        } // ParseLogicResult(string, int)

        private INumberProvider ParseNumber(string aExpression, int aMaxRecursion)
        {
            --aMaxRecursion;
            m_ParsingContext.Preprocess(this, ref aExpression);
            aExpression = aExpression.Trim();
            if (aExpression.Contains(","))
            {
                string[] parts = aExpression.Split(',');

                var paramList = new ParameterList();
                paramList.inputs.Capacity = parts.Length;
                for (int i = 0; i < parts.Length; i++)
                {
                    string s = parts[i].Trim();
                    if (!string.IsNullOrEmpty(s))
                    {
                        paramList.inputs.Add(ParseNumber(s, aMaxRecursion));
                    }
                }
                if (paramList.inputs.Count == 1)
                {
                    return(paramList.inputs[0]);
                }
                return(paramList);
            }
            else if (aExpression.Contains("+"))
            {
                string[] parts             = aExpression.Split('+');
                List <INumberProvider> exp = new List <INumberProvider>(parts.Length);
                for (int i = 0; i < parts.Length; i++)
                {
                    string s = parts[i].Trim();
                    if (!string.IsNullOrEmpty(s))
                    {
                        exp.Add(ParseNumber(s, aMaxRecursion));
                    }
                }
                if (exp.Count == 1)
                {
                    return(exp[0]);
                }
                return(new OperationAdd(exp.ToArray()));
            }
            else if (aExpression.Contains("-"))
            {
                string[] parts             = aExpression.Split('-');
                List <INumberProvider> exp = new List <INumberProvider>(parts.Length);
                if (!string.IsNullOrEmpty(parts[0].Trim()))
                {
                    exp.Add(ParseNumber(parts[0], aMaxRecursion));
                }
                for (int i = 1; i < parts.Length; i++)
                {
                    string s = parts[i].Trim();
                    if (!string.IsNullOrEmpty(s))
                    {
                        exp.Add(new OperationNegate(ParseNumber(s, aMaxRecursion)));
                    }
                }
                if (exp.Count == 1)
                {
                    return(exp[0]);
                }
                return(new OperationAdd(exp.ToArray()));
            }
            else if (aExpression.Contains("*"))
            {
                string[] parts             = aExpression.Split('*');
                List <INumberProvider> exp = new List <INumberProvider>(parts.Length);
                for (int i = 0; i < parts.Length; i++)
                {
                    exp.Add(ParseNumber(parts[i], aMaxRecursion));
                }
                if (exp.Count == 1)
                {
                    return(exp[0]);
                }
                return(new OperationProduct(exp.ToArray()));
            }
            else if (aExpression.Contains("/"))
            {
                string[] parts             = aExpression.Split('/');
                List <INumberProvider> exp = new List <INumberProvider>(parts.Length);
                if (!string.IsNullOrEmpty(parts[0].Trim()))
                {
                    exp.Add(ParseNumber(parts[0], aMaxRecursion));
                }
                for (int i = 1; i < parts.Length; i++)
                {
                    string s = parts[i].Trim();
                    if (!string.IsNullOrEmpty(s))
                    {
                        exp.Add(new OperationReciprocal(ParseNumber(s, aMaxRecursion)));
                    }
                }
                return(new OperationProduct(exp.ToArray()));
            }
            else if (aExpression.Contains("^"))
            {
                int pos = aExpression.IndexOf('^');
                var val = ParseNumber(aExpression.Substring(0, pos), aMaxRecursion);
                var pow = ParseNumber(aExpression.Substring(pos + 1), aMaxRecursion);
                return(new OperationPower(val, pow));
            }

            int p = aExpression.IndexOf("$B");

            if (p > 0)
            {
                string fName = aExpression.Substring(0, p).Trim();
                Func <ParameterList, double> func = m_ParsingContext.GetFunction(fName);
                if (func != null)
                {
                    aExpression = aExpression.Substring(p);
                    string inner = m_ParsingContext.GetBracket(ref aExpression);
                    var    param = ParseNumber(inner, aMaxRecursion);
                    if (param is ParameterList)
                    {
                        return(new CustomFunction(func, (ParameterList)param));
                    }
                    return(new CustomFunction(func, new ParameterList(param)));
                }
            }
            string bracketContent = m_ParsingContext.GetBracket(ref aExpression);

            if (!string.IsNullOrEmpty(bracketContent))
            {
                return(ParseNumber(bracketContent, aMaxRecursion));
            }
            ValueProvider value = m_ParsingContext.GetCommand(ref aExpression);

            if (value != null)
            {
                return(value);
            }

            double doubleValue;

            if (double.TryParse(aExpression.Trim(), out doubleValue))
            {
                return(new ConstantNumber {
                    constantValue = doubleValue
                });
            }

            if (ValidIdentifier(aExpression))
            {
                return(context.GetVariable(aExpression.Trim()));
            }

            if (aMaxRecursion > 0)
            {
                return new BoolToNumber {
                           val = ParseLogicResult(aExpression, aMaxRecursion)
                }
            }
            ;
            throw new ParseException("Unexpected end / expression");
        } //ParseNumber(string, int)