Пример #1
0
        static object[] ProcessParentheses(object[] expression)
        {
            int?startIndex, endIndex, currentLevel = 0;

            startIndex = endIndex = null;
            string errorMessage = String.Empty;

            for (int i = 0; i < expression.Length; i++)
            {
                if (expression[i] is Word)
                {
                    Word w = (Word)expression[i];
                    if (w.GetCharacterType() == Word.CharacterType.Symbol)
                    {
                        if (w.GetSymbol() == Word.SymbolType.ParenthesesOpen)
                        {
                            if (startIndex == null)
                            {
                                startIndex = i;
                                currentLevel++;
                            }
                            else
                            {
                                currentLevel++;
                            }
                        }
                        else if (w.GetSymbol() == Word.SymbolType.ParenthesesClose)
                        {
                            currentLevel--;

                            if (currentLevel == 0)
                            {
                                endIndex = i;
                                break;
                            }
                        }
                    }
                }
                // Handle no open only closed
                if (currentLevel < 0)
                {
                    errorMessage = "Unexpected \")\" are you missing \"(\"?";
                }
            }

            int length = endIndex.Value - startIndex.Value;

            object[] processedExpression = new object[expression.Length - length];
            Chunk    c = null;

            if (startIndex != null && endIndex != null)
            {
                Queue <object> subExpression = new Queue <object>();

                for (int i = startIndex.Value + 1; i < endIndex.Value; i++)
                {
                    subExpression.Enqueue(expression[i]);
                }
                c = Chunkify(subExpression);
            }
            // Return an exception if there is an issue
            if (errorMessage != String.Empty)
            {
                c = new ProgramException(errorMessage);
            }

            for (int i = 0; i < expression.Length; i++)
            {
                if (i > endIndex.Value)
                {
                    processedExpression[i - endIndex.Value] = expression[i];
                }
                else if (i > startIndex.Value)
                {
                    continue;
                }
                else if (i == startIndex.Value)
                {
                    processedExpression[i] = c;
                }
                else
                {
                    processedExpression[i] = expression[i];
                }
            }
            return(processedExpression);
        }
Пример #2
0
        static object[] ProcessOperator(object[] expression, int index)
        {
            Operation operation = OperatorTypeToOperation(((Word)expression[index]).GetOperator().GetValueOrDefault());

            int startIndex = index - operation.GetAmountOfArgumentsOnLeft();
            int endIndex   = index + operation.GetAmountOfArgumentsOnRight();
            int length     = endIndex - startIndex;

            Chunk[] left         = new Chunk[operation.GetAmountOfArgumentsOnLeft()], right = new Chunk[operation.GetAmountOfArgumentsOnRight()];
            string  errorMessage = String.Empty;

            if (startIndex < 0)
            {
                errorMessage = "Invalid amount of arguments on left side.";
            }
            else if (endIndex >= expression.Length)
            {
                errorMessage = "Invalid amount of arguments on right side.";
            }
            else
            {
                // Process left side
                for (int i = startIndex; i < index; i++)
                {
                    if (expression[i] is Word)
                    {
                        Word word = (Word)expression[i];
                        if (word.GetCharacterType() == Word.CharacterType.Text || word.GetCharacterType() == Word.CharacterType.Number)
                        {
                            left[i - startIndex] = new Chunk(word);
                        }
                        else
                        {
                            // Thrown an error, invalid syntax, unexpected operator, symbol or unexpected error somewhere else
                            errorMessage = "Invalid syntax: Unexpected " + word.GetCharacterType();
                        }
                    }
                    else if (expression[i] is Chunk)
                    {
                        left[i - startIndex] = (Chunk)expression[i];
                    }
                }

                // Process right side
                for (int i = index + 1; i <= endIndex; i++)
                {
                    if (expression[i] is Word)
                    {
                        Word word = (Word)expression[i];
                        if (word.GetCharacterType() == Word.CharacterType.Text || word.GetCharacterType() == Word.CharacterType.Number)
                        {
                            right[i - 1 - index] = new Chunk(word);
                        }
                        else
                        {
                            // Thrown an error, invalid syntax, unexpected operator, symbol or unexpected error somewhere else
                            errorMessage = "Invalid syntax: Unexpected " + word.GetCharacterType();
                        }
                    }
                    else if (expression[i] is Chunk)
                    {
                        right[i - 1 - index] = (Chunk)expression[i];
                    }
                }
            }
            Chunk c;

            if (errorMessage == String.Empty)
            {
                c = new Chunk(operation, left, right);
            }
            else
            {
                c = new ProgramException(errorMessage);
            }
            object[] processedExpression = new object[expression.Length - length + 1];
            for (int i = 0; i < expression.Length; i++)
            {
                if (i > endIndex)
                {
                    processedExpression[i - endIndex] = expression[i];
                }
                else if (i > startIndex)
                {
                    continue;
                }
                else if (i == startIndex)
                {
                    processedExpression[i] = c;
                }
                else
                {
                    processedExpression[i] = expression[i];
                }
            }
            return(processedExpression);
        }