Exemple #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));
        }
Exemple #2
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);
        }
Exemple #3
0
        public static List <Expression> Arguments(MatchIterator iterator)
        {
            List <Expression> arguments = new List <Expression>();

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

            do
            {
                iterator.AssertExpression(out Expression argument);
                arguments.Add(argument);
            }while (iterator.NextIsKeySymbol(Symbols.Comma));

            iterator.AssertEnd();

            return(arguments);
        }
Exemple #4
0
        public static CollectedParameters Parameters(MatchIterator iterator)
        {
            List <SprakType>    parameterTypes = new List <SprakType>();
            List <string>       parameterNames = new List <string>();
            CollectedParameters result
                = new CollectedParameters(parameterTypes, parameterNames);

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

            do
            {
                iterator.AssertType(out SprakType type);
                iterator.AssertName(out string name);
                parameterTypes.Add(type);
                parameterNames.Add(name);
            }while (iterator.NextIsKeySymbol(Symbols.Comma));

            iterator.AssertEnd();

            return(result);
        }