Example #1
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();
        }
Example #2
0
        public static FunctionHeader Header(MatchIterator iterator)
        {
            iterator.AssertTokenType(TokenType.Type, out Token typeToken);
            iterator.AssertTokenType(TokenType.Name, out Token nameToken);
            iterator.AssertKeySymbol(Symbols.OpenBracket, out _);
            Token open = (Token)iterator.Current;

            CollectedParameters parameters;

            if (iterator.Next(out parameters))
            {
                iterator.MoveNext();
            }
            else
            {
                parameters = new CollectedParameters();
            }

            iterator.AssertKeySymbol(Symbols.CloseBracket, out _);
            Token end = (Token)iterator.Current;

            iterator.AssertEnd();

            FunctionTypeSignature typeSignature = new FunctionTypeSignature(parameters.Types);
            FunctionSignature     signature     = new FunctionSignature(
                FunctionSignature.Local, nameToken.Content, typeSignature);

            FunctionHeader result = new FunctionHeader(
                typeToken, nameToken, open, end, signature, parameters.Names);

            return(result);
        }
Example #3
0
        public static FunctionCall Call(MatchIterator iterator)
        {
            // It would be cool in patterner v2 if the pattern was constructed in the same method as the finalizer.
            // For example, a "dummy" matchIterator could be passed through here and pick up the requirements, perhaps?
            // This would, of course, require the big feature of pattern simplification
            // It would also require defining a special "Expression" subpattern? At this point it would be better if everything were an expression, I guess

            iterator.AssertTokenType(TokenType.Name, out Token name);
            iterator.AssertKeySymbol(Symbols.OpenBracket, out _);
            Token open = (Token)iterator.Current;

            if (iterator.Next(out List <Expression> arguments))
            {
                iterator.MoveNext();
            }

            else
            {
                arguments = new List <Expression>();
            }

            iterator.AssertKeySymbol(Symbols.CloseBracket, out _);
            Token end = (Token)iterator.Current;

            iterator.AssertEnd();

            FunctionCall result = new FunctionCall(name, open, end, arguments);

            return(result);
        }
Example #4
0
        public static VariableAssignment Assignment(MatchIterator iterator)
        {
            if (!iterator.NextIsToken(TokenType.Type, out Token type))
            {
                type = null;
            }

            iterator.AssertTokenType(TokenType.Name, out Token name);


            if (iterator.Next(out List <CollectedIndex> indices))
            {
                iterator.MoveNext();
            }

            else
            {
                indices = new List <CollectedIndex>(0);
            }

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

            if (!iterator.NextIsExpression(out Expression value))
            {
                value = null;
            }

            iterator.AssertEnd();

            return(new VariableAssignment(type, name, indices, op, value));
        }
Example #5
0
        public static VariableReference CreateReference(MatchIterator iterator)
        {
            iterator.AssertTokenType(TokenType.Name, out Token token);
            iterator.AssertEnd();

            return(new VariableReference(token));
        }
Example #6
0
        public static Command Create(MatchIterator iterator)
        {
            iterator.AssertTokenType(TokenType.KeyWord, out Token keyWord);
            iterator.AssertEnd();

            Command result = new Command(keyWord);

            return(result);
        }
Example #7
0
        public static ElseHeader Else(MatchIterator iterator)
        {
            iterator.AssertKeyword(Keywords.Else, out Token elseToken);
            iterator.AssertEnd();

            ElseHeader result = new ElseHeader(elseToken);

            return(result);
        }
Example #8
0
        public static LiteralGet Create(MatchIterator iterator)
        {
            iterator.AssertToken(out Token token);
            LiteralGet result = new LiteralGet(token);

            iterator.AssertEnd();

            return(result);
        }
Example #9
0
        public static IfHeader If(MatchIterator iterator)
        {
            iterator.AssertKeyword(Keywords.If, out Token ifToken);
            iterator.AssertExpression(out Expression condition);
            iterator.AssertEnd();

            IfHeader result = new IfHeader(ifToken, condition);

            return(result);
        }
Example #10
0
        public static ElseIfHeader ElseIf(MatchIterator iterator)
        {
            iterator.AssertKeyword(Keywords.Else, out Token elseToken);
            iterator.AssertKeyword(Keywords.If, out Token ifToken);
            iterator.AssertExpression(out Expression condition);
            iterator.AssertEnd();

            ElseIfHeader result = new ElseIfHeader(elseToken, ifToken, condition);

            return(result);
        }
Example #11
0
        public static Return Return(MatchIterator iterator)
        {
            iterator.AssertKeyword(Keywords.Return);
            Token startToken = (Token)iterator.Current;

            Expression expression;

            iterator.NextIsExpression(out expression);

            iterator.AssertEnd();

            Return result = new Return(startToken, expression);

            return(result);
        }
Example #12
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);
        }
Example #13
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);
        }