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.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); }
//Get Array Function public static ParserFunction GetArrayFunction(string name, ref int from, string action) { if (!string.IsNullOrWhiteSpace(action)) { return(null); } int arrayStart = name.IndexOf(Constants.START_ARRAY); if (arrayStart <= 0) { return(null); } int origLength = name.Length; int arrayIndex = Utils.ExtractArrayElement(ref name); if (arrayIndex < 0) { return(null); } ParserFunction pf = ParserFunction.GetFunction(name); if (pf == null) { return(null); } from -= (origLength - arrayStart - 1); return(pf); }
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)); }
static ParserFunction GetObjectFunction(string name, ParsingScript script) { if (script.CurrentClass != null && script.CurrentClass.Name == name) { script.Backward(name.Length + 1); return(new FunctionCreator()); } if (script.ClassInstance != null && (script.ClassInstance.PropertyExists(name) || script.ClassInstance.FunctionExists(name))) { name = script.ClassInstance.InstanceName + "." + name; } //int ind = name.LastIndexOf('.'); int ind = name.IndexOf('.'); if (ind <= 0) { return(null); } string baseName = name.Substring(0, ind); if (s_namespaces.ContainsKey(baseName)) { int ind2 = name.IndexOf('.', ind + 1); if (ind2 > 0) { ind = ind2; baseName = name.Substring(0, ind); } } string prop = name.Substring(ind + 1); ParserFunction pf = ParserFunction.GetFromNamespace(prop, baseName, script); if (pf != null) { return(pf); } pf = ParserFunction.GetVariable(baseName, script, true); if (pf == null || !(pf is GetVarFunction)) { pf = ParserFunction.GetFunction(baseName, script); if (pf == null) { pf = Utils.ExtractArrayElement(baseName); } } GetVarFunction varFunc = pf as GetVarFunction; if (varFunc == null) { return(null); } varFunc.PropertyName = prop; return(varFunc); }
protected override Variable Evaluate(string data, ref int from) { Variable varValue = Utils.GetItem(data, ref from); // Специален случай за добавяне на низ (или число) към низ. while (varValue.Type != Variable.VarType.NUMBER && from > 0 && data[from - 1] == '+') { Variable addition = Utils.GetItem(data, ref from); varValue.String += addition.AsString(); } // Проверете дали променливата, която трябва да бъде настроена, има формата на x (0), // означава, че това е масив елемент. int arrayIndex = Utils.ExtractArrayElement(ref _name); if (arrayIndex < 0) { ParserFunction.AddGlobalOrLocalVariable(_name, new GetVarFunction(varValue)); return(varValue); } Variable currentValue; ParserFunction pf = ParserFunction.GetFunction(_name); if (pf != null) { currentValue = pf.GetValue(data, ref from); } else { currentValue = new Variable(); } List <Variable> tuple = currentValue.Tuple == null ? new List <Variable>() : currentValue.Tuple; if (tuple.Count > arrayIndex) { tuple[arrayIndex] = varValue; } else { for (int i = tuple.Count; i < arrayIndex; i++) { tuple.Add(Variable.EmptyInstance); } tuple.Add(varValue); } currentValue.Tuple = tuple; ParserFunction.AddGlobalOrLocalVariable(_name, new GetVarFunction(currentValue)); return(currentValue); }
protected override Variable Evaluate(string data, ref int from) { bool prefix = string.IsNullOrWhiteSpace(_name); if (prefix) // Ако е префикс, все още нямаме име на променлива. { _name = Utils.GetToken(data, ref from, Constants.TOKEN_SEPARATION); } // Стойност, която трябва да се добави към променливата: int valueDelta = _action == Constants.INCREMENT ? 1 : -1; int returnDelta = prefix ? valueDelta : 0; // Проверете дали променливата, която трябва да бъде настроена, има формата на x (0), // означава, че това е масив елемент. double newValue = 0; int arrayIndex = Utils.ExtractArrayElement(ref _name); bool exists = ParserFunction.FunctionExists(_name); if (!exists) { throw new ArgumentException("Variable [" + _name + "] не съществува"); } Variable currentValue = ParserFunction.GetFunction(_name).GetValue(data, ref from); if (arrayIndex >= 0) // Променлива с индекс (масив елемент). { if (currentValue.Tuple == null) { throw new ArgumentException("Tuple [" + _name + "] не съществува"); } if (currentValue.Tuple.Count <= arrayIndex) { throw new ArgumentException("Tuple [" + _name + "] има само " + currentValue.Tuple.Count + " елементи"); } newValue = currentValue.Tuple[arrayIndex].Value + returnDelta; currentValue.Tuple[arrayIndex].Value += valueDelta; } else // Нормална променлива. { newValue = currentValue.Value + returnDelta; currentValue.Value += valueDelta; } Variable varValue = new Variable(newValue); ParserFunction.AddGlobalOrLocalVariable(_name, new GetVarFunction(currentValue)); return(varValue); }
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)); }
protected override Variable Evaluate(string data, ref int from) { // Стойност, която трябва да се добави към променливата: Variable valueB = Utils.GetItem(data, ref from); // Проверете дали променливата, която трябва да бъде настроена, има формата на x (0), // означава, че това е масив елемент. int arrayIndex = Utils.ExtractArrayElement(ref _name); bool exists = ParserFunction.FunctionExists(_name); if (!exists) { throw new ArgumentException("Променлив [" + _name + "] не съществува"); } Variable currentValue = ParserFunction.GetFunction(_name).GetValue(data, ref from); Variable valueA = currentValue; if (arrayIndex >= 0) // Променлива с индекс. { if (currentValue.Tuple == null) { throw new ArgumentException("Tuple [" + _name + "] не съществува"); } if (currentValue.Tuple.Count <= arrayIndex) { throw new ArgumentException("Tuple [" + _name + "] има само " + currentValue.Tuple.Count + " елементи"); } valueA = currentValue.Tuple[arrayIndex]; } if (valueA.Type == Variable.VarType.NUMBER) { NumberOperator(valueA, valueB, _action); } else { StringOperator(valueA, valueB, _action); } Variable varValue = new Variable(valueA); ParserFunction.AddGlobalOrLocalVariable(_name, new GetVarFunction(varValue)); return(valueA); }
protected override Parser.Result Evaluate(string data, ref int from) { string varName = Utils.GetToken(data, ref from, Constants.END_ARG_ARRAY); if (from >= data.Length) { throw new ArgumentException("Couldn't set variable before end of line"); } NameVar nv = new NameVar(varName); Parser.Result varValue = new Parser.Result(); string value = ScDumper.GetValue(nv); varValue.Value = Double.Parse(value); // Check if the variable to be set has the form of x(0), // meaning that this is an array element. int arrayIndex = Utils.ExtractArrayElement(ref varName); if (arrayIndex >= 0) { bool exists = ParserFunction.FunctionExists(varName); Parser.Result currentValue = exists ? ParserFunction.GetFunction(varName).GetValue(data, ref from) : new Parser.Result(); List <Parser.Result> tuple = currentValue.Tuple ?? new List <Parser.Result>(); if (tuple.Count > arrayIndex) { tuple[arrayIndex] = varValue; } else { for (int i = tuple.Count; i < arrayIndex; i++) { tuple.Add(new Parser.Result(Double.NaN, string.Empty)); } tuple.Add(varValue); } varValue = new Parser.Result(Double.NaN, null, tuple); } ParserFunction.AddFunction(varName, new GetVarFunction(varValue)); return(new Parser.Result(Double.NaN, varName)); }
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); }