hare

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

util.ha (1732B)


      1 // License: GPL-3.0
      2 // (c) 2022 Byron Torres <b@torresjrjr.com>
      3 // (c) 2022 Sebastian <sebastian@sebsite.pw>
      4 use fmt;
      5 use hare::ast;
      6 use hare::module;
      7 use io;
      8 use strings;
      9 use strio;
     10 
     11 // Forked from [[hare::unparse]].
     12 fn newline(out: io::handle, indent: size) (size | io::error) = {
     13 	let n = 0z;
     14 	n += fmt::fprint(out, "\n")?;
     15 	for (let i = 0z; i < indent; i += 1) {
     16 		n += fmt::fprint(out, "\t")?;
     17 	};
     18 	return n;
     19 };
     20 
     21 fn multiline_comment(s: str) bool =
     22 	strings::byteindex(s, '\n') as size != len(s) - 1;
     23 
     24 fn trim_comment(s: str) str = {
     25 	let trimmed = strio::dynamic();
     26 	let tok = strings::tokenize(s, "\n");
     27 	for (true) {
     28 		const line = match (strings::next_token(&tok)) {
     29 		case void =>
     30 			break;
     31 		case let line: str =>
     32 			yield line;
     33 		};
     34 		strio::concat(&trimmed, strings::trimprefix(line, " "), "\n")!;
     35 	};
     36 	return strings::dup(strio::string(&trimmed));
     37 };
     38 
     39 fn submodules(ctx: *context) ([]str | error) = {
     40 	let identpath = module::identpath(ctx.ident);
     41 	defer free(identpath);
     42 
     43 	let submodules: []str = [];
     44 	for (let i = 0z; i < len(ctx.version.subdirs); i += 1) {
     45 		let dir = ctx.version.subdirs[i];
     46 		// XXX: the list of reserved directory names is not yet
     47 		// finalized. See https://todo.sr.ht/~sircmpwn/hare/516
     48 		if (dir == "contrib") continue;
     49 		if (dir == "cmd") continue;
     50 		if (dir == "docs") continue;
     51 		if (dir == "ext") continue;
     52 		if (dir == "vendor") continue;
     53 		if (dir == "scripts") continue;
     54 
     55 		let submod = [identpath, dir]: ast::ident;
     56 		match (module::lookup(ctx.mctx, submod)) {
     57 		case let ver: module::version =>
     58 			// TODO: free version data
     59 			void;
     60 		case module::notfound =>
     61 			continue;
     62 		case let err: module::error =>
     63 			return err;
     64 		};
     65 
     66 		append(submodules, dir);
     67 	};
     68 
     69 	return submodules;
     70 };