harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 2133a1303e94bc4222c5b8185f7a2fe2b2ca20e7
parent 65e0df8bc58d73997054d18634e591eb98323070
Author: Alexey Yerin <yyp@disroot.org>
Date:   Sat,  7 Aug 2021 13:55:01 +0300

gen: implement enum store/load

Signed-off-by: Alexey Yerin <yyp@disroot.org>

Diffstat:
Msrc/gen.c | 14++++++++++----
Atests/912-enums.ha | 36++++++++++++++++++++++++++++++++++++
Mtests/configure | 3++-
3 files changed, 48 insertions(+), 5 deletions(-)

diff --git a/src/gen.c b/src/gen.c @@ -115,7 +115,8 @@ gen_store(struct gen_context *ctx, struct gen_value object, struct gen_value value) { - switch (type_dealias(object.type)->storage) { + const struct type *ty = type_dealias(object.type); + switch (ty->storage) { case STORAGE_ARRAY: case STORAGE_SLICE: case STORAGE_STRING: @@ -128,7 +129,9 @@ gen_store(struct gen_context *ctx, gen_copy_memcpy(ctx, object, value); return; case STORAGE_ENUM: - assert(0); // TODO + object.type = builtin_type_for_storage(ty->_enum.storage, + (ty->flags & TYPE_CONST) != 0); + break; default: break; // no-op } @@ -142,7 +145,8 @@ gen_store(struct gen_context *ctx, static struct gen_value gen_load(struct gen_context *ctx, struct gen_value object) { - switch (type_dealias(object.type)->storage) { + const struct type *ty = type_dealias(object.type); + switch (ty->storage) { case STORAGE_ARRAY: case STORAGE_FUNCTION: case STORAGE_SLICE: @@ -153,7 +157,9 @@ gen_load(struct gen_context *ctx, struct gen_value object) case STORAGE_UNION: return object; case STORAGE_ENUM: - assert(0); // TODO + object.type = builtin_type_for_storage(ty->_enum.storage, + (ty->flags & TYPE_CONST) != 0); + break; default: break; // no-op } diff --git a/tests/912-enums.ha b/tests/912-enums.ha @@ -0,0 +1,36 @@ +type implicit = enum { + ZERO, + ONE, + TWO, +}; + +type explicit = enum { + NEGATIVE = -1, + FORTY_TWO = 42, + FORTY_THREE, +}; + +type with_storage = enum u16 { + ZERO, + A_LOT = 0xCAFE, +}; + +export fn main() int = { + assert(implicit::ZERO == 0); + assert(implicit::ONE == 1); + assert(implicit::TWO == 2); + const val = implicit::TWO; + assert(val == 2); + + assert(explicit::NEGATIVE == -1); + assert(explicit::FORTY_TWO == 42); + assert(explicit::FORTY_THREE == 43); + const val = explicit::FORTY_TWO; + assert(val == 42); + + assert(with_storage::ZERO == 0); + assert(with_storage::A_LOT == 0xCAFE); + const val = with_storage::A_LOT; + assert(val == 0xCAFE); + return 0; +}; diff --git a/tests/configure b/tests/configure @@ -15,7 +15,8 @@ tests() { 908-loops \ 909-defer \ 910-tagged \ - 911-slices + 911-slices \ + 912-enums do cat <<EOF tests/$t: harec tests/$t.ha tests/rt.o