示例#1
0
        public Declaration Parse(LoonyParser parser, LoonyToken token)
        {
            var start = token;

            var      name       = parser.Take(LoonyTokenType.Identifier);
            var      parameters = new List <FuncDeclaration.Parameter>();
            TypeBase returnType = null;

            parser.Take(LoonyTokenType.LeftParen);

            if (!parser.MatchAndTake(LoonyTokenType.RightParen))
            {
                do
                {
                    var paramName = parser.Take(LoonyTokenType.Identifier);
                    parser.Take(LoonyTokenType.Colon);
                    var paramType = parser.ParseType();

                    parameters.Add(new FuncDeclaration.Parameter(paramName, paramType));
                } while (parser.MatchAndTake(LoonyTokenType.Comma));

                parser.Take(LoonyTokenType.RightParen);
            }

            if (parser.MatchAndTake(LoonyTokenType.Colon))
            {
                returnType = parser.ParseType();
            }

            var body = parser.ParseBlock(false);

            var end = parser.Previous;

            return(new FuncDeclaration(start, end, name, parameters, returnType, body));
        }
示例#2
0
        public Declaration Parse(LoonyParser parser, LoonyToken token)
        {
            var start = token;

            var name = parser.Take(LoonyTokenType.Identifier);
            var parameters = new List<FuncDeclaration.Parameter>();
            TypeBase returnType = null;

            parser.Take(LoonyTokenType.LeftParen);

            if (!parser.MatchAndTake(LoonyTokenType.RightParen))
            {
                do
                {
                    var paramName = parser.Take(LoonyTokenType.Identifier);
                    parser.Take(LoonyTokenType.Colon);
                    var paramType = parser.ParseType();

                    parameters.Add(new FuncDeclaration.Parameter(paramName, paramType));
                } while (parser.MatchAndTake(LoonyTokenType.Comma));

                parser.Take(LoonyTokenType.RightParen);
            }

            if (parser.MatchAndTake(LoonyTokenType.Colon))
                returnType = parser.ParseType();

            var body = parser.ParseBlock(false);

            var end = parser.Previous;

            return new FuncDeclaration(start, end, name, parameters, returnType, body);
        }
示例#3
0
文件: Types.cs 项目: Rohansi/LoonyC
        private static TypeBase Parse(string type)
        {
            var lexer  = new LoonyLexer(type);
            var parser = new LoonyParser(lexer);

            return(parser.ParseType());
        }
示例#4
0
        public Statement Parse(LoonyParser parser, LoonyToken token, out bool trailingSemicolon)
        {
            trailingSemicolon = true;

            var        name        = parser.Take(LoonyTokenType.Identifier).Contents;
            TypeBase   type        = null;
            Expression initializer = null;

            if (parser.MatchAndTake(LoonyTokenType.Colon))
            {
                type = parser.ParseType();
            }

            if (type == null)
            {
                parser.Take(LoonyTokenType.Assign);
                initializer = parser.ParseExpression();
            }
            else if (parser.MatchAndTake(LoonyTokenType.Assign))
            {
                initializer = parser.ParseExpression();
            }

            return(new VariableStatement(token, parser.Previous, name, type, initializer));
        }
示例#5
0
        public Statement Parse(LoonyParser parser, LoonyToken token, out bool trailingSemicolon)
        {
            trailingSemicolon = true;

            var name = parser.Take(LoonyTokenType.Identifier).Contents;
            TypeBase type = null;
            Expression initializer = null;

            if (parser.MatchAndTake(LoonyTokenType.Colon))
                type = parser.ParseType();

            if (type == null)
            {
                parser.Take(LoonyTokenType.Assign);
                initializer = parser.ParseExpression();
            }
            else if (parser.MatchAndTake(LoonyTokenType.Assign))
            {
                initializer = parser.ParseExpression();
            }

            return new VariableStatement(token, parser.Previous, name, type, initializer);
        }