hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 2826377a354f0cd4b4076cea8b7634653a6df988
parent ded6a857ae6ec506260ead973a6e91e1f4a0b6ca
Author: Autumn! <autumnull@posteo.net>
Date:   Mon,  8 May 2023 18:22:01 +0000

hare::ast: add imports_finish

Signed-off-by: Autumn! <autumnull@posteo.net>

Diffstat:
Mcmd/haredoc/main.ha | 4+---
Mhare/ast/import.ha | 9+++++++++
Mhare/ast/unit.ha | 5+----
Mhare/parse/+test/unit.ha | 4+---
4 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/cmd/haredoc/main.ha b/cmd/haredoc/main.ha @@ -152,9 +152,7 @@ export fn main() void = { }; match (scan(in.path)) { case let u: ast::subunit => - for (let i = 0z; i < len(u.imports); i += 1) { - ast::import_finish(u.imports[i]); - }; + ast::imports_finish(u.imports); append(decls, u.decls...); case let err: error => fmt::fatal("Error:", strerror(err)); diff --git a/hare/ast/import.ha b/hare/ast/import.ha @@ -41,3 +41,12 @@ export fn import_finish(import: import) void = { }; free(import.objects); }; + +// Frees resources associated with each [[import]] in a slice, and then +// frees the slice itself. +export fn imports_finish(imports: []import) void = { + for (let i = 0z; i < len(imports); i += 1) { + import_finish(imports[i]); + }; + free(imports); +}; diff --git a/hare/ast/unit.ha b/hare/ast/unit.ha @@ -10,10 +10,7 @@ export type subunit = struct { // Frees resources associated with a [[subunit]]. export fn subunit_finish(u: subunit) void = { - for (let i = 0z; i < len(u.imports); i += 1) { - import_finish(u.imports[i]); - }; - free(u.imports); + imports_finish(u.imports); for (let i = 0z; i < len(u.decls); i += 1) { decl_finish(u.decls[i]); }; diff --git a/hare/parse/+test/unit.ha b/hare/parse/+test/unit.ha @@ -79,9 +79,7 @@ fn tup_to_import(tup: import_tuple) ast::import = ast::import { let lexer = lex::init(&buf, "<test>"); defer lex::finish(&lexer); let mods = imports(&lexer)!; - defer for (let i = 0z; i < len(mods); i += 1) { - ast::import_finish(mods[i]); - }; + defer ast::imports_finish(mods); let expected: [_]import_tuple = [ (ast::import_mode::IDENT, ["foo"], "", []),