aboutsummaryrefslogtreecommitdiff
path: root/src/compiler/builtins.zc
diff options
context:
space:
mode:
authorGravatar zinc <zinc@downsta.ir> 2026-10-14 16:48:18 +0330
committerGravatar zinc <zinc@downsta.ir> 2026-10-14 16:48:18 +0330
commit37faa46eff8210365b144ccb4758dc3967b68b48 (patch)
treea7352e8e6dd6563146444bf4859c831a26894a32 /src/compiler/builtins.zc
parentMerge branch 'main' into work/spec (diff)
enum: a member is defined as soon as it is declared, so later values can name it (appendix b.2.2)
Diffstat (limited to 'src/compiler/builtins.zc')
-rw-r--r--src/compiler/builtins.zc8
1 files changed, 6 insertions, 2 deletions
diff --git a/src/compiler/builtins.zc b/src/compiler/builtins.zc
index 214d493..6dc586a 100644
--- a/src/compiler/builtins.zc
+++ b/src/compiler/builtins.zc
@@ -71,8 +71,10 @@ int ptagged(word t) {
// enum name { A, B = 5 } is a plain enum: each member is a constant, plain (A)
// and scoped (name.A). a member with a block of fields makes it a tagged enum
// (see enum_tagged): enum name { circle { int r; }, dot }.
+// the first member of the enum being read, so a member's value can name the members before it; -1 outside
+int enum_open = 0 - 1;
int kw_enum() {
- char* n; char* en = 0; int first = npm, cnt = 0, tagged = 0, gi, nf, v = 0, et = 0, sk, ft; char* fname;
+ char* n; char* en = 0; int first = npm, cnt = 0, tagged = 0, gi, nf, v = 0, et = 0, sk, ft, rv = 0; char* fname;
stmtonly();
next();
if (tk == T_ID) { en = tv; next(); }
@@ -97,7 +99,9 @@ int kw_enum() {
expect(s_semi);
}
expect(s_rb);
- } else if (accept(s_eq)) { pm_val[gi] = cexpr(); pm_exp[gi] = 1; }
+ } else { enum_open = first; if (accept(s_eq)) { pm_val[gi] = cexpr(); pm_exp[gi] = 1; } enum_open = 0 - 1; }
+ if (pm_exp[gi]) rv = pm_val[gi]; else if (!pm_nf[gi]) pm_val[gi] = rv; // the running value, read by cprim
+ rv++;
cnt++;
if (!accept(s_comma)) break;
if (is_op(s_rb)) break;