harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

commit 6dd923e1ead90cadbb7f82e7e8c3dfb21c6ba443
parent f2496cde69520e29dd256bd94f295d2c4ad843ef
Author: Ember Sawady <ecs@d2evs.net>
Date:   Mon,  6 Feb 2023 16:34:31 +0000

eval_const: implement struct case

Signed-off-by: Ember Sawady <ecs@d2evs.net>

Diffstat:
Msrc/eval.c | 17++++++++++++++++-
Mtests/06-structs.ha | 14+++++++++-----
2 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/src/eval.c b/src/eval.c @@ -442,7 +442,22 @@ eval_const(struct context *ctx, struct expression *in, struct expression *out) out->constant.tagged.value = xcalloc(sizeof(struct expression), 1); return eval_expr(ctx, in->constant.tagged.value, out->constant.tagged.value); - case STORAGE_STRUCT: + case STORAGE_STRUCT:; + struct struct_constant **next = &out->constant._struct; + for (struct struct_constant *_struct = in->constant._struct; + _struct; _struct = _struct->next) { + struct struct_constant *cur = *next = + xcalloc(sizeof(struct struct_constant), 1); + cur->field = _struct->field; + cur->value = xcalloc(sizeof(struct expression), 1); + enum eval_result r = + eval_expr(ctx, _struct->value, cur->value); + if (r != EVAL_OK) { + return r; + } + next = &cur->next; + } + break; case STORAGE_UNION: assert(0); // TODO case STORAGE_TUPLE:; diff --git a/tests/06-structs.ha b/tests/06-structs.ha @@ -215,14 +215,19 @@ fn fields() void = { assert(&sp.c: uintptr == &n: uintptr + 2); }; -fn eval_struct() void = { +def S = struct { + a: u8 = 69, + b: u32 = 1337, +}; + +fn eval() void = { static let s = struct { a: u8 = 69, b: u32 = 1337, }; -}; -fn eval_access() void = { + static assert(S.a == 69); + static assert(struct { a: u8 = 69, b: u32 = 1337, @@ -239,8 +244,7 @@ export fn main() void = { autofill(); invariants(); fields(); - eval_struct(); - eval_access(); + eval(); // TODO: // - Union tests // - Embedded structs