~smlavine/hareimports

80b3575a650003dc9786abb02ce0ed5664bcac56 — Sebastian LaVine 26 days ago da87231
Finish list() operation; expand test_list()
2 files changed, 31 insertions(+), 8 deletions(-)

M +test.ha
M main.ha
M +test.ha => +test.ha +19 -5
@@ 18,15 18,16 @@ use hare::lex;
use hare::parse;
use io;
use os;
use strings;
use strio;

def TEST_SOURCE: str = `
use fmt;
use io;
use strings::{tokenizer, compare, trimsuffix};
use os::*;
use ebcdic = ascii;

export fn main() void = {
	fmt::println("Hello")!;
};
export fn main() void = void;
`;

// Makes a source_file from the constant source str defined above.


@@ 51,5 52,18 @@ fn mktestsource() source_file = {
	list(source);
	os::stdout = orig_stdout;

	assert(strio::string(&stream) == "TEST_SOURCE:\nfmt\nio\n");
	const correct = [
		"TEST_SOURCE:",
		"fmt",
		"strings::{tokenizer, compare, trimsuffix}",
		"os::*",
		"ebcdic = ascii",
		"", // strings::split includes what is after the last "\n"
	];
	const testlines = strings::split(strio::string(&stream), "\n");
	defer free(testlines);

	assert(len(correct) == len(testlines));
	for (let i = 0z; i < len(correct); i += 1)
		assert(correct[i] == testlines[i]);
};

M main.ha => main.ha +12 -3
@@ 38,10 38,19 @@ export fn list(source: source_file) void = {
		defer free(s);

		switch (u.imports[i].mode) {
		case ast::import_mode::ALIAS =>
			fmt::printfln("{} (= {})", u.imports[i].alias, s)!;
		case =>
		case ast::import_mode::IDENT =>
			fmt::println(s)!;
		case ast::import_mode::ALIAS =>
			fmt::printfln("{} = {}", u.imports[i].alias, s)!;
		case ast::import_mode::MEMBERS =>
			fmt::printf("{}::{{", s)!;
			const nlim = len(u.imports[i].objects) - 1;
			for (let n = 0z; n < nlim; n += 1)
				fmt::printf("{}, ",
					u.imports[i].objects[n].1)!;
			fmt::printfln("{}}}", u.imports[i].objects[nlim].1)!;
		case ast::import_mode::WILDCARD =>
			fmt::printfln("{}::*", s)!;
		};
	};
};