hare

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

commit a9a776a5d05269ec4d61472aa68226b1b53f3c45
parent 9bbdedd642aca60e071dc60572a83b0b19f7cf9a
Author: Drew DeVault <sir@cmpwn.com>
Date:   Mon, 22 Feb 2021 15:50:07 -0500

hare::ast: add import_free

Diffstat:
Mhare/ast/types.ha | 22++++++++++++++++++++--
Mhare/parse/+test.ha | 8++++++++
2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/hare/ast/types.ha b/hare/ast/types.ha @@ -2,7 +2,7 @@ // significant part (foo::bar::baz becomes ["baz", "bar", "foo"]. export type ident = []str; -// Frees resources associated with an identifier +// Frees resources associated with an identifier. export fn ident_free(ident: ident) void = { for (let i = 0z; i < len(ident); i += 1) { free(ident[i]); @@ -10,7 +10,7 @@ export fn ident_free(ident: ident) void = { free(ident); }; -// A sub-unit, typically representing a single source file +// A sub-unit, typically representing a single source file. export type subunit = struct { imports: []import, declarations: []declaration, @@ -34,5 +34,23 @@ export type import_objects = struct { // An imported module export type import = (import_module | import_alias | import_objects); +// Frees resources associated with an import. +export fn import_free(import: import) void = { + match (import) { + m: import_module => ident_free(m: ident), + a: import_alias => { + ident_free(a.ident); + free(a.alias); + }, + o: import_objects => { + ident_free(o.ident); + for (let i = 0z; i < len(o.objects); i += 1) { + free(o.objects[i]); + }; + free(o.objects); + }, + }; +}; + // TODO export type declaration = void; diff --git a/hare/parse/+test.ha b/hare/parse/+test.ha @@ -58,8 +58,13 @@ use fmt; let buf = bufio::fixed(strings::to_utf8(in)); let lexer = lex::lexer_init(buf, "<test>"); let mods = imports(&lexer) as []ast::import; + defer for (let i = 0z; i < len(mods); i += 1) { + ast::import_free(mods[i]); + }; + assert(len(mods) == 1); assert(mods[0] is ast::import_module); + let mod = mods[0] as ast::import_module; assert(len(mod) == 1 && mod[0] == "foo"); assert(lex::lex(&lexer) is io::EOF); @@ -74,6 +79,9 @@ use fmt; let buf = bufio::fixed(strings::to_utf8(in)); let lexer = lex::lexer_init(buf, "<test>"); let mods = imports(&lexer) as []ast::import; + defer for (let i = 0z; i < len(mods); i += 1) { + ast::import_free(mods[i]); + }; assert(len(mods) == 3); let expected: [_][]str = [["foo"], ["bar"], ["bat", "baz"]];