diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-09-15 12:35:47 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-09-15 13:00:31 +0900 |
| commit | c12aa625ce7fed32f1416b9c711ea9d6e960e002 (patch) | |
| tree | edcd116017cb84fa07189e8c650c34206cb275a4 | |
| parent | 93c25fde8b26da97a7984c48cbc9f0f7f6037483 (diff) | |
| download | ducc-c12aa625ce7fed32f1416b9c711ea9d6e960e002.tar.gz ducc-c12aa625ce7fed32f1416b9c711ea9d6e960e002.tar.zst ducc-c12aa625ce7fed32f1416b9c711ea9d6e960e002.zip | |
fix: issue where enumeration cannot be referenced until enum definition completes
| -rw-r--r-- | src/parse.c | 12 | ||||
| -rw-r--r-- | tests/test_enums.sh | 8 |
2 files changed, 12 insertions, 8 deletions
diff --git a/src/parse.c b/src/parse.c index 99db2b5..e9e9312 100644 --- a/src/parse.c +++ b/src/parse.c @@ -1453,9 +1453,14 @@ static AstNode* parse_enum_member(Parser* p) { return member; } -static AstNode* parse_enum_members(Parser* p) { +static void parse_enum_members(Parser* p, int enum_idx) { int next_value = 0; AstNode* list = ast_new_list(16); + + if (!p->enums->node_items[enum_idx].node_members) { + p->enums->node_items[enum_idx].node_members = list; + } + while (peek_token(p)->kind != TokenKind_brace_r) { AstNode* member = parse_enum_member(p); @@ -1469,11 +1474,11 @@ static AstNode* parse_enum_members(Parser* p) { ++next_value; ast_append(list, member); + if (!consume_token_if(p, TokenKind_comma)) { break; } } - return list; } void parse_typedef_decl(Parser* p, AstNode* decls) { @@ -1673,9 +1678,8 @@ static Type* parse_enum_specifier(Parser* p) { enum_idx = p->enums->node_len - 1; } - AstNode* members = parse_enum_members(p); + parse_enum_members(p, enum_idx); expect(p, TokenKind_brace_r); - p->enums->node_items[enum_idx].node_members = members; Type* ty = type_new(TypeKind_enum); ty->ref.defs = p->enums; diff --git a/tests/test_enums.sh b/tests/test_enums.sh index 228786a..3fc15a0 100644 --- a/tests/test_enums.sh +++ b/tests/test_enums.sh @@ -21,7 +21,7 @@ EOF cat <<'EOF' > expected 10,11,20,21 -0,5,6 +0,5,6,6 EOF test_diff <<'EOF' @@ -37,12 +37,12 @@ enum E1 { enum E2 { E, F = 5, - G + G, + H = G, }; int main() { printf("%d,%d,%d,%d\n", A, B, C, D); - printf("%d,%d,%d\n", E, F, G); + printf("%d,%d,%d,%d\n", E, F, G, H); } EOF - |
