protected override Variable Evaluate(ParsingScript script) { Variable varValue = Utils.GetItem(script); // Check if the variable to be set has the form of x[a][b]..., // meaning that this is an array element. List <Variable> arrayIndices = Utils.GetArrayIndices(ref m_name); if (arrayIndices.Count == 0) { ParserFunction.AddGlobalOrLocalVariable(m_name, new GetVarFunction(varValue)); return(varValue); } Variable array; ParserFunction pf = ParserFunction.GetFunction(m_name); if (pf != null) { array = pf.GetValue(script); } else { array = new Variable(); } ExtendArray(array, arrayIndices, 0, varValue); ParserFunction.AddGlobalOrLocalVariable(m_name, new GetVarFunction(array)); return(array); }
protected override Variable Evaluate(ParsingScript script) { // 1. Get the name of the variable. string varName = Utils.GetToken(script, Constants.END_ARG_ARRAY); Utils.CheckNotEnd(script, m_name); List <Variable> arrayIndices = Utils.GetArrayIndices(ref varName); // 2. Get the current value of the variable. ParserFunction func = ParserFunction.GetFunction(varName); Utils.CheckNotNull(varName, func); Variable currentValue = func.GetValue(script); Variable element = currentValue; // 2b. Special case for an array. if (arrayIndices.Count > 0)// array element { element = Utils.ExtractArrayElement(currentValue, arrayIndices); script.MoveForwardIf(Constants.END_ARRAY); } // 3. Convert type to string. string type = Constants.TypeToString(element.Type); script.MoveForwardIf(Constants.END_ARG, Constants.SPACE); Variable newValue = new Variable(type); return(newValue); }
protected override Variable Evaluate(ParsingScript script) { // First check if this element is part of an array: if (script.TryPrev() == Constants.START_ARRAY) { // There is an index given - it must be for an element of the tuple. if (m_value.Tuple == null || m_value.Tuple.Count == 0) { throw new ArgumentException("No tuple exists for the index"); } if (m_arrayIndices == null) { string startName = script.Substr(script.Pointer - 1); m_arrayIndices = Utils.GetArrayIndices(ref startName, ref m_delta); } script.Forward(m_delta); Variable result = Utils.ExtractArrayElement(m_value, m_arrayIndices); return(result); } // Otherwise just return the stored value. return(m_value); }
protected override Variable Evaluate(ParsingScript script) { // 1. Get the name of the variable. string varName = Utils.GetToken(script, Constants.NEXT_OR_END_ARRAY); Utils.CheckNotEnd(script, Constants.CONTAINS); // 2. Get the current value of the variable. List <Variable> arrayIndices = Utils.GetArrayIndices(ref varName); ParserFunction func = ParserFunction.GetFunction(varName); Utils.CheckNotNull(varName, func); Variable currentValue = func.GetValue(script); // 2b. Special dealings with arrays: Variable query = arrayIndices.Count > 0 ? Utils.ExtractArrayElement(currentValue, arrayIndices) : currentValue; // 3. Get the value to be looked for. Variable searchValue = Utils.GetItem(script); Utils.CheckNotEnd(script, Constants.CONTAINS); // 4. Check if the value to search for exists. bool exists = query.Exists(searchValue, true /* notEmpty */); script.MoveBackIf(Constants.START_GROUP); return(new Variable(exists)); }
protected override Variable Evaluate(ParsingScript script) { bool prefix = string.IsNullOrWhiteSpace(m_name); if (prefix)// If it is a prefix we do not have the variable name yet. { m_name = Utils.GetToken(script, Constants.TOKEN_SEPARATION); } // Value to be added to the variable: int valueDelta = m_action == Constants.INCREMENT ? 1 : -1; int returnDelta = prefix ? valueDelta : 0; // Check if the variable to be set has the form of x[a][b], // meaning that this is an array element. double newValue = 0; List <Variable> arrayIndices = Utils.GetArrayIndices(ref m_name); ParserFunction func = ParserFunction.GetFunction(m_name); Utils.CheckNotNull(m_name, func); Variable currentValue = func.GetValue(script); if (arrayIndices.Count > 0 || script.TryCurrent() == Constants.START_ARRAY) { if (prefix) { string tmpName = m_name + script.Rest; int delta = 0; arrayIndices = Utils.GetArrayIndices(ref tmpName, ref delta); script.Forward(Math.Max(0, delta - tmpName.Length)); } Variable element = Utils.ExtractArrayElement(currentValue, arrayIndices); script.MoveForwardIf(Constants.END_ARRAY); newValue = element.Value + returnDelta; element.Value += valueDelta; } else // A normal variable. { newValue = currentValue.Value + returnDelta; currentValue.Value += valueDelta; } ParserFunction.AddGlobalOrLocalVariable(m_name, new GetVarFunction(currentValue)); return(new Variable(newValue)); }
static ParserFunction GetArrayFunction(string name, ParsingScript script, string action) { int arrayStart = name.IndexOf(Constants.START_ARRAY); if (arrayStart < 0) { return(null); } if (arrayStart == 0) { Variable arr = Utils.ProcessArrayMap(new ParsingScript(name)); return(new GetVarFunction(arr)); } string arrayName = name; int delta = 0; List <Variable> arrayIndices = Utils.GetArrayIndices(script, arrayName, delta, (string arr, int del) => { arrayName = arr; delta = del; }); if (arrayIndices.Count == 0) { return(null); } ParserFunction pf = ParserFunction.GetVariable(arrayName, script); GetVarFunction varFunc = pf as GetVarFunction; if (varFunc == null) { return(null); } // we temporarily backtrack for the processing script.Backward(name.Length - arrayStart - 1); script.Backward(action != null ? action.Length : 0); // delta shows us how manxy chars we need to advance forward in GetVarFunction() delta -= arrayName.Length; delta += action != null ? action.Length : 0; varFunc.Indices = arrayIndices; varFunc.Delta = delta; return(varFunc); }
protected override Variable Evaluate(ParsingScript script) { // Value to be added to the variable: Variable right = Utils.GetItem(script); List <Variable> arrayIndices = Utils.GetArrayIndices(ref m_name); ParserFunction func = ParserFunction.GetFunction(m_name); Utils.CheckNotNull(m_name, func); Variable currentValue = func.GetValue(script); Variable left = currentValue; if (arrayIndices.Count > 0)// array element { left = Utils.ExtractArrayElement(currentValue, arrayIndices); script.MoveForwardIf(Constants.END_ARRAY); } if (left.Type == Variable.VarType.NUMBER) { NumberOperator(left, right, m_action); } else { StringOperator(left, right, m_action); } if (arrayIndices.Count > 0)// array element { AssignFunction.ExtendArray(currentValue, arrayIndices, 0, left); ParserFunction.AddGlobalOrLocalVariable(m_name, new GetVarFunction(currentValue)); } else { ParserFunction.AddGlobalOrLocalVariable(m_name, new GetVarFunction(left)); } return(left); }
protected override Variable Evaluate(ParsingScript script) { // 1. Get the name of the variable. string varName = Utils.GetToken(script, Constants.END_ARG_ARRAY); Utils.CheckNotEnd(script, m_name); List <Variable> arrayIndices = Utils.GetArrayIndices(ref varName); // 2. Get the current value of the variable. ParserFunction func = ParserFunction.GetFunction(varName); Utils.CheckNotNull(varName, func); Variable currentValue = func.GetValue(script); Variable element = currentValue; // 2b. Special case for an array. if (arrayIndices.Count > 0)// array element { element = Utils.ExtractArrayElement(currentValue, arrayIndices); script.MoveForwardIf(Constants.END_ARRAY); } // 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 = element.Type == Variable.VarType.ARRAY ? element.Tuple.Count : element.AsString().Length; script.MoveForwardIf(Constants.END_ARG, Constants.SPACE); Variable newValue = new Variable(size); return(newValue); }