示例#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));
        }
示例#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();
        }
示例#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);
        }
示例#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);
        }
示例#5
0
        public static Expression Create(MatchIterator iterator)
        {
            LinkedList <object> items = new LinkedList <object>();

            iterator.AssertNext();

            // postfix unary operators are not supported here
            // (Like i--, etc)
            // I don't think they are really needed for sprak, and defaulting
            // to assuming prefix simplifies things.

            while (true)
            {
                items.AddLast(GetValue(iterator));

                if (iterator.AtEnd())
                {
                    break;
                }

                iterator.AssertTokenType(TokenType.Operator, out Token op);
                items.AddLast(op);

                if (iterator.AtEnd())
                {
                    break;
                }
            }

            void ParseOperatorToken(LinkedListNode <object> opNode)
            {
                Token token = (Token)opNode.Value;
                LinkedListNode <object> leftNode  = opNode.Previous;
                LinkedListNode <object> rightNode = opNode.Next;

                Expression   left   = (Expression)leftNode.Value;
                Expression   right  = (Expression)rightNode?.Value;
                OperatorCall result = new OperatorCall(token, left, right);

                items.AddBefore(leftNode, result);

                items.Remove(leftNode);
                items.Remove(opNode);

                if (rightNode != null)
                {
                    items.Remove(rightNode);
                }
            }

            LinkedListNode <object> current, next;

            foreach (Operator[] group in Operator.OperatorPrecedenceGroups)
            {
                current = items.First.Next;

                while (current != null)
                {
                    next = current.Next?.Next;

                    Token token = (Token)current.Value;
                    if (Operator.TryParse(out Operator op, text:token.Content))
                    {
                        if (group.Contains(op))
                        {
                            ParseOperatorToken(current);
                        }
                    }

                    current = next;
                }
            }

            current = items.First.Next;
            while (current != null)
            {
                ParseOperatorToken(current);
                current = current.Next?.Next;
            }

            if (items.First.Next != null)
            {
                throw new Exception("Unable to parse all operators");
            }

            return((Expression)items.First.Value);
        }