Parse named parameters of functions

This commit is contained in:
Wolfgang Bumiller 2012-07-26 23:33:19 +02:00
parent 5d766f60b2
commit a99972d7dd

View file

@ -118,6 +118,7 @@ static ast_value *parser_parse_type(parser_t *parser, int basetype, bool *isfunc
lex_ctx ctx = parser_ctx(parser); lex_ctx ctx = parser_ctx(parser);
int vtype = basetype; int vtype = basetype;
int temptype; int temptype;
size_t i;
MEM_VECTOR_INIT(&params, p); MEM_VECTOR_INIT(&params, p);
@ -129,54 +130,56 @@ static ast_value *parser_parse_type(parser_t *parser, int basetype, bool *isfunc
ast_value *param; ast_value *param;
bool dummy; bool dummy;
if (!parser_next(parser)) { if (!parser_next(parser))
MEM_VECTOR_CLEAR(&params, p); goto on_error;
return NULL;
}
if (parser->tok == ')') if (parser->tok == ')')
break; break;
temptype = parser_token(parser)->constval.t; temptype = parser_token(parser)->constval.t;
if (!parser_next(parser)) { if (!parser_next(parser))
MEM_VECTOR_CLEAR(&params, p); goto on_error;
return NULL;
}
param = parser_parse_type(parser, temptype, &dummy); param = parser_parse_type(parser, temptype, &dummy);
(void)dummy; (void)dummy;
if (!param) { if (!param)
MEM_VECTOR_CLEAR(&params, p); goto on_error;
return NULL;
if (parser->tok == TOKEN_IDENT) {
/* named parameter */
if (!ast_value_set_name(param, parser_tokval(parser)))
goto on_error;
if (!parser_next(parser))
goto on_error;
} }
if (!paramlist_t_p_add(&params, param)) { if (!paramlist_t_p_add(&params, param)) {
MEM_VECTOR_CLEAR(&params, p);
parseerror(parser, "Out of memory while parsing typename"); parseerror(parser, "Out of memory while parsing typename");
return NULL; goto on_error;
} }
if (parser->tok == ',') if (parser->tok == ',')
continue; continue;
if (parser->tok == ')') if (parser->tok == ')')
break; break;
MEM_VECTOR_CLEAR(&params, p);
parseerror(parser, "Unexpected token"); parseerror(parser, "Unexpected token");
return NULL; goto on_error;
}
if (!parser_next(parser)) {
MEM_VECTOR_CLEAR(&params, p);
return NULL;
} }
if (!parser_next(parser))
goto on_error;
} }
var = ast_value_new(ctx, "<unnamed>", vtype); var = ast_value_new(ctx, "<unnamed>", vtype);
if (!var) { if (!var)
MEM_VECTOR_CLEAR(&params, p); goto on_error;
return NULL;
}
MEM_VECTOR_MOVE(&params, p, var, params); MEM_VECTOR_MOVE(&params, p, var, params);
return var; return var;
on_error:
for (i = 0; i < params.p_count; ++i)
ast_value_delete(params.p[i]);
MEM_VECTOR_CLEAR(&params, p);
return NULL;
} }
typedef struct typedef struct