commit f861e7f3e0d47772d63a1a111e435daec26bd400
parent 4fa79fce9d13130a1990c2ee95da1dc7a20afce8
Author: Eyal Sawady <ecs@d2evs.net>
Date: Wed, 10 Feb 2021 14:11:03 -0500
Implement allocations with implicit capacity
Diffstat:
2 files changed, 10 insertions(+), 3 deletions(-)
diff --git a/src/gen.c b/src/gen.c
@@ -504,11 +504,11 @@ gen_slice_alloc(struct gen_context *ctx,
if (expr->alloc.cap != NULL) {
gen_expression(ctx, expr->alloc.cap, &cap);
- constl(&temp, expr->result->array.members->size);
- pushi(ctx->current, &size, Q_MUL, &cap, &temp, NULL);
} else {
- assert(0); // TODO: Alloc without explicit capacity
+ cap = len;
}
+ constl(&temp, expr->result->array.members->size);
+ pushi(ctx->current, &size, Q_MUL, &cap, &temp, NULL);
struct qbe_value ret = {0};
gen_temp(ctx, &ret, &qbe_long, "alloc.ret.%d");
diff --git a/tests/17-alloc.ha b/tests/17-alloc.ha
@@ -57,6 +57,13 @@ fn slice() void = {
assert(x[i] == (i + 1): int);
};
free(x);
+
+ let y = alloc([]int, [1, 2, 3]);
+ assert(len(x) == 3);
+ for (let i = 0z; i < len(y); i += 1) {
+ assert(y[i] == (i + 1): int);
+ };
+ free(y);
};
fn string() void = {