hare

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

commit 8e37ec3849a5bfd186b3487612a7dd9af46348a2
parent 097b30120d980ac8a6e443f8c0bfc1d3f62f185e
Author: Kirill Primak <vyivel@eclair.cafe>
Date:   Sat, 24 Dec 2022 16:07:28 +0300

hare/module: update context.paths initialization

This commit utilizes strings::tokenize(), merges similar code paths, and
allows multiple dependency vendoring.

Dependencies are included in lexicographical order. Assuming the
following directory structure:

vendor/
  dependency-1/
    alice/
      mod.ha
  dependency-2/
    bob/
      mod.ha
  dependency-3/
    bob/
      mod.ha
    carol/
      mod.ha

The following imports will work:

use alice;
use bob;
use carol;

Where bob will refer to the dependency-3's module.

Signed-off-by: Kirill Primak <vyivel@eclair.cafe>

Diffstat:
Mhare/module/context.ha | 45+++++++++++++++++++++++++++------------------
1 file changed, 27 insertions(+), 18 deletions(-)

diff --git a/hare/module/context.ha b/hare/module/context.ha @@ -5,6 +5,7 @@ use dirs; use fmt; use fs; +use glob; use hare::ast; use os; use path; @@ -35,26 +36,34 @@ export fn context_init(tags: []tag, defs: []str, harepath: str) context = { fs = os::cwd, tags = tags, defines = defs, - paths = match (os::getenv("HAREPATH")) { - case void => - // TODO: Use strings::tokenize here - let sl = strings::split(harepath, ":"); - defer free(sl); - let path: []str = alloc([], len(sl) + 1); - for (let i = 0z; i < len(sl); i += 1) { - append(path, strings::dup(sl[i])); + paths = { + let harepath = match (os::getenv("HAREPATH")) { + case void => + yield harepath; + case let s: str => + yield s; }; - append(path, strings::dup("vendor")); - append(path, strings::dup(".")); - yield path; - case let s: str => - let sl = strings::split(s, ":"); - defer free(sl); - let path: []str = alloc([], len(sl) + 1); - for (let i = 0z; i < len(sl); i += 1) { - append(path, strings::dup(sl[i])); + + let path: []str = []; + let tok = strings::tokenize(harepath, ":"); + for (true) match (strings::next_token(&tok)) { + case void => + break; + case let s: str => + append(path, strings::dup(s)); + }; + + let vendor = glob::glob("vendor/*"); + defer glob::finish(&vendor); + for (true) match (glob::next(&vendor)) { + case void => + break; + case glob::failure => + void; // XXX: Anything else? + case let s: str => + append(path, strings::dup(s)); }; - append(path, strings::dup("vendor")); + append(path, strings::dup(".")); yield path; },