aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-09-01 22:44:45 +0900
committernsfisis <nsfisis@gmail.com>2025-09-01 22:44:45 +0900
commit394899b2d7e75b7a922d3a402abc8f85a82b031c (patch)
tree66b441e538e12f456a84e6c22f1f7ca2c1e17407
parent27fc021e6771e404d01db447a1e5804911b4d6f9 (diff)
downloadducc-394899b2d7e75b7a922d3a402abc8f85a82b031c.tar.gz
ducc-394899b2d7e75b7a922d3a402abc8f85a82b031c.tar.zst
ducc-394899b2d7e75b7a922d3a402abc8f85a82b031c.zip
feat: support enum with explicit value
-rw-r--r--src/parse.c18
-rw-r--r--tests/113.sh26
2 files changed, 43 insertions, 1 deletions
diff --git a/src/parse.c b/src/parse.c
index 1eea182..0a63870 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -1247,6 +1247,16 @@ static AstNode* parse_enum_member(Parser* p) {
const Token* name = parse_ident(p);
AstNode* member = ast_new(AstNodeKind_enum_member);
member->name = name->value.string;
+
+ if (consume_token_if(p, TokenKind_assign)) {
+ // TODO: support other kinds of constant expression.
+ const Token* v = expect(p, TokenKind_literal_int);
+ member->node_int_value = v->value.integer;
+ } else {
+ // TODO: use another special value for placeholder because -1 is a valid value.
+ member->node_int_value = -1;
+ }
+
return member;
}
@@ -1255,8 +1265,14 @@ static AstNode* parse_enum_members(Parser* p) {
AstNode* list = ast_new_list(16);
while (peek_token(p)->kind != TokenKind_brace_r) {
AstNode* member = parse_enum_member(p);
- member->node_int_value = next_value;
+
+ if (member->node_int_value != -1) {
+ next_value = member->node_int_value;
+ } else {
+ member->node_int_value = next_value;
+ }
++next_value;
+
ast_append(list, member);
if (!consume_token_if(p, TokenKind_comma)) {
break;
diff --git a/tests/113.sh b/tests/113.sh
new file mode 100644
index 0000000..94e6e5f
--- /dev/null
+++ b/tests/113.sh
@@ -0,0 +1,26 @@
+cat <<'EOF' > expected
+10,11,20,21
+0,5,6
+EOF
+
+test_diff <<'EOF'
+int printf();
+
+enum E1 {
+ A = 10,
+ B,
+ C = 20,
+ D,
+};
+
+enum E2 {
+ E,
+ F = 5,
+ G
+};
+
+int main() {
+ printf("%d,%d,%d,%d\n", A, B, C, D);
+ printf("%d,%d,%d\n", E, F, G);
+}
+EOF