commit e55ff4a43ab4f309b64bd5e68d906ac2474f2d54
parent 4a1efa52ffa00714b4bd1dc7b10998d81b5d7115
Author: erai <erai@omiltem.net>
Date: Sat, 16 Mar 2024 13:42:34 -0400
enum constants
Diffstat:
4 files changed, 68 insertions(+), 10 deletions(-)
diff --git a/bootstrap.sh b/bootstrap.sh
@@ -6,12 +6,10 @@ uptodate() {
target=$1
shift
if ! [ -f "${target}" ]; then
- echo "${target} does not exist" >&2
return 1
fi
while [ $# -gt 0 ]; do
if ! [ -f "$1" ] || [ "$1" -nt "${target}" ]; then
- echo "${target} stale $1" >&2
return 1
fi
shift
diff --git a/cc0.c b/cc0.c
@@ -587,6 +587,7 @@ enum {
N_CONDLIST,
N_COND,
N_ENUM,
+ N_ENUMITEM,
N_ENUMLIST,
N_LOOP,
N_BREAK,
@@ -1844,7 +1845,33 @@ member_decl(void)
return mknode(N_MEMBERDECL, a, b);
}
-// enum_list := ident
+// enum_item := ident
+// | ident '=' num
+struct node *
+enum_item(void)
+{
+ struct node *a;
+ struct node *b;
+
+ a = ident();
+ if (!a) {
+ return 0;
+ }
+
+ if (tt != T_ASSIGN) {
+ return mknode(N_ENUMITEM, a, 0);
+ }
+ feed();
+
+ b = num();
+ if (!b) {
+ die("expected num");
+ }
+
+ return mknode(N_ENUMITEM, a, b);
+}
+
+// enum_list := enum_item
// | enum_list ',' enum_list
struct node *
enum_list(void)
@@ -1853,7 +1880,7 @@ enum_list(void)
struct node *e;
struct node *a;
- a = ident();
+ a = enum_item();
if (!a) {
return 0;
}
@@ -1867,7 +1894,7 @@ enum_list(void)
}
feed();
- a = ident();
+ a = enum_item();
if (!a) {
return n;
}
@@ -3158,12 +3185,16 @@ defenum(struct node *n)
break;
}
- d = efind(n->a->s, 1);
+ d = efind(n->a->a->s, 1);
if (d->defined) {
die("duplicate enum");
}
+ if (n->a->b) {
+ i = n->a->b->n;
+ }
+
d->defined = 1;
d->val = i;
diff --git a/cc1.c b/cc1.c
@@ -230,13 +230,17 @@ defenum(c: *compiler, n: *node) {
break;
}
- name = n.a.s;
+ name = n.a.a.s;
d = find(c, name, 0:*byte, 1);
if (d.enum_defined) {
cdie(c, "duplicate enum");
}
+ if (n.a.b) {
+ i = n.a.b.n;
+ }
+
d.enum_defined = 1;
d.enum_value = i;
d.enum_def = n;
diff --git a/parse1.c b/parse1.c
@@ -32,6 +32,7 @@ enum {
N_CONDLIST,
N_COND,
N_ENUM,
+ N_ENUMITEM,
N_ENUMLIST,
N_LOOP,
N_BREAK,
@@ -1099,14 +1100,38 @@ parse_stmt_list(c: *compiler): *node {
}
}
-// enum_list := ident
+// enum_item := ident
+// | ident '=' num
+parse_enum_item(c: *compiler): *node {
+ var a: *node;
+ var b: *node;
+
+ a = parse_ident(c);
+ if (!a) {
+ return 0:*node;
+ }
+
+ if (c.tt != T_ASSIGN) {
+ return mknode1(c, N_ENUMITEM, a);
+ }
+ feed(c);
+
+ b = parse_num(c);
+ if (!b) {
+ cdie(c, "expected num");
+ }
+
+ return mknode(c, N_ENUMITEM, a, b);
+}
+
+// enum_list := enum_item
// | enum_list ',' enum_list
parse_enum_list(c: *compiler): *node {
var n: *node;
var e: *node;
var a: *node;
- a = parse_ident(c);
+ a = parse_enum_item(c);
if (!a) {
return 0:*node;
}
@@ -1120,7 +1145,7 @@ parse_enum_list(c: *compiler): *node {
}
feed(c);
- a = parse_ident(c);
+ a = parse_enum_item(c);
if (!a) {
return n;
}