commit 0a945a424611a0b1ceb6e2da256c7cf428a2f3ec
parent 733b63bb68af25da88bd45eed5959c845a514766
Author: Lou Woell <lou.woell@posteo.de>
Date: Thu, 4 Sep 2025 15:24:08 +0200
factor out utils
Diffstat:
4 files changed, 63 insertions(+), 63 deletions(-)
diff --git a/cmd/harehelper/find.ha b/cmd/harehelper/find.ha
@@ -86,15 +86,3 @@ fn find_uid (id: ast::ident, path: str) (void | ast::ident) = {
return void;
};
-
-fn ident_last(id: ast::ident) ast::ident = {
- let l = len(id);
-
- if(l > 1) return id[l - 1 ..] else return id;
-};
-
-fn ident_ns(id: ast::ident) ast::ident = {
- let l = len(id);
-
- if(l > 1) return id[..l - 1] else return id;
-};
diff --git a/cmd/harehelper/list_modules.ha b/cmd/harehelper/list_modules.ha
@@ -1,8 +1,6 @@
use fmt;
use hare::module;
use hare::parse;
-use hare::unparse;
-use os;
use path;
use strings;
@@ -13,7 +11,7 @@ fn get_modules (ctx: *module::context) []module::module = {
const harepath = strings::split(ctx.harepath, ":")!;
let mod = path::init()!;
- for(let p .. harepath) {
+ for (let p .. harepath) {
path::set(&mod, p)!;
let r = module::gather_submodules(ctx, &res, &mod, true);
@@ -30,11 +28,7 @@ fn get_modules (ctx: *module::context) []module::module = {
fn list_modules (ctx: *module::context) void = {
const mods = get_modules(ctx);
for (let m &.. mods) {
- unparse::ident(
- os::stdout,
- m.ns,
- )!;
- fmt::println()!;
+ printident(m.ns,);
module::finish(m);
};
free(mods);
diff --git a/cmd/harehelper/list_symbols.ha b/cmd/harehelper/list_symbols.ha
@@ -1,14 +1,8 @@
-use bufio;
use fs;
-use hare::lex;
-use fmt;
use getopt;
use hare::ast;
use hare::module;
use hare::parse;
-use hare::unparse;
-use io;
-use os;
fn list (cmd: getopt::command, ctx: *module::context) void = {
@@ -17,11 +11,7 @@ fn list (cmd: getopt::command, ctx: *module::context) void = {
let list = list_symbols(ctx, id);
for(let i .. list) {
- unparse::ident(
- os::stdout,
- i,
- )!;
- fmt::println()!;
+ printident(i);
ast::ident_free(i);
};
free(list);
@@ -36,42 +26,29 @@ fn list_symbols (ctx: *module::context, id: ast::ident) []ast::ident = {
for(let s .. src.ha) {
let decls = scan(s)!;
for (let d .. decls) {
- if(!d.exported) continue;
- match(d.decl){
- case let d: []ast::decl_const => void;
- for (let t .. d) {
- append(res, ast::ident_dup(t.ident))!;
+ if(!d.exported) continue;
+ match(d.decl){
+ case let d: []ast::decl_const => void;
+ for (let t .. d) {
+ append(res, ast::ident_dup(t.ident))!;
+ };
+ case let d: []ast::decl_global => void;
+ for (let t .. d) {
+ append(res, ast::ident_dup(t.ident))!;
+ };
+ case let d: []ast::decl_type => void;
+ for (let t .. d) {
+ append(res, ast::ident_dup(t.ident))!;
+ };
+ case let d: ast::decl_func => void;
+ append(res, ast::ident_dup(d.ident))!;
+ case let d: ast::assert_expr => void;
};
- case let d: []ast::decl_global => void;
- for (let t .. d) {
- append(res, ast::ident_dup(t.ident))!;
- };
- case let d: []ast::decl_type => void;
- for (let t .. d) {
- append(res, ast::ident_dup(t.ident))!;
- };
- case let d: ast::decl_func => void;
- append(res, ast::ident_dup(d.ident))!;
- case let d: ast::assert_expr => void;
- };
+ ast::decl_finish(d);
};
+ free(decls);
};
return res;
};
-fn scan(path: str) ([]ast::decl | parse::error) = {
- const input = match (os::open(path)) {
- case let f: io::file =>
- yield f;
- case let err: fs::error =>
- fmt::fatalf("Error reading {}: {}", path, fs::strerror(err));
- };
- defer io::close(input)!;
- let sc = bufio::newscanner(input);
- defer bufio::finish(&sc);
- let lexer = lex::init(&sc, path, lex::flag::NONE);
- let su = parse::subunit(&lexer)?;
- ast::imports_finish(su.imports);
- return su.decls;
-};
diff --git a/cmd/harehelper/util.ha b/cmd/harehelper/util.ha
@@ -0,0 +1,41 @@
+use hare::ast;
+use hare::parse;
+use hare::unparse;
+use hare::lex;
+use io;
+use fs;
+use os;
+use fmt;
+use bufio;
+
+fn printident(i: ast::ident) void = {
+ unparse::ident(os::stdout, i)!;
+ fmt::println()!;
+};
+
+fn ident_last(id: ast::ident) ast::ident = {
+ let l = len(id);
+ if(l > 1) return id[l - 1 ..] else return id;
+};
+
+fn ident_ns(id: ast::ident) ast::ident = {
+ let l = len(id);
+
+ if(l > 1) return id[..l - 1] else return id;
+};
+
+fn scan(path: str) ([]ast::decl | parse::error) = {
+ const input = match (os::open(path)) {
+ case let f: io::file =>
+ yield f;
+ case let err: fs::error =>
+ fmt::fatalf("Error reading {}: {}", path, fs::strerror(err));
+ };
+ defer io::close(input)!;
+ let sc = bufio::newscanner(input);
+ defer bufio::finish(&sc);
+ let lexer = lex::init(&sc, path, lex::flag::NONE);
+ let su = parse::subunit(&lexer)?;
+ ast::imports_finish(su.imports);
+ return su.decls;
+};