Ejemplo n.º 1
0
        private Declaration ParseFuncDeclaration()
        {
            Token  start      = Match(TokenKind.Keyword_Func);
            string identifier = Match(TokenKind.Identifier).Text;

            // parse parameter list
            Match(TokenKind.LeftParenthesis);
            List <ParameterDeclaration> parameters = new List <ParameterDeclaration>();

            while (_lookahead.Kind != TokenKind.RightParenthesis)
            {
                Token name = Match(TokenKind.Identifier);
                Match(TokenKind.Colon);
                AST.Type type = ParseType();
                parameters.Add(new ParameterDeclaration(name.Position, name.Text, type));

                if (_lookahead.Kind != TokenKind.Comma)
                {
                    break;
                }

                Match(TokenKind.Comma);
            }
            Match(TokenKind.RightParenthesis);

            // parse return type specification
            Match(TokenKind.Colon);
            AST.Type returnType = ParseType();

            Match(TokenKind.Tilde);
            Expression body = ParseExpression();

            return(new FunctionDeclaration(start.Position, identifier, returnType, parameters.ToArray(), body));
        }
Ejemplo n.º 2
0
        private Declaration ParseVariableDeclaration()
        {
            Token start = Match(TokenKind.Keyword_Var);
            Token name  = Match(TokenKind.Identifier);

            Match(TokenKind.Colon);
            AST.Type type = ParseType();
            return(new VariableDeclaration(start.Position, name.Text, type));
        }