~smlavine/hareimports

ea0a7db7a0ecd480b96d7b372ce1fee67ecea542 — Sebastian LaVine 2 months ago 25f8b07
Restructure main(), add plcaeholder loops

listing imports is separated out into its own function, and an operation
function pointer is created, to make it easier to toggle between
different actions with options.

Operations on the source files is separated out of the loop where we lex
module files; comments are added to explain why this is necessary.

Various TODO comments are added to make it clear what is unfinished.
1 files changed, 48 insertions(+), 16 deletions(-)

M main.ha
M main.ha => main.ha +48 -16
@@ 24,6 24,23 @@ use hare::unparse;
use io;
use os;

// Lists the imports of a given file, in the order they are imported.
fn list(path: str, subunit: ast::subunit) void = {
	fmt::printfln("{}:", path)!;
	for (let i = 0z; i < len(subunit.imports); i += 1) {
		const m = &subunit.imports[i];
		const s = unparse::identstr(m.ident);
		defer free(s);

		switch (m.mode) {
		case ast::import_mode::ALIAS =>
			fmt::printfln("{} (= {})", m.alias, s)!;
		case =>
			fmt::println(s)!;
		};
	};
};

export fn main() void = {
	const help: []getopt::help = [
		"Hare import manager",


@@ 35,11 52,17 @@ export fn main() void = {

	let tags: []module::tag = [];
	defer module::tags_free(tags);

	// XXX: have a slice of operations to do in order, instead of just
	// allowing one?
	let op: *fn(path: str, subunit: ast::subunit) void = &list;

	for (let i = 0z; i < len(cmd.opts); i += 1) {
		const opt = cmd.opts[i];
		// TODO: parse other options to indicate different operations
		switch (opt.0) {
		// XXX: should accept multiple -T options, and maybe also -X
		// See addtags and deltags in cmd/hare/subcmds.ha
		switch (opt.0) {
		case 'T' =>
			// This flag is needed for hareimports to know whether
			// an indentifier refers to a remote module/enum or a


@@ 80,6 103,11 @@ export fn main() void = {
		fmt::fatal(module::strerror(e));
	};

	// TODO: enum slice

	let sourcefiles: [](str, ast::subunit) = [];
	defer free(sourcefiles);

	for (let i = 0z; i < len(ver.inputs); i += 1) {
		const input = ver.inputs[i];
		if (input.ft != module::filetype::HARE) {


@@ 95,20 123,24 @@ export fn main() void = {
		case let e: parse::error =>
			fmt::fatal(parse::strerror(e));
		};
		defer ast::subunit_finish(subunit);

		fmt::printfln("{}:", input.path)!;
		for (let i = 0z; i < len(subunit.imports); i += 1) {
			const m = &subunit.imports[i];

			const s = unparse::identstr(m.ident);
			defer free(s);
			switch (m.mode) {
			case ast::import_mode::ALIAS =>
				fmt::printfln("{} (= {})", m.alias, s)!;
			case =>
				fmt::println(s)!;
			};
		};

		append(sourcefiles, (input.path, subunit));

		// TODO: look for enums declared in this subunit; add to slice
		// of module enums declared above.
	};

	// We must look for enums in all source files before processing
	// the imports.

	for (let i = 0z; i < len(sourcefiles); i += 1) {
		// TODO: parse subunit.decls to see if imports are unused or
		// if unimported modules are used
		null;
	};

	for (let i = 0z; i < len(sourcefiles); i += 1) {
		op(sourcefiles[i].0, sourcefiles[i].1);
		ast::subunit_finish(sourcefiles[i].1);
	};
};