From 17cb85dfb837949cd3a559fe8e99dee1f72463a4 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 23 Jan 2017 16:40:17 -0500 Subject: basic support for functions with variable length arguments See #77 --- src/parser.cpp | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'src/parser.cpp') diff --git a/src/parser.cpp b/src/parser.cpp index dbb6836e60..04c31fcfe0 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -250,16 +250,11 @@ static AstNode *ast_parse_type_expr(ParseContext *pc, size_t *token_index, bool } /* -ParamDecl = option("noalias" | "comptime") option(Symbol ":") TypeExpr | "..." +ParamDecl = option("noalias" | "comptime") option(Symbol ":") (TypeExpr | "...") */ static AstNode *ast_parse_param_decl(ParseContext *pc, size_t *token_index) { Token *token = &pc->tokens->at(*token_index); - if (token->id == TokenIdEllipsis) { - *token_index += 1; - return nullptr; - } - AstNode *node = ast_create_node(pc, NodeTypeParamDecl, token); if (token->id == TokenIdKeywordNoAlias) { @@ -282,7 +277,13 @@ static AstNode *ast_parse_param_decl(ParseContext *pc, size_t *token_index) { } } - node->data.param_decl.type = ast_parse_type_expr(pc, token_index, true); + Token *ellipsis_tok = &pc->tokens->at(*token_index); + if (ellipsis_tok->id == TokenIdEllipsis) { + *token_index += 1; + node->data.param_decl.is_var_args = true; + } else { + node->data.param_decl.type = ast_parse_type_expr(pc, token_index, true); + } return node; } @@ -304,12 +305,10 @@ static void ast_parse_param_decl_list(ParseContext *pc, size_t *token_index, for (;;) { AstNode *param_decl_node = ast_parse_param_decl(pc, token_index); bool expect_end = false; - if (param_decl_node) { - params->append(param_decl_node); - } else { - *is_var_args = true; - expect_end = true; - } + assert(param_decl_node); + params->append(param_decl_node); + expect_end = param_decl_node->data.param_decl.is_var_args; + *is_var_args = expect_end; Token *token = &pc->tokens->at(*token_index); *token_index += 1; -- cgit v1.2.3