IValue Parse(string aExpression) { aExpression = aExpression.Trim(); int index = aExpression.IndexOf('('); while (index >= 0) { SubstitudeBracket(ref aExpression, index); index = aExpression.IndexOf('('); } if (aExpression.Contains(',')) { string[] parts = aExpression.Split(','); List<IValue> exp = new List<IValue>(parts.Length); for (int i = 0; i < parts.Length; i++) { string s = parts[i].Trim(); if (!string.IsNullOrEmpty(s)) exp.Add(Parse(s)); } return new MultiParameterList(exp.ToArray()); } else if (aExpression.Contains('+')) { string[] parts = aExpression.Split('+'); List<IValue> exp = new List<IValue>(parts.Length); for (int i = 0; i < parts.Length; i++) { string s = parts[i].Trim(); if (!string.IsNullOrEmpty(s)) exp.Add(Parse(s)); } if (exp.Count == 1) return exp[0]; return new OperationSum(exp.ToArray()); } else if (aExpression.Contains('-')) { string[] parts = aExpression.Split('-'); List<IValue> exp = new List<IValue>(parts.Length); if (!string.IsNullOrEmpty(parts[0].Trim())) exp.Add(Parse(parts[0])); for (int i = 1; i < parts.Length; i++) { string s = parts[i].Trim(); if (!string.IsNullOrEmpty(s)) exp.Add(new OperationNegate(Parse(s))); } if (exp.Count == 1) return exp[0]; return new OperationSum(exp.ToArray()); } else if (aExpression.Contains('*')) { string[] parts = aExpression.Split('*'); List<IValue> exp = new List<IValue>(parts.Length); for (int i = 0; i < parts.Length; i++) { exp.Add(Parse(parts[i])); } if (exp.Count == 1) return exp[0]; return new OperationProduct(exp.ToArray()); } else if (aExpression.Contains('/')) { string[] parts = aExpression.Split('/'); List<IValue> exp = new List<IValue>(parts.Length); if (!string.IsNullOrEmpty(parts[0].Trim())) exp.Add(Parse(parts[0])); for (int i = 1; i < parts.Length; i++) { string s = parts[i].Trim(); if (!string.IsNullOrEmpty(s)) exp.Add(new OperationReciprocal(Parse(s))); } return new OperationProduct(exp.ToArray()); } else if (aExpression.Contains('^')) { int pos = aExpression.IndexOf('^'); var val = Parse(aExpression.Substring(0, pos)); var pow = Parse(aExpression.Substring(pos + 1)); return new OperationPower(val, pow); } foreach (var M in m_Funcs) { if (aExpression.StartsWith(M.Key)) { var inner = aExpression.Substring(M.Key.Length); var param = Parse(inner); var multiParams = param as MultiParameterList; IValue[] parameters; if (multiParams != null) parameters = multiParams.Parameters; else parameters = new IValue[] { param }; return new CustomFunction(M.Key, M.Value, parameters); } } foreach (var C in m_Consts) { if (aExpression.StartsWith(C.Key)) { return new CustomFunction(C.Key, (p) => C.Value(), null); } } int index2a = aExpression.IndexOf('&'); int index2b = aExpression.IndexOf(';'); if (index2a >= 0 && index2b >= 2) { var inner = aExpression.Substring(index2a + 1, index2b - index2a - 1); int bracketIndex; if (int.TryParse(inner, out bracketIndex) && bracketIndex >= 0 && bracketIndex < m_BracketHeap.Count) { return Parse(m_BracketHeap[bracketIndex]); } else throw new ParseException("Can't parse substitude token"); } double doubleValue; if (double.TryParse(aExpression, out doubleValue)) { return new Number(doubleValue); } if (ValidIdentifier(aExpression)) { if (m_Context.Parameters.ContainsKey(aExpression)) return m_Context.Parameters[aExpression]; var val = new Parameter(aExpression); m_Context.Parameters.Add(aExpression, val); return val; } throw new ParseException("Reached unexpected end within the parsing tree"); }
double[] InvokeMultiResult(double[] aParams, Parameter[] aParamList) { int count = System.Math.Min(aParamList.Length, aParams.Length); for (int i = 0; i < count; i++) { if (aParamList[i] != null) aParamList[i].Value = aParams[i]; } return MultiValue; }