示例#1
0
        public static IEnumerable <Element> ParseNumberWithUnaryElements(string input)
        {
            string[] components = input.SeparateUnaryElements();

            // the nice thing about all the unary operators is that they're one character
            int currentComponentIndex = 0;
            int currentCharacterIndex = 0;

            if (currentComponentIndex == 1) // if we're in the number
            {
                currentComponentIndex = 2;
                yield return(Element.Parse(components[1]));
            }
            else
            {
                if (currentCharacterIndex < components[currentComponentIndex].Length)
                {
                    yield return(Element.ParseUnaryOperator(components[currentComponentIndex][currentCharacterIndex++].ToString(), currentComponentIndex == 0));
                }
                else if (currentCharacterIndex == components[currentComponentIndex].Length)
                {
                    if (currentComponentIndex == 0)
                    {
                        currentComponentIndex = 1;
                    }
                    else
                    {
                        yield break;
                    }
                }
            }
        }
示例#2
0
        private static List <Element> ParseElements(string input)
        {
            input = SpaceElements(input);
            List <Element> result = new List <Element>();

            foreach (string elementString in input.Split(' '))
            {
                if (elementString.Any(c => c == '+' || c == '-' || c == '~' || c == '!'))
                {
                    var elementComponents = Element.ParseNumberWithUnaryElements(elementString);
                    result.AddRange(elementComponents); // so much for that yield, I guess
                }

                result.Add(Element.Parse(elementString));
            }

            return(result);
        }