hare

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

commit d30d92c80786c9ea82bf1b23cb2f788680b3d198
parent a9a501ab2226b36c9f6e369e5dc3b21cc008c60d
Author: Dridi Boukelmoune <dridi.boukelmoune@gmail.com>
Date:   Tue,  1 Oct 2024 18:03:26 +0200

hare::module: Resolve submodules from a location

This allows resolving foo::bar submodules, wherever foo::bar is located
on the HAREPATH, or current directory. On the other hand, resolving
submodules from foo/'s parent directory resolves foo, foo::bar, their
siblings and children.

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

Diffstat:
Mcmd/hare/build/gather.ha | 4+---
Mhare/module/deps.ha | 46++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 43 insertions(+), 7 deletions(-)

diff --git a/cmd/hare/build/gather.ha b/cmd/hare/build/gather.ha @@ -19,9 +19,7 @@ export fn gather(ctx: *context, input: str) ([]module::module | error) = { module::gather(&ctx.ctx, &mods, ["debug"])?; }; const nsubmods = if (ctx.submods) { - let id: ast::ident = []; - defer ast::ident_free(id); - yield module::gather_submodules(&ctx.ctx, &mods, &buf, &id)?; + module::gather_submodules(&ctx.ctx, &mods, &buf)?; } else 0z; ctx.top = match (module::gather(&ctx.ctx, &mods, &buf)) { diff --git a/hare/module/deps.ha b/hare/module/deps.ha @@ -127,12 +127,50 @@ export fn gather( // 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 should be left empty for source -// directories. Dependencies will also be written to the cache. +// example a HAREPATH component). Dependencies will also be written to the +// cache. export fn gather_submodules( ctx: *context, out: *[]module, + mod: location, + recursive: bool = true, +) (size | error) = { + let id: ast::ident = []; + defer ast::ident_free(id); + + let buf = match (mod) { + case let b: *path::buffer => + yield b; + case let m: ast::ident => + append(id, m...); + static let b = path::buffer { ... }; + let res = find(ctx, id)?; + defer finish_srcset(&res.1); + path::set(&b, res.0)?; + yield &b; + }; + + static let srcdir = path::buffer { ... }; + srcdir = *buf; + for (let i = 0z; i < len(id); i += 1) { + path::pop(&srcdir); + }; + + let subpath = strings::concat(path::string(&srcdir), ":", + ctx.harepath); + defer free(subpath); + + let subctx = context { + harepath = subpath, + harecache = ctx.harecache, + tags = ctx.tags, + }; + return _gather_submodules(&subctx, out, buf, &id, recursive); +}; + +export fn _gather_submodules( + ctx: *context, + out: *[]module, buf: *path::buffer, mod: *ast::ident, recursive: bool = true, @@ -156,7 +194,7 @@ export fn gather_submodules( return e; }; }; - n += gather_submodules(ctx, out, buf, mod, recursive)?; + n += _gather_submodules(ctx, out, buf, mod, recursive)?; }; return n; };