protected override Parser.Result Evaluate(string data, ref int from) { // 1. Get the name of the variable. string varName = Utils.GetToken(data, ref from, Constants.NEXT_ARG_ARRAY); if (from >= data.Length) { throw new ArgumentException("Couldn't append variable"); } // 2. Get the current value of the variable. ParserFunction func = ParserFunction.GetFunction(varName); Parser.Result currentValue = func.GetValue(data, ref from); // 3. Get the value to be looked for. Parser.Result searchValue = Utils.GetItem(data, ref from); // 4. Take either the string part if it is defined, // or the numerical part converted to a string otherwise. string basePart = currentValue.String != null ? currentValue.String : currentValue.Value.ToString(); string search = searchValue.String != null ? searchValue.String : searchValue.Value.ToString(); int result = basePart.IndexOf(search); return(new Parser.Result(result, null)); }
protected override Parser.Result Evaluate(string data, ref int from) { // 1. Get the name of the variable. string varName = Utils.GetToken(data, ref from, Constants.NEXT_ARG_ARRAY); if (from >= data.Length) { throw new ArgumentException("Couldn't append variable"); } // 2. Get the current value of the variable. ParserFunction func = ParserFunction.GetFunction(varName); Parser.Result currentValue = func.GetValue(data, ref from); // 3. Get the value to be added or appended. Parser.Result newValue = Utils.GetItem(data, ref from); // 4. Take either the string part if it is defined, // or the numerical part converted to a string otherwise. string arg1 = currentValue.String != null ? currentValue.String : currentValue.Value.ToString(); string arg2 = newValue.String != null ? newValue.String : newValue.Value.ToString(); // 5. The variable becomes a string after adding a string to it. newValue.Reset(); newValue.String = arg1 + arg2; ParserFunction.AddFunction(varName, new GetVarFunction(newValue)); return(newValue); }
protected override Parser.Result Evaluate(string data, ref int from) { // 1. Get the name of the variable. string varName = Utils.GetToken(data, ref from, Constants.END_ARG_ARRAY); if (from >= data.Length) { throw new ArgumentException("Couldn't get variable"); } // 2. Get the current value of the variable. ParserFunction func = ParserFunction.GetFunction(varName); Parser.Result currentValue = func.GetValue(data, ref from); // 3. Take either the length of the underlying tuple or // string part if it is defined, // or the numerical part converted to a string otherwise. int size = currentValue.Tuple != null ? currentValue.Tuple.Count : currentValue.String != null ? currentValue.String.Length : currentValue.Value.ToString().Length; Parser.Result newValue = new Parser.Result(size); return(newValue); }
public static Result LoadAndCalculate(string data, ref int from, char[] to) { if (from >= data.Length || to.Contains(data[from])) { return(new Result()); } List <Cell> listToMerge = new List <Cell>(16); StringBuilder item = new StringBuilder(); do { // Main processing cycle of the first part. char ch = data[from++]; if (StillCollecting(item.ToString(), ch, to)) { // The char still belongs to the previous operand. item.Append(ch); if (from < data.Length && !to.Contains(data[from])) { continue; } } // We are done getting the next token. The getValue() call below may // recursively call loadAndCalculate(). This will happen if extracted // item is a function or if the next item is starting with a START_ARG '('. string parsingItem = item.ToString(); ParserFunction func = new ParserFunction(data, ref from, parsingItem, ch); Result current = func.GetValue(data, ref from); if (Double.IsNaN(current.Value)) { // If there is no numerical result, we are not in a math expression. return(current); } char action = ValidAction(ch) ? ch : UpdateAction(data, ref from, ch, to); listToMerge.Add(new Cell(current.Value, action)); item.Clear(); } while (from < data.Length && !to.Contains(data[from])); if (from < data.Length && data[from] == Constants.END_ARG) { // This happens when called recursively inside of the math expression: // move one char forward. from++; } Cell baseCell = listToMerge[0]; int index = 1; Result result = new Result(); result.Value = Merge(baseCell, ref index, listToMerge); return(result); }
protected override Parser.Result Evaluate(string data, ref int from) { string substring; // 1. Get the name of the variable. string varName = Utils.GetToken(data, ref from, Constants.NEXT_ARG_ARRAY); if (from >= data.Length) { throw new ArgumentException("Couldn't get variable"); } // 2. Get the current value of the variable. ParserFunction func = ParserFunction.GetFunction(varName); Parser.Result currentValue = func.GetValue(data, ref from); // 3. Take either the string part if it is defined, // or the numerical part converted to a string otherwise. string arg = currentValue.String != null ? currentValue.String : currentValue.Value.ToString(); // 4. Get the initial index of the substring. bool lengthAvailable = Utils.SeparatorExists(data, from); Parser.Result init = Utils.GetItem(data, ref from, true /* expectInt */); // 5. Get the length of the substring if available. if (lengthAvailable) { Parser.Result length = Utils.GetItem(data, ref from, true /* expectInt */); if (init.Value + length.Value > arg.Length) { throw new ArgumentException("The total substring length is larger than [" + arg + "]"); } substring = arg.Substring((int)init.Value, (int)length.Value); } else { substring = arg.Substring((int)init.Value); } Parser.Result newValue = new Parser.Result(Double.NaN, substring); return(newValue); }