hare

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

commit ba7c293138d8fcaacab30fac8234df8ef4d19e07
parent 85e34ae697939b3ce62d344e7f78d4273555f49c
Author: Drew DeVault <sir@cmpwn.com>
Date:   Tue, 20 Apr 2021 12:22:48 -0400

haredoc: resolve all references

That was easier than I anticipated

Diffstat:
Mcmd/haredoc/html.ha | 27+++++++++++++++++++++------
Mcmd/haredoc/main.ha | 6++++--
Mcmd/haredoc/resolver.ha | 23++++++++++++++++++++++-
3 files changed, 47 insertions(+), 9 deletions(-)

diff --git a/cmd/haredoc/html.ha b/cmd/haredoc/html.ha @@ -139,7 +139,7 @@ fn details(ctx: *context, decl: ast::decl) (void | error) = { }; fn htmlref(ctx: *context, ref: ast::ident) (void | io::error) = { - match (resolve(ctx, ref)) { + const ik = match (resolve(ctx, ref)) { _: void => { const ident = unparse::identstr(ref); fmt::errorfln("Warning: Unresolved reference: {}", ident); @@ -148,14 +148,29 @@ fn htmlref(ctx: *context, ref: ast::ident) (void | io::error) = { "title='This reference could not be found'>{}</a>", ident)?; free(ident); + return; }, - ik: (ast::ident, symkind) => { - const kind = ik.1, id = ik.0; - const ident = unparse::identstr(id); - fmt::printf("<a href='#{0}' class='ref'>{0}</a>", ident)?; - free(ident); + ik: (ast::ident, symkind) => ik, + }; + + const kind = ik.1, id = ik.0; + const ident = unparse::identstr(id); + switch (kind) { + symkind::LOCAL => + fmt::printf("<a href='#{0}' class='ref'>{0}</a>", ident)?, + symkind::MODULE => { + // TODO: Not necessarily in /stdlib + let ipath = module::identpath(id); + fmt::printf("<a href='/stdlib/{}' class='ref'>{}</a>", + ipath, ident)?; + }, + symkind::SYMBOL => { + let ipath = module::identpath(id[..len(id) - 1]); + fmt::printf("<a href='/stdlib/{}#{}' class='ref'>{}</a>", + ipath, id[len(id) - 1], ident)?; }, }; + free(ident); }; fn markup_html(ctx: *context, in: *io::stream) (void | io::error) = { diff --git a/cmd/haredoc/main.ha b/cmd/haredoc/main.ha @@ -18,6 +18,7 @@ type format = enum { }; type context = struct { + mctx: *module::context, ident: ast::ident, tags: []module::tag, version: module::version, @@ -103,7 +104,8 @@ export fn main() void = { f: *io::stream => io::close(f), }; - let dctx = context { + const ctx = context { + mctx = &ctx, ident = id, tags = tags, version = version, @@ -112,7 +114,7 @@ export fn main() void = { template = template, readme = readme, }; - match (emit(&dctx)) { + match (emit(&ctx)) { _: void => void, err: error => fmt::fatal("Error: {}", strerror(err)), }; diff --git a/cmd/haredoc/resolver.ha b/cmd/haredoc/resolver.ha @@ -1,4 +1,5 @@ use hare::ast; +use hare::module; type symkind = enum { LOCAL, @@ -13,7 +14,27 @@ fn resolve(ctx: *context, what: ast::ident) ((ast::ident, symkind) | void) = { if (is_local(ctx, what)) { return (what, symkind::LOCAL); }; - return void; + + if (len(what) > 1) { + // Look for symbol in remote module + let partial = what[..len(what) - 1]; + + match (module::lookup(ctx.mctx, partial)) { + ver: module::version => { + return (what, symkind::SYMBOL); + }, + _: module::error => void, + }; + }; + + match (module::lookup(ctx.mctx, what)) { + ver: module::version => { + return (what, symkind::MODULE); + }, + _: module::error => void, + }; + + return; }; fn is_local(ctx: *context, what: ast::ident) bool = {