commit cef3acde93573e24b6ec1288a76a2e9fb6bdae56
parent ca75d695f3401f362dce735d506213b55bcadca1
Author: Brian Swetland <swetland@frotz.net>
Date: Sun, 23 May 2021 23:35:02 -0700
compiler: more enums
- allow value assignments
- test value assignments
- test constant expressions
Diffstat:
3 files changed, 18 insertions(+), 0 deletions(-)
diff --git a/src/compiler.c b/src/compiler.c
@@ -1850,6 +1850,10 @@ void parse_enum_def() {
if (obj != nil) {
error("cannot redefine %s as enum tag\n", name->text);
}
+ if (ctx.tok == tASSIGN) {
+ next();
+ val = parse_init_constexpr(ctx.type_int32);
+ }
require(tCOMMA);
obj = make_var(oConst, name, ctx.type_int32, 0, val);
obj->next = ctx.scope->first;
diff --git a/test/1050-enums.log b/test/1050-enums.log
@@ -1,4 +1,10 @@
D 00000000
D 00000001
D 00000002
+D 00000005
+D 00000006
+D 10000000
+D 10000001
+D 00000064
+D 00000002
X 00000000
diff --git a/test/1050-enums.src b/test/1050-enums.src
@@ -3,10 +3,18 @@ var i i32 = 3;
enum { ZERO, ONE, TWO, };
+enum { A = 5, B, C = 0x10000000, D, E = 50 + 50, F = ONE * TWO, };
+
func start() i32 {
_hexout_(ZERO);
_hexout_(ONE);
_hexout_(TWO);
+ _hexout_(A);
+ _hexout_(B);
+ _hexout_(C);
+ _hexout_(D);
+ _hexout_(E);
+ _hexout_(F);
return 0;
}