harec

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

commit 3f492a587d81ac0c6f4e081176d9ff75d884dfe2
parent 6cee56a9ed63ce3cb53cb1e547f9c66f9db507f9
Author: Pierre Curto <pierre.curto@gmail.com>
Date:   Fri, 18 Nov 2022 15:25:43 +0100

support arrays in def

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

Diffstat:
Msrc/typedef.c | 20+++++++++++++++++++-
Mtests/34-declarations.ha | 3+++
2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/src/typedef.c b/src/typedef.c @@ -143,7 +143,25 @@ emit_const(const struct expression *expr, FILE *out) fprintf(out, ": "); emit_type(expr->constant.tagged.tag, out); break; - case STORAGE_ARRAY: + case STORAGE_ARRAY: { + fprintf(out, "["); + const struct type *t = type_dealias(expr->result); + struct array_constant *item = val->array; + if (t->array.expandable) { + emit_const(item->value, out); + fprintf(out, "..."); + } else { + for (size_t i = 0; i < t->array.length; i += 1) { + if (i > 0) { + fprintf(out, ","); + } + emit_const(item->value, out); + item = item->next; + }; + } + fprintf(out, "]"); + break; + } case STORAGE_SLICE: case STORAGE_STRUCT: case STORAGE_TUPLE: diff --git a/tests/34-declarations.ha b/tests/34-declarations.ha @@ -1,5 +1,7 @@ use rt::{compile, exited, EXIT_SUCCESS}; +def ARR: [3]u8 = [1, 2, 3]; + // interdependent constants def A1: int = -1; def A2: int = A1 + 2; @@ -10,6 +12,7 @@ def B2: int = B1 + 2; def B1: int = -1; fn constants() void = { + assert(ARR[0] == 1 && ARR[1] == 2 && ARR[2] == 3); assert(A1 == -1 && B1 == -1); assert(A2 == 1 && B2 == 1); assert(len(B3) == 3);