//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);
        }
示例#2
0
        protected override Variable Evaluate(ParsingScript script)
        {
            List <Variable> args = script.GetFunctionArgs();

            Utils.CheckArgs(args.Count, 3, m_name);

            string   varName = Utils.GetSafeString(args, 0);
            Variable toAdd   = Utils.GetSafeVariable(args, 1);
            string   hash    = Utils.GetSafeString(args, 2);

            var      function = ParserFunction.GetFunction(varName);
            Variable mapVar   = function != null?function.GetValue(script) :
                                    new Variable(Variable.VarType.ARRAY);

            mapVar.AddVariableToHash(hash, toAdd);
            for (int i = 3; i < args.Count; i++)
            {
                string hash2 = Utils.GetSafeString(args, 3);
                if (!string.IsNullOrWhiteSpace(hash2) &&
                    !hash2.Equals(hash, StringComparison.OrdinalIgnoreCase))
                {
                    mapVar.AddVariableToHash(hash2, toAdd);
                }
            }

            ParserFunction.AddGlobalOrLocalVariable(varName,
                                                    new GetVarFunction(mapVar));

            return(Variable.EmptyInstance);
        }
示例#3
0
        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, m_name);

            // 2. Get the current value of the variable.
            ParserFunction func = ParserFunction.GetFunction(varName);

            Utils.CheckNotNull(varName, func);
            Variable currentValue = func.GetValue(script);

            Utils.CheckArray(currentValue, varName);

            // 3. Get the variable to remove.
            Variable item = Utils.GetItem(script);

            Utils.CheckNonNegativeInt(item);

            currentValue.Tuple.RemoveAt(item.AsInt());

            ParserFunction.AddGlobalOrLocalVariable(varName,
                                                    new GetVarFunction(currentValue));
            return(Variable.EmptyInstance);
        }
示例#4
0
        protected override Variable Evaluate(ParsingScript script)
        {
            // 1. Get the name of the variable.
            string varName = Utils.GetToken(script, Constants.NEXT_ARG_ARRAY);

            Utils.CheckNotEmpty(script, varName, m_name);

            // 2. Get the current value of the variable.
            ParserFunction func         = ParserFunction.GetFunction(varName, script);
            Variable       currentValue = func.GetValue(script);

            // 3. Get the value to be added or appended.
            Variable newValue = Utils.GetItem(script);

            // 4. Take either the string part if it is defined,
            // or the numerical part converted to a string otherwise.
            string arg1 = currentValue.AsString();
            string arg2 = newValue.AsString();

            // 5. The variable becomes a string after adding a string to it.
            newValue.Reset();
            newValue.String = arg1 + arg2;

            ParserFunction.AddGlobalOrLocalVariable(varName, new GetVarFunction(newValue));

            return(newValue);
        }
示例#5
0
        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);
        }
示例#6
0
        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));
        }
示例#7
0
        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);
        }
示例#8
0
        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);
        }
示例#9
0
        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));
        }
示例#10
0
        protected override Variable Evaluate(ParsingScript script)
        {
            // 1. Get the name of the variable.
            List <Variable> args = script.GetFunctionArgs();

            Utils.CheckArgs(args.Count, 2, m_name, true);
            string varName = args[0].AsString();

            // 2. Get the current value of the variable.
            ParserFunction func = ParserFunction.GetFunction(varName);

            Utils.CheckNotNull(varName, func);
            Variable currentValue = func.GetValue(script);

            Utils.CheckArray(currentValue, varName);

            // 3. Get the variable to remove.
            Variable item = args[1];

            bool removed = currentValue.Tuple.Remove(item);

            ParserFunction.AddGlobalOrLocalVariable(varName,
                                                    new GetVarFunction(currentValue));
            return(new Variable(removed));
        }
示例#11
0
        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);
        }
示例#12
0
        // Translation
        public void AddTranslation(NameValueCollection languageDictionary, string originalName)
        {
            string translation = languageDictionary[originalName];

            if (string.IsNullOrWhiteSpace(translation))
            {
                // Преводът не е предвиден за тази функция.
                return;
            }

            if (translation.IndexOfAny((" \t\r\n").ToCharArray()) >= 0)
            {
                throw new ArgumentException("Превод на [" + translation + "] съдържа бели полета");
            }

            ParserFunction originalFunction = ParserFunction.GetFunction(originalName);

            ParserFunction.AddGlobal(translation, originalFunction);

            // Ако списъкът с функции, след които може да има интервал (освен скоби)
            // съдържа оригиналната функция, също добавете превод към списъка.
            if (Constants.FUNCT_WITH_SPACE.Contains(originalName))
            {
                Constants.FUNCT_WITH_SPACE.Add(translation);
            }
        }
示例#13
0
        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);
        }
示例#14
0
        protected override Variable Evaluate(string data, ref int from)
        {
            // 1. Получете името на променливата.
            string varName = Utils.GetToken(data, ref from, Constants.END_ARG_ARRAY);

            if (from >= data.Length)
            {
                throw new ArgumentException("Променливата не можа да се получи");
            }

            // 2. Получете текущата стойност на променливата.
            ParserFunction func         = ParserFunction.GetFunction(varName);
            Variable       currentValue = func.GetValue(data, ref from);

            // 3. Вземете дължината на основната тона или
            // низовата част, ако е дефинирана,
            // или цифровата част, превърната в низ по друг начин.
            int size = currentValue.Tuple != null ? currentValue.Tuple.Count :
                       currentValue.AsString().Length;


            Utils.MoveForwardIf(data, ref from, Constants.END_ARG, Constants.SPACE);

            Variable newValue = new Variable(size);

            return(newValue);
        }
示例#15
0
        public static Variable GetVariable(string varName, ParsingScript script)
        {
            ParserFunction func = ParserFunction.GetFunction(varName);

            Utils.CheckNotNull(varName, func);
            Variable varValue = func.GetValue(script);

            return(varValue);
        }
示例#16
0
        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);
        }
示例#17
0
        Variable.VarType GetVariableType(string paramName)
        {
            if (IsNumber(paramName))
            {
                return(Variable.VarType.NUMBER);
            }
            else if (IsString(paramName))
            {
                return(Variable.VarType.STRING);
            }

            if (Constants.RESERVED.Contains(paramName) ||
                Constants.TOKEN_SEPARATION_STR.Contains(paramName))
            {
                return(Variable.VarType.NONE);
            }

            var functionReturnType = GetReturnType(paramName);

            if (functionReturnType != Variable.VarType.NONE)
            {
                return(functionReturnType);
            }

            Variable arg;

            if (m_argsMap.TryGetValue(paramName, out arg))
            {
                return(arg.Type);
            }

            ParserFunction function = ParserFunction.GetFunction(paramName, m_parentScript);

            if (function == null)
            {
                return(Variable.VarType.NONE);
            }

            if (function is INumericFunction)
            {
                return(Variable.VarType.NUMBER);
            }
            else if (function is IStringFunction)
            {
                return(Variable.VarType.STRING);
            }
            else if (function is IArrayFunction)
            {
                return(Variable.VarType.ARRAY);
            }

            return(Variable.VarType.NONE);
        }
示例#18
0
        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);
        }
示例#19
0
        static string TranslateScript(string script, string fromLang, string toLang, ParsingScript parentScript)
        {
            StringBuilder result = new StringBuilder();
            StringBuilder item   = new StringBuilder();

            Dictionary <string, string> keywordsDict = KeywordsDictionary(fromLang, toLang);
            Dictionary <string, string> transDict    = TranslationDictionary(fromLang, toLang);
            bool inQuotes = false;

            for (int i = 0; i < script.Length; i++)
            {
                char ch = script[i];
                inQuotes = ch == Constants.QUOTE ? !inQuotes : inQuotes;

                if (inQuotes)
                {
                    result.Append(ch);
                    continue;
                }
                if (!Constants.TOKEN_SEPARATION.Contains(ch))
                {
                    item.Append(ch);
                    continue;
                }
                if (item.Length > 0)
                {
                    string token       = item.ToString();
                    string translation = string.Empty;
                    if (toLang == Constants.ENGLISH)
                    {
                        ParserFunction func = ParserFunction.GetFunction(token, parentScript);
                        if (func != null)
                        {
                            translation = func.Name;
                        }
                    }
                    if (string.IsNullOrEmpty(translation) &&
                        !keywordsDict.TryGetValue(token, out translation) &&
                        !transDict.TryGetValue(token, out translation))
                    {
                        translation = token;//TryTranslit (fromLang, toLang, token);
                    }
                    result.Append(translation);
                    item.Clear();
                }
                result.Append(ch);
            }

            return(result.ToString());
        }
示例#20
0
        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));
        }
示例#21
0
        protected override Variable Evaluate(ParsingScript script)
        {
            string         funcName = Utils.GetToken(script, Constants.TOKEN_SEPARATION);
            ParserFunction function = ParserFunction.GetFunction(funcName);
            CustomFunction custFunc = function as CustomFunction;

            Utils.CheckNotNull(funcName, custFunc);

            string body = Utils.BeautifyScript(custFunc.Body, custFunc.Header);

            Translation.PrintScript(body);

            return(new Variable(body));
        }
示例#22
0
        public static Variable GetVariable(string varName, ParsingScript script, bool testNull = true)
        {
            ParserFunction func = ParserFunction.GetFunction(varName, script);

            if (!testNull && func == null)
            {
                return(null);
            }
            Utils.CheckNotNull(varName, func);
            Variable varValue = func.GetValue(script);

            Utils.CheckNotNull(varValue, varName);
            return(varValue);
        }
示例#23
0
        public static async Task <Variable> GetVariableAsync(string varName, ParsingScript script, bool testNull = true)
        {
            ParserFunction func = ParserFunction.GetFunction(varName, script);

            if (!testNull && func == null)
            {
                return(null);
            }
            Utils.CheckNotNull(varName, func, script);
            Variable varValue = await func.GetValueAsync(script);

            Utils.CheckNotNull(varValue, varName, script);
            return(varValue);
        }
示例#24
0
        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);
        }
示例#25
0
        public static Variable GetVar(string paramName, ParsingScript script = null)
        {
            if (script == null)
            {
                script = new ParsingScript("");
            }
            ParserFunction function = ParserFunction.GetFunction(paramName);

            if (function == null)
            {
                throw new ArgumentException("Variable [" + paramName + "] not found.");
            }
            Variable result = function.GetValue(script);

            return(result);
        }
示例#26
0
        protected override Variable Evaluate(ParsingScript script)
        {
            bool            isList = false;
            List <Variable> args   = Utils.GetArgs(script,
                                                   Constants.START_ARG, Constants.END_ARG, out isList);

            Utils.CheckArgs(args.Count, 2, m_name);

            Variable all     = Utils.GetSafeVariable(args, 0);
            string   varName = Utils.GetSafeString(args, 1);
            int      index   = Utils.GetSafeInt(args, 2);

            var      function = ParserFunction.GetFunction(varName);
            Variable mapVar   = new Variable(Variable.VarType.ARRAY);

            if (all.Tuple == null)
            {
                return(Variable.EmptyInstance);
            }

            string currentValue = "";
            int    currentCount = 0;

            int globalCount = 0;

            for (int i = 0; i < all.Tuple.Count; i++)
            {
                Variable current = all.Tuple[i];
                if (current.Tuple == null || current.Tuple.Count < index)
                {
                    break;
                }
                string newValue = current.Tuple[index].AsString();
                if (currentValue != newValue)
                {
                    currentValue = newValue;
                    currentCount = 0;
                }
                mapVar.Tuple.Add(new Variable(currentCount));
                currentCount++;
                globalCount++;
            }

            ParserFunction.AddGlobalOrLocalVariable(varName,
                                                    new GetVarFunction(mapVar));
            return(mapVar);
        }
示例#27
0
        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));
        }
示例#28
0
        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);
        }
示例#29
0
        public void AddTranslation(NameValueCollection languageDictionary, string originalName)
        {
            string translation = languageDictionary[originalName];

            if (string.IsNullOrWhiteSpace(translation))
            { // The translation is not provided for this function.
                return;
            }

            if (translation.IndexOfAny((" \t\r\n").ToCharArray()) >= 0)
            {
                throw new ArgumentException("Translation of [" + translation + "] contains white spaces");
            }

            ParserFunction originalFunction = ParserFunction.GetFunction(originalName);

            ParserFunction.AddFunction(translation, originalFunction);
        }
示例#30
0
        public static bool ExtractParameterNames(List <Variable> args, string functionName, ParsingScript script)
        {
            CustomFunction custFunc = ParserFunction.GetFunction(functionName, script) as CustomFunction;

            if (custFunc == null)
            {
                return(false);
            }

            var realArgs = custFunc.RealArgs;

            for (int i = 0; i < args.Count && i < realArgs.Length; i++)
            {
                string name = args[i].CurrentAssign;
                args[i].ParamName = string.IsNullOrWhiteSpace(name) ? realArgs[i] : name;
            }
            return(true);
        }