BuilderNode ParseTree(ITokenCollection tokens)
        {
            Func <IToken, bool> splitPredicate = token =>
            {
                var maxWeight = tokens.OfType <OperationToken>().Any()
                    ? tokens.OfType <OperationToken>().Max(t => t.Weight)
                    : 0;

                var operationToken = token as OperationToken;
                if (operationToken != null && operationToken.Weight == maxWeight)
                {
                    return(true);
                }
                return(false);
            };

            var split = tokens.Split(splitPredicate);

            if (split.Delimiter == null)
            {
                var token = split.Left.First();
                return(ParseNode(token));
            }

            var node = new BuilderNode
            {
                Token = split.Delimiter,
                Left  = ParseTree(split.Left),
                Right = ParseTree(split.Right)
            };

            return(node);
        }
Пример #2
0
        public IEnumerable <IToken> Assemble(ITokenCollection tokens)
        {
            var digitTokens = tokens.OfType <DigitToken>().ToArray();
            var numberStr   = digitTokens.Aggregate("", (accumulator, token) => accumulator + token.Value);
            var number      = double.Parse(numberStr);

            yield return(new NumberToken(number));
        }
Пример #3
0
        public ITokenCollection Tokenize(ITokenCollection tokens)
        {
            if (!tokens.OfType <ParenthesisToken>().Any())
            {
                return(tokens);
            }

            var result              = new List <IToken>();
            var parentheses         = new List <IToken>();
            var isParenthesisOpened = false;
            var scopeDepth          = 0;

            foreach (var token in tokens)
            {
                if (isParenthesisOpened)
                {
                    if (token.IsParenthesis(Parenthesis.Left))
                    {
                        scopeDepth++;
                    }
                    else if (token.IsParenthesis(Parenthesis.Right))
                    {
                        scopeDepth--;

                        if (scopeDepth == 0)
                        {
                            parentheses = Tokenize(parentheses.ToTokenCollection()).ToList();

                            var parenthesesToken = new ParenthesesToken(new TokenCollection(parentheses));
                            result.Add(parenthesesToken);

                            parentheses.Clear();
                            isParenthesisOpened = false;
                            continue;
                        }
                    }
                    parentheses.Add(token);
                }
                else
                {
                    if (token.IsParenthesis(Parenthesis.Left))
                    {
                        isParenthesisOpened = true;
                        scopeDepth++;
                    }
                    else
                    {
                        result.Add(token);
                    }
                }
            }

            return(result.ToTokenCollection());
        }