Ejemplo n.º 1
0
        private AstItem ReadArray(TokenIterator tokens)
        {
            tokens.PopExpected(TokenKind.SquareBracketOpen);
            var firstValue = ReadExpression(tokens, new List <TokenKind> {
                TokenKind.SemiColon, TokenKind.Comma
            });

            var token = tokens.Pop();

            if (token.Kind == TokenKind.SquareBracketClose)
            {
                //Array with only single value
                return(AstItem.ValueArrayDefinition(new List <AstItem>()
                {
                    firstValue
                }));
            }
            if (token.Kind == TokenKind.Comma)
            {
                //Array definition with n values
                var valueTokens = ReadBreaketedList(tokens, TokenKind.SquareBracketOpen, TokenKind.SquareBracketClose, TokenKind.Comma);

                var valueExpressions = new List <AstItem>(valueTokens.Count + 1)
                {
                    firstValue
                };

                foreach (var tokenList in valueTokens)
                {
                    var iterator  = new TokenIterator(tokenList);
                    var valueItem = ReadExpression(iterator, null);
                    valueExpressions.Add(valueItem);
                }

                return(AstItem.ValueArrayDefinition(valueExpressions));
            }
            else if (token.Kind == TokenKind.SemiColon)
            {
                //Array definition with one value and number of items
                var numItemsExpression = ReadExpression(tokens, TokenKind.SquareBracketClose);
                return(AstItem.SizedArrayDefinition(firstValue, numItemsExpression));
            }

            throw new Exception("Unexpected token in array definition: " + token);
        }