public static IElement Parse(IReadOnlyList <Token> tokens)
        {
            var  result  = new BinaryOperation();
            bool haveLHS = false;

            for (int i = 0; i < tokens.Count; i++)
            {
                var token = tokens[i];

                // look at the type of token
                switch (token.MyType)
                {
                case Token.Type.Integer:
                    var integer = new Integer(int.Parse(token.Text));
                    if (!haveLHS)
                    {
                        result.Left = integer;
                        haveLHS     = true;
                    }
                    else
                    {
                        result.Right = integer;
                    }
                    break;

                case Token.Type.Plus:
                    result.MyType = BinaryOperation.Type.Addition;
                    break;

                case Token.Type.Minus:
                    result.MyType = BinaryOperation.Type.Subtraction;
                    break;

                case Token.Type.Lparen:
                    int j = i;
                    for (; j < tokens.Count; ++j)
                    {
                        if (tokens[j].MyType == Token.Type.Rparen)
                        {
                            break;     // found it!
                        }
                    }
                    // process subexpression w/o opening (
                    var subexpression = tokens.Skip(i + 1).Take(j - i - 1).ToList();
                    var element       = Parse(subexpression);
                    if (!haveLHS)
                    {
                        result.Left = element;
                        haveLHS     = true;
                    }
                    else
                    {
                        result.Right = element;
                    }
                    i = j;     // advance
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        public IElement Parse(IReadOnlyList <Token> tokens)
        {
            var result  = new BinaryOperation();
            var haveLHS = false;

            for (var i = 0; i < tokens.Count; i++)
            {
                var token = tokens[i];
                switch (token.Type)
                {
                case Type.Integer:
                    var integer = new Integer(int.Parse(token.Text));
                    if (!haveLHS)
                    {
                        result.LeftElement = integer;
                        haveLHS            = true;
                    }
                    else
                    {
                        result.RightElement = integer;
                    }
                    break;

                case Type.Plus:
                    result.Type = BinaryOperationType.Addition;
                    break;

                case Type.Minus:
                    result.Type = BinaryOperationType.Subtraction;
                    break;

                case Type.LeftBracket:
                    int j = i;
                    for (; j < tokens.Count; ++j)
                    {
                        if (tokens[j].Type == Type.RightBracket)
                        {
                            break;
                        }
                    }
                    var subexpression = tokens.Skip(i + 1).Take(j - i - 1).ToList();
                    var element       = Parse(subexpression);
                    if (!haveLHS)
                    {
                        result.LeftElement = element;
                        haveLHS            = true;
                    }
                    else
                    {
                        result.RightElement = element;
                    }

                    i = j;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            return(result);
        }