Пример #1
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));
        }
Пример #2
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));
        }
Пример #3
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);
        }
Пример #4
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);
        }
Пример #5
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));
        }
Пример #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 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);
        }
Пример #7
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);
        }
Пример #8
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);
        }
Пример #9
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);
        }
Пример #10
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);
        }
Пример #11
0
        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
            {
                Value = Merge(baseCell, ref index, listToMerge)
            };

            return(result);
        }
Пример #12
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);
        }
Пример #13
0
        public static Variable Calculate(string functionName, string argsStr)
        {
            ParsingScript  script  = new ParsingScript(argsStr);
            string         action  = "";
            ParserFunction func    = new ParserFunction(script, functionName, Constants.EMPTY, ref action);
            Variable       current = func.GetValue(script);

            return(current);
        }
Пример #14
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);
        }
Пример #15
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, script);
            Variable varValue = func.GetValue(script);

            Utils.CheckNotNull(varValue, varName, script);
            return(varValue);
        }
Пример #16
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));
        }
Пример #17
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);
        }
Пример #18
0
        static List <Variable> Split(ParsingScript script, char[] to)
        {
            List <Variable> listToMerge = new List <Variable>(16);

            if (!script.StillValid() || to.Contains(script.Current))
            {
                listToMerge.Add(Variable.EmptyInstance);
                script.Forward();
                return(listToMerge);
            }

            int    arrayIndexDepth = 0;
            bool   inQuotes        = false;
            int    negated         = 0;
            char   ch;
            string action;

            do
            { // Main processing cycle of the first part.
                string token = ExtractNextToken(script, to, ref inQuotes, ref arrayIndexDepth, ref negated, out ch, out action);

                bool ternary = UpdateIfTernary(script, token, ch, listToMerge, (List <Variable> newList) => { listToMerge = newList; });
                if (ternary)
                {
                    return(listToMerge);
                }

                bool negSign = CheckConsistencyAndSign(script, listToMerge, action, ref token);

                // We are done getting the next token. The GetValue() call below may
                // recursively call SplitAndMerge(). This will happen if extracted
                // item is a function or if the next item is starting with a START_ARG '('.
                ParserFunction func    = new ParserFunction(script, token, ch, ref action);
                Variable       current = func.GetValue(script);

                if (UpdateResult(script, to, listToMerge, token, negSign, ref current, ref negated, ref action))
                {
                    return(listToMerge);
                }
            } while (script.StillValid() &&
                     (inQuotes || arrayIndexDepth > 0 || !to.Contains(script.Current)));

            // This happens when called recursively inside of the math expression:
            script.MoveForwardIf(Constants.END_ARG);

            return(listToMerge);
        }
Пример #19
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);
        }
Пример #20
0
        protected override Variable Evaluate(ParsingScript script)
        {
            string substring;

            // 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);
            Variable       currentValue = func.GetValue(script);

            // 3. Take either the string part if it is defined,
            // or the numerical part converted to a string otherwise.
            string arg = currentValue.AsString();
            // 4. Get the initial index of the substring.
            Variable init = Utils.GetItem(script);

            Utils.CheckNonNegativeInt(init);

            // 5. Get the length of the substring if available.
            bool lengthAvailable = Utils.SeparatorExists(script);

            if (lengthAvailable)
            {
                Variable length = Utils.GetItem(script);
                Utils.CheckPosInt(length);
                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);
            }
            Variable newValue = new Variable(substring);

            return(newValue);
        }
Пример #21
0
        protected override Variable Evaluate(ParsingScript script)
        {
            // 1. Get the name of the variable.
            string varName = Utils.GetToken(script, Constants.END_ARG_ARRAY);

            Utils.CheckNotEmpty(script, varName, m_name);

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

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

            Variable newValue = new Variable(arg.ToLower());

            return(newValue);
        }
Пример #22
0
        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);
        }
Пример #23
0
        public static Variable GetVariable(string varName, ParsingScript script = null, bool testNull = true)
        {
            varName = varName.ToLower();
            if (script == null)
            {
                script = new ParsingScript("");
            }

            ParserFunction func = ParserFunction.GetVariable(varName, script);

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

            Utils.CheckNotNull(varValue, varName, script);
            return(varValue);
        }
Пример #24
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 string part if it is defined,
            // or the numerical part converted to a string otherwise.
            string arg = currentValue.String ?? currentValue.Value.ToString();

            Parser.Result newValue = new Parser.Result(Double.NaN, arg.ToLower());
            return(newValue);
        }
Пример #25
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);
            Variable       currentValue = func.GetValue(script);

            // 3. Get the value to be looked for.
            Variable searchValue = Utils.GetItem(script);

            // 4. Apply the corresponding C# function.
            string basePart = currentValue.AsString();
            string search   = searchValue.AsString();

            int result = basePart.IndexOf(search);

            return(new Variable(result));
        }
Пример #26
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. 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);
        }
Пример #27
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.
            ParserFunction func = ParserFunction.GetFunction(varName);

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

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

            // 4. Add it to the tuple.
            currentValue.AddVariable(item);

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

            return(currentValue);
        }
Пример #28
0
        // Calculate
        // Изчисли
        private static List <Variable> Calculate(string data, ref int from, char[] to)
        {
            List <Variable> listToMerge = new List <Variable>(); // 32

            // Console.WriteLine(forum);

            if (from >= data.Length || to.Contains(data[from]))
            {
                listToMerge.Add(Variable.EmptyInstance);
                return(listToMerge);
            }

            StringBuilder item     = new StringBuilder();
            int           negated  = 0;     // отказване
            bool          inQuotes = false; // в Цитати

            do
            {
                // Основен цикъл на обработка на първата част.
//                string negateSymbol = Utils.IsNotSign(data.Substring(from));
//
//                //Console.WriteLine(negateSymbol);
//
//                if (negateSymbol != null)
//                {
//                    negated++;
//                    from += negateSymbol.Length;
//                    continue;
//                }

                char ch = data[from++];

                inQuotes = ch == Constants.QUOTE && (from == 0 || data[from] != '\\') ? !inQuotes : inQuotes;

                string action = null;

                bool keepCollecting = inQuotes || StillCollecting(item.ToString(), to, data, from, ref action);

                //Console.WriteLine(ch);
                if (keepCollecting)
                {
                    // Char отново принадлежи към предишния операнд.
                    item.Append(ch);

                    if (from < data.Length && (inQuotes || !to.Contains(data[from])))
                    {
                        continue;
                    }
                }

                string parsingItem = item.ToString();

                // CheckConsistency(parsingItem, listToMerge, data, from);

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

                if (action != null && action.Length > 1)
                {
                    from += (action.Length - 1);
                }

                // Ние сме готови да получим следващия знак. Призивът getValue () може да е по-долу
                // рекурсивно извиква loadAndCalculate (). Това ще стане, ако се извлече
                // елементът е функция или ако следващият елемент започва с START_ARG '('.

                ParserFunction func = new ParserFunction(data, ref from, parsingItem, ch, ref action);

                Variable current = func.GetValue(data, ref from);

                if (negated > 0 && current.Type == Variable.VarType.NUMBER)
                {
                    // Ако има знак NOT, това е булев.
                    // Използвайте XOR (вярно, ако точно един от аргументите е вярно).
                    bool boolRes = !((negated % 2 == 0) ^ Convert.ToBoolean(current.Value));
                    current = new Variable(Convert.ToDouble(boolRes));
                    negated = 0;
                }

                if (action == null)
                {
                    action = UpdateAction(data, ref from, to);
                }


                char next = from < data.Length ? data[from] : Constants.EMPTY;
                bool done = listToMerge.Count == 0 &&
                            ((action == Constants.END_ARG_STR &&
                              current.Type != Variable.VarType.NUMBER) ||
                             next == Constants.END_STATEMENT);
                if (done)
                {
                    // Ако няма числен резултат, ние не сме в математически израз.
                    listToMerge.Add(current);
                    return(listToMerge);
                }

                Variable cell = new Variable(current);
                cell.Action = action;
                listToMerge.Add(cell);
                item.Clear();
            } while (from < data.Length && (inQuotes || !to.Contains(data[from])));

            // Това се случва, когато се нарича рекурсивно вътре в математическия израз:
            Utils.MoveForwardIf(data, ref from, Constants.END_ARG);

            return(listToMerge);
        }
Пример #29
0
        private static List <Variable> Split(ParsingScript script, char[] to)
        {
            List <Variable> listToMerge = new List <Variable>(16);

            if (!script.StillValid() || to.Contains(script.Current))
            {
                listToMerge.Add(Variable.EmptyInstance);
                script.Forward();
                return(listToMerge);
            }

            StringBuilder item            = new StringBuilder();
            int           arrayIndexDepth = 0;
            bool          inQuotes        = false;
            int           negated         = 0;

            string rest = script.Rest;

            //if (rest == "b[a[0]];") {
            //  int stop = 1;
            //}

            do // Main processing cycle of the first part.
            {
                string negateSymbol = Utils.IsNotSign(script.Rest);
                if (negateSymbol != null && !inQuotes)
                {
                    negated++;
                    script.Forward(negateSymbol.Length);
                    continue;
                }

                char ch = script.CurrentAndForward();
                CheckQuotesIndices(script, ch, ref inQuotes, ref arrayIndexDepth);
                string action = null;

                bool keepCollecting = inQuotes || arrayIndexDepth > 0 ||
                                      StillCollecting(item.ToString(), to, script, ref action);
                if (keepCollecting)
                {
                    // The char still belongs to the previous operand.
                    item.Append(ch);

                    bool goForMore = script.StillValid() &&
                                     (inQuotes || arrayIndexDepth > 0 || !to.Contains(script.Current));
                    if (goForMore)
                    {
                        continue;
                    }
                }

                if (SkipOrAppendIfNecessary(item, ch, to))
                {
                    continue;
                }

                string token = item.ToString();

                bool ternary = UpdateIfTernary(script, token, ch, ref listToMerge);
                if (ternary)
                {
                    return(listToMerge);
                }

                CheckConsistency(token, listToMerge, script);

                script.MoveForwardIf(Constants.SPACE);

                if (action != null && action.Length > 1)
                {
                    script.Forward(action.Length - 1);
                }

                // 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 '('.
                ParserFunction func    = new ParserFunction(script, token, ch, ref action);
                Variable       current = func.GetValue(script);
                current.ParsingToken = token;

                if (negated > 0 && current.Type == Variable.VarType.NUMBER)
                {
                    // If there has been a NOT sign, this is a boolean.
                    // Use XOR (true if exactly one of the arguments is true).
                    bool neg = !((negated % 2 == 0) ^ Convert.ToBoolean(current.Value));
                    current = new Variable(Convert.ToDouble(neg));
                    negated = 0;
                }

                if (action == null)
                {
                    action = UpdateAction(script, to);
                }
                else
                {
                    script.MoveForwardIf(action[0]);
                }

                char next = script.TryCurrent(); // we've already moved forward
                bool done = listToMerge.Count == 0 &&
                            (next == Constants.END_STATEMENT ||
                             (action == Constants.NULL_ACTION && current.Type != Variable.VarType.NUMBER) ||
                             current.IsReturn);
                if (done)
                {
                    if (action != null && action != Constants.END_ARG_STR)
                    {
                        throw new ArgumentException("Action [" +
                                                    action + "] without an argument.");
                    }
                    // If there is no numerical result, we are not in a math expression.
                    listToMerge.Add(current);
                    return(listToMerge);
                }

                Variable cell = current.Clone();
                cell.Action = action;

                bool addIt = UpdateIfBool(script, ref cell, ref listToMerge);
                if (addIt)
                {
                    listToMerge.Add(cell);
                }
                item.Clear();
            } while (script.StillValid() &&
                     (inQuotes || arrayIndexDepth > 0 || !to.Contains(script.Current)));

            // This happens when called recursively inside of the math expression:
            script.MoveForwardIf(Constants.END_ARG);

            return(listToMerge);
        }