hare

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

commit d311f7c612d56182a8803bed4377dfe3e063d985
parent 17d5002892e86971926994ce99b02884fc0ea020
Author: Dridi Boukelmoune <dridi.boukelmoune@gmail.com>
Date:   Tue,  1 Oct 2024 18:03:24 +0200

cmd/hare: Move gather_submodules() to stdlib

Signed-off-by: Dridi Boukelmoune <dridi.boukelmoune@gmail.com>

Diffstat:
Mcmd/hare/build/gather.ha | 30+-----------------------------
Mhare/module/deps.ha | 35+++++++++++++++++++++++++++++++++++
2 files changed, 36 insertions(+), 29 deletions(-)

diff --git a/cmd/hare/build/gather.ha b/cmd/hare/build/gather.ha @@ -21,7 +21,7 @@ export fn gather(ctx: *context, input: str) ([]module::module | error) = { const nsubmods = if (ctx.submods) { let id: ast::ident = []; defer ast::ident_free(id); - yield gather_submodules(&ctx.ctx, &mods, &buf, &id)?; + yield module::gather_submodules(&ctx.ctx, &mods, &buf, &id)?; } else 0z; ctx.top = match (module::gather(&ctx.ctx, &mods, &buf)) { @@ -44,31 +44,3 @@ export fn gather(ctx: *context, input: str) ([]module::module | error) = { }; return mods; }; - -fn gather_submodules( - ctx: *module::context, - mods: *[]module::module, - buf: *path::buffer, - mod: *ast::ident, -) (size | error) = { - let n = 0z; - let it = os::iter(path::string(buf))?; - defer fs::finish(it); - - for (let dir => module::next(it)?) { - path::push(buf, dir.name)?; - defer path::pop(buf); - append(mod, dir.name); - defer delete(mod[len(mod) - 1]); - match (module::gather(ctx, mods, *mod)) { - case size => - n += 1; - case let e: module::error => - if (!(module::unwrap_error(e) is module::not_found)) { - return e; - }; - }; - n += gather_submodules(ctx, mods, buf, mod)?; - }; - return n; -}; diff --git a/hare/module/deps.ha b/hare/module/deps.ha @@ -121,6 +121,41 @@ export fn gather( return _gather(ctx, out, &stack, mod)?; }; +// Gather submodules and all their dependencies, appending them to an existing +// slice, deduplicated, in reverse topological order, returning the number of +// modules added to the slice. The submodules are searched relative to a parent +// directory that may target a module, or a source directory (for example a +// HAREPATH component). The parent module identifier should reflect the nature +// of the parent directory and may be left empty for source directories. +// Dependencies will also be written to the cache. +export fn gather_submodules( + ctx: *context, + out: *[]module, + buf: *path::buffer, + mod: *ast::ident, +) (size | error) = { + let n = 0z; + let it = os::iter(path::string(buf))?; + defer fs::finish(it); + + for (let dir => next(it)?) { + path::push(buf, dir.name)?; + defer path::pop(buf); + append(mod, dir.name); + defer delete(mod[len(mod) - 1]); + match (gather(ctx, out, *mod)) { + case size => + n += 1; + case let e: error => + if (!(unwrap_error(e) is not_found)) { + return e; + }; + }; + n += gather_submodules(ctx, out, buf, mod)?; + }; + return n; +}; + fn _gather( ctx: *context, out: *[]module,