From 16aa2c081db71181ba33aa205d2b0699efa29a99 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Tue, 2 Sep 2025 00:17:51 +0900 Subject: feat: support constant expression in array size --- src/parse.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/parse.c b/src/parse.c index 57dd316..ecd8e65 100644 --- a/src/parse.c +++ b/src/parse.c @@ -870,6 +870,8 @@ static Type* parse_pointer_opt(Parser* p, Type* ty) { return ty; } +static int eval(AstNode* e); + // array-declarator: // direct-declarator '[' TODO type-qualifier-list? TODO assignment-expression? ']' // TODO direct-declarator '[' 'static' type-qualifier-list? assignment-expression? ']' @@ -878,11 +880,8 @@ static Type* parse_pointer_opt(Parser* p, Type* ty) { static Type* parse_array_declarator_suffix(Parser* p, Type* ty) { next_token(p); // skip '[' - AstNode* size_expr = parse_expr(p); - if (size_expr->kind != AstNodeKind_int_expr) { - fatal_error("parse_var_decl: invalid array size"); - } - int size = size_expr->node_int_value; + AstNode* size_expr = parse_assignment_expr(p); + int size = eval(size_expr); expect(p, TokenKind_bracket_r); return type_new_array(ty, size); -- cgit v1.2.3-70-g09d2