Esempio n. 1
0
        public static IElement Parse(IReadOnlyList <Token> tokens)
        {
            bool isLHSPresent    = false;
            var  binaryOperation = new BinaryOperation();

            for (int i = 0; i < tokens.Count; i++)
            {
                switch (tokens[i].TokenType)
                {
                case Token.Type.Lparen:
                    int j = i + 1;
                    for (; j < tokens.Count; j++)
                    {
                        if (tokens[j].TokenType == Token.Type.Rparen)
                        {
                            break;
                        }
                    }
                    var subExp  = tokens.Skip(i).Take(j - i - 1).ToList();
                    var element = Parse(subExp);
                    if (isLHSPresent)
                    {
                        binaryOperation.RHS = element;
                    }
                    else
                    {
                        binaryOperation.LHS = element;
                        isLHSPresent        = true;
                    }
                    break;

                case Token.Type.Addition:
                    binaryOperation.OperationType = BinaryOperation.Operation.Addition;
                    break;

                case Token.Type.Subtraction:
                    binaryOperation.OperationType = BinaryOperation.Operation.Subtraction;
                    break;

                case Token.Type.Number:
                    if (isLHSPresent)
                    {
                        binaryOperation.RHS = new Integer(int.Parse(tokens[i].Text));
                    }
                    else
                    {
                        binaryOperation.LHS = new Integer(int.Parse(tokens[i].Text));
                        isLHSPresent        = true;
                    }
                    break;

                default:
                    break;
                }
            }
            return(binaryOperation);
        }
Esempio n. 2
0
        static IElement Parse(IReadOnlyList <Token> tokens)
        {
            var  result   = new BinaryOperation();
            bool haveLeft = false;

            for (int i = 0; i < tokens.Count; i++)
            {
                var token = tokens[i];
                switch (token.MyType)
                {
                case Token.Type.Integer:
                    var integer = new Integer(int.Parse(token.Text));
                    if (!haveLeft)
                    {
                        result.Left = integer;
                        haveLeft    = 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.LParentheses:
                    int j = i;
                    for (; j < tokens.Count; j++)
                    {
                        if (tokens[j].MyType == Token.Type.RParentheses)
                        {
                            break;
                        }
                    }
                    var subexpression = tokens.Skip(i + 1).Take(j - i - 1).ToList();
                    var element       = Parse(subexpression);
                    if (!haveLeft)
                    {
                        result.Left = element;
                        haveLeft    = true;
                    }
                    else
                    {
                        result.Right = element;
                    }
                    i = j;
                    break;
                }
            }
            return(result);
        }
        private IElement Parse(IReadOnlyList <Token> tokens)
        {
            var result = new BinaryOperation();

            if (tokens.Count == 1)
            {
                return(new Number(int.Parse(tokens[0].Text)));
            }

            result.Left          = new Number(int.Parse(tokens[0].Text));
            result.OperationType = (tokens[1].SignType == Sign.Plus) ?
                                   BinaryOperation.Type.Add :
                                   BinaryOperation.Type.Sub;
            result.Right = new Number(int.Parse(tokens[2].Text));

            var newTokens = new List <Token>();

            newTokens.Add(new Token(Sign.Number, result.Value.ToString()));
            newTokens.AddRange(tokens.Skip(3));

            return(Parse(newTokens));
        }
Esempio n. 4
0
        static IElement Parse(IReadOnlyList <Token> tokens)
        {
            var  result       = new BinaryOperation();
            bool haveLeftSide = false;

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

                case Enums.Type.Plus:
                    result.MyType = OperationType.Addition;
                    break;

                case Enums.Type.Minus:
                    result.MyType = OperationType.Substraction;
                    break;

                case Enums.Type.Lparen:
                    int j = i;
                    for (; j < tokens.Count; ++j)
                    {
                        if (tokens[j].MyType == Enums.Type.Rparen)
                        {
                            break;
                        }
                    }

                    var subExpression = tokens.Skip(i + 1).Take(j - i - 1).ToList();
                    var element       = Parse(subExpression);
                    if (!haveLeftSide)
                    {
                        result.Left  = element;
                        haveLeftSide = true;
                    }
                    else
                    {
                        result.Right = element;
                    }
                    i = j;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            return(result);
        }
Esempio n. 5
0
        private static IElement Parse(IReadOnlyList <Token> tokens)
        {
            var result  = new BinaryOperation();
            var haveLhs = false;

            for (var 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:
                    var 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;

                case Token.Type.Rparen:
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            return(result);
        }