Exemple #1
0
        /// <summary>
        /// Проверка входного орератора на совпадение
        /// </summary>
        /// <param name="items">Колекция элементов выражений сомпиляции</param>
        /// <param name="indexEvent">Индек начала проверки</param>
        public bool IsCheck(List <CompiledItem> items, int index)
        {
            if (items[index].Text != Signatures[0])
            {
                return(false);
            }
            int startIndex = 0;

            if (_operatorItemList[0].Kind == eCompiledItemKind.Expression)
            {
                if (index == 0)
                {
                    return(false);
                }
                else if (items[index - 1].Kind != eCompiledItemKind.Expression)
                {
                    return(false);
                }
                else
                {
                    startIndex = 1;
                }
            }
            else if (index > 0 && items[index - 1].Kind != eCompiledItemKind.Signature)
            {
                return(false);
            }

            for (int i = startIndex; i < _operatorItemList.Count; i++)
            {
                if (items.Count <= index)
                {
                    return(false);
                }

                OperatorItem operatorItem = _operatorItemList[i];
                CompiledItem compiledItem = items[index];
                if (operatorItem.Kind != compiledItem.Kind)
                {
                    return(false);
                }
                if (operatorItem.Kind == eCompiledItemKind.Signature && operatorItem.Text != compiledItem.Text)
                {
                    return(false);
                }
                index++;
            }
            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Компиляция текстового выражения
        /// </summary>
        /// <param name="sourceString">Строка с выражением</param>
        /// <param name="value">Объект занчения объекта переменной</param>
        /// <returns>Скомпилированный объект выражения</returns>
        public Expression Compile(string sourceString, Value value)
        {
            if (sourceString == null)
            {
                throw new ArgumentNullException("sourceString");
            }
            if (sourceString.Length == 0)
            {
                throw new ArgumentException("String is empty.", "sourceString");
            }

            List <CompiledItem> res = new List <CompiledItem>();

            int             length;
            string          operandString;
            CompiledItem    addItem;
            string          signature;
            List <Operator> collection        = null;
            bool            operandStarted    = false;
            int             operandStartIndex = 0;

            for (int i = 0; i < sourceString.Length; i++)
            {
                if (sourceString[i] == ' ')
                {
                    continue;
                }
                length        = 0;
                addItem       = null;
                signature     = null;
                operandString = sourceString.Substring(i);
                foreach (KeyValuePair <string, List <Operator> > pair in _signaturesDictionary)
                {
                    if (operandString.StartsWith(pair.Key) && pair.Key.Length > length)
                    {
                        signature  = pair.Key;
                        collection = pair.Value;
                    }
                }
                if (signature != null)
                {
                    if (collection[0].Kind == eOperatorKind.Variable)
                    {
                        if (collection[0].Signatures.Count == 1)
                        {
                            res.Add(GetVariableOrValue(signature, value));
                        }
                        else
                        {
                            signature = operandString.Substring(0, operandString.IndexOf(collection[0].Signatures[1]) + collection[0].Signatures[1].Length);
                            res.Add(GetVariableOrValue(signature.Substring(collection[0].Signatures[0].Length,
                                                                           signature.Length - (collection[0].Signatures[0].Length + collection[0].Signatures[1].Length)), value));
                        }
                        i += signature.Length - 1;
                        continue;
                    }

                    addItem = new CompiledItem(signature, collection);
                    if (operandStarted)
                    {
                        res.Add(GetVariableOrValue(sourceString.Substring(operandStartIndex, i - operandStartIndex), value));
                        operandStarted = false;
                    }
                    res.Add(addItem);
                    if (addItem.Kind == eCompiledItemKind.Signature)
                    {
                        i += addItem.Text.Length - 1;
                    }
                }
                else if (!operandStarted)
                {
                    operandStarted    = true;
                    operandStartIndex = i;
                }
            }
            if (operandStarted)
            {
                res.Add(GetVariableOrValue(sourceString.Substring(operandStartIndex), value));
            }
            return(LinkExpression(res));
        }