harec

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

commit 6dc8bc29d8f2ce33f8419093d295162f995b7a54
parent 19d98d5ac19b9f31c8196dd411a850856503e952
Author: Charlie Stanton <charlie@shtanton.xyz>
Date:   Tue,  6 Jul 2021 23:22:30 +0100

Add using slices to create recursive data structures

Fixes #459

Diffstat:
Msrc/check.c | 3++-
Mtests/08-slices.ha | 31+++++++++++++++++++++++++++++++
2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/src/check.c b/src/check.c @@ -2921,9 +2921,10 @@ type_is_specified(struct context *ctx, const struct ast_type *atype) case STORAGE_ALIAS: return scope_lookup(ctx->scope, &atype->alias) != NULL; case STORAGE_ARRAY: - case STORAGE_SLICE: return type_is_specified(ctx, atype->array.members) && expr_is_specified(ctx, atype->array.length); + case STORAGE_SLICE: + return true; case STORAGE_ENUM: for (struct ast_enum_field *field = atype->_enum.values; field; field = field->next) { diff --git a/tests/08-slices.ha b/tests/08-slices.ha @@ -129,6 +129,36 @@ fn slicing() void = { ) != 0, "slicing object with non-array, non-slice range"); }; +type tree = struct { + value: u64, + children: []tree, +}; + +fn sum_tree(t: tree) u64 = { + let sum = t.value; + for (let i = 0z; i < len(t.children); i += 1) { + sum += sum_tree(t.children[i]); + }; + return sum; +}; + +fn recursive_structure() void = { + const t = tree { + value = 15, + children = [tree { + value = 23, + children = [tree { + value = 62, + children = [], + }, tree { + value = 34, + children = [], + }], + }], + }; + assert(sum_tree(t) == 134, "recursive structure using slices"); +}; + export fn main() void = { from_array(); storage(); @@ -136,4 +166,5 @@ export fn main() void = { indexing(); assignment(); slicing(); + recursive_structure(); };