예제 #1
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);
        }
예제 #2
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);
        }
예제 #3
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));
        }
예제 #4
0
        public static string GetStringOrVarValue(ParsingScript script)
        {
            script.MoveForwardIf(Constants.SPACE);

            // If this token starts with a quote then it is a string constant.
            // Otherwide we treat it as a variable, but if the variable doesn't exist then it
            // will be still treated as a string constant.
            bool stringConstant = script.Rest.StartsWith(Constants.QUOTE.ToString());

            string token = Utils.GetToken(script, Constants.NEXT_OR_END_ARRAY);

            // Check if this is a variable definition:
            stringConstant = stringConstant || !ParserFunction.FunctionExists(token);
            if (!stringConstant)
            {
                Variable sourceValue = ParserFunction.GetFunction(token).GetValue(script);
                token = sourceValue.String;
            }

            return(token);
        }