Ejemplo n.º 1
0
        static Token EvalParenthesis(
            Token root)
        {
            var openingParenthesis = root.FindLast(TokenType.OpeningParenthesis);

            while (openingParenthesis != null)
            {
                var closingParenthesis = openingParenthesis.Find(TokenType.ClosingParenthesis);

                if (closingParenthesis == null)
                {
                    throw new EvaluationException("Missing closing parenthesis.");
                }

                var previous = openingParenthesis.TruncatePrevious();
                var next     = closingParenthesis.TruncateNext();

                openingParenthesis = openingParenthesis.TruncateNext();
                closingParenthesis = closingParenthesis.TruncatePrevious();

                var token = EvalFunctionsAndOperators(openingParenthesis);
                var value = token.As <OperandToken>().Value;

                root = new OperandToken(value);

                root.ReplacePrevious(previous);
                root.ReplaceNext(next);

                openingParenthesis = root.Find(TokenType.OpeningParenthesis, true);
            }

            return(root.FindRoot());
        }