Beispiel #1
0
        public static Expression CreateOld(MatchIterator iterator)
        {
            Token opToken;

            if (iterator.NextIsToken(TokenType.Operator, out opToken))
            {
                // assume a left operator for now
                Expression righExpr = Create(iterator);
                return(new OperatorCall(opToken, null, righExpr));
            }

            Expression nextExpr;

            if (iterator.NextIsKeySymbol(Symbols.OpenBracket))
            {
                iterator.AssertExpression(out nextExpr);
                iterator.AssertKeySymbol(Symbols.CloseBracket, out _);
            }

            else
            {
                iterator.AssertExpression(out nextExpr);
            }

            if (iterator.AtEnd())
            {
                return(nextExpr);
            }

            if (iterator.Next(out List <CollectedIndex> indices))
            {
                foreach (CollectedIndex index in indices)
                {
                    nextExpr = new Indexer(nextExpr, index.Open, index.Index, index.Close);
                }

                iterator.MoveNext();
            }

            if (iterator.AtEnd())
            {
                return(nextExpr);
            }

            iterator.AssertTokenType(TokenType.Operator, out opToken);

            if (iterator.AtEnd())
            {
                // Assume a right operator for now
                return(new OperatorCall(opToken, nextExpr, null));
            }

            // Assume a binary operator for now
            Expression remainder = Create(iterator);

            return(new OperatorCall(opToken, nextExpr, remainder));
        }
Beispiel #2
0
        public static LoopHeader Loop(MatchIterator iterator)
        {
            Expression array;

            iterator.AssertKeyword(Keywords.Loop);
            Token loop = (Token)iterator.Current;

            if (iterator.AtEnd())
            {
                LoopHeader result = new LoopHeader(loop);
                return(result);
            }

            iterator.AssertExpression(out Expression firstExpression);

            if (iterator.AtEnd())
            {
                // Actually checking that this expression is an array is for
                // a later step, when the messenger is available.
                array = firstExpression;
                LoopHeader result = new LoopHeader(loop, array);
                return(result);
            }

            if (iterator.NextIsKeyword(Keywords.In, out Token inToken))
            {
                // Again, actually checking that this is indeed just a name
                // has to wait for a later stage.
                Expression name = firstExpression;

                iterator.AssertExpression(out array);
                iterator.AssertEnd();

                LoopHeader result = new LoopHeader(loop, name, inToken, array);
                return(result);
            }

            if (iterator.NextIsKeyword(Keywords.From, out Token from))
            {
                Expression name = firstExpression;

                iterator.AssertExpression(out Expression start);
                iterator.AssertKeyword(Keywords.To, out Token to);
                iterator.AssertExpression(out Expression end);
                iterator.AssertEnd();

                LoopHeader result = new LoopHeader(
                    loop, name, from, start, to, end);
                return(result);
            }

            throw iterator.UnexpectedEnd();
        }
Beispiel #3
0
        private static Expression GetValue(MatchIterator iterator)
        {
            if (iterator.NextIsToken(TokenType.Operator, out Token opToken))
            {
                // Early out: if we are starting with an operator, than it is
                // a right unary operator, and unary ops have the highest
                // precedence at the moment.
                return(new OperatorCall(opToken, null, GetValue(iterator)));
            }

            Expression expression;

            // We don't have to recursively descend through the brackets -
            // that is handled at the pattern matching level. As far as we are
            // concerned, there are either expressions or expressions wrapped
            // in brackets.

            if (iterator.NextIsKeySymbol(Symbols.OpenBracket, out Token open))
            {
                iterator.AssertExpression(out Expression value);
                iterator.AssertKeySymbol(Symbols.CloseBracket, out Token close);

                expression = new Group(open, value, close);
            }
            else
            {
                iterator.AssertExpression(out expression);
            }

            // Finally, consider any indexing that follow the expression.
            // Sprak only allows one level of this, but that will be enforced
            // later where a more friendly error can be displayed.

            if (iterator.Next(out List <CollectedIndex> indices))
            {
                foreach (CollectedIndex index in indices)
                {
                    expression = new Indexer(expression, index.Open, index.Index, index.Close);
                }

                iterator.MoveNext();
            }

            return(expression);
        }