~sircmpwn/hare

e616ea4612c2356896c4e077af975e9092a99629 — Bor Grošelj Simić 7 months ago 4298b68
bufio::fixed: implement closer
M bufio/dynamic.ha => bufio/dynamic.ha +1 -0
@@ 189,4 189,5 @@ export fn truncate(s: *io::stream) (void | errors::unsupported) = {
	assert(io::read(s, buf[..]) is io::EOF);
	// TODO: this should check for errors::unsupported (harec bug prevents that)
	assert(io::write(s, [1, 2]) is io::error);
	io::close(s);
};

M bufio/fixed.ha => bufio/fixed.ha +1 -0
@@ 58,6 58,7 @@ fn fixed_close(s: *io::stream) void = free(s);
	// TODO: add a read test too
	static let buf: [1024]u8 = [0...];
	let stream = fixed(buf, io::mode::WRITE);
	defer io::close(stream);
	let n = 0z;
	n += io::write(stream, strings::toutf8("hello ")) as size;
	n += io::write(stream, strings::toutf8("world")) as size;

M bufio/scanner.ha => bufio/scanner.ha +4 -0
@@ 76,6 76,7 @@ export fn scanrune(stream: *io::stream) (rune | utf8::invalid | io::EOF | io::er

@test fn scanbyte() void = {
	let buf = fixed([1, 3, 3, 7], io::mode::READ);
	defer io::close(buf);

	assert(scanbyte(buf) as u8 == 1);
	assert(scanbyte(buf) as u8 == 3);


@@ 86,6 87,7 @@ export fn scanrune(stream: *io::stream) (rune | utf8::invalid | io::EOF | io::er

@test fn scantok() void = {
	let buf = fixed([1, 3, 4, 5, 3, 7], io::mode::READ);
	defer io::close(buf);

	let tok = scantok(buf, 4) as []u8;
	defer free(tok);


@@ 101,6 103,7 @@ export fn scanrune(stream: *io::stream) (rune | utf8::invalid | io::EOF | io::er
@test fn scanline() void = {
	let helloworld = strings::toutf8("hello\nworld");
	let buf = fixed(helloworld, io::mode::READ);
	defer io::close(buf);

	let line = scanline(buf) as []u8;
	defer free(line);


@@ 118,6 121,7 @@ export fn scanrune(stream: *io::stream) (rune | utf8::invalid | io::EOF | io::er
		0xE3, 0x81, 0x93, 0xE3, 0x82, 0x93, 0xE3, 0x81,
		0xAB, 0xE3, 0x81, 0xA1, 0xE3, 0x81, 0xAF, 0x00,
	], io::mode::READ);
	defer io::close(in);

	const expected: [_](rune | utf8::invalid | io::EOF | io::error) = [
		'こ', 'ん', 'に', 'ち', 'は', '\0', io::EOF,

M fmt/fmt.ha => fmt/fmt.ha +3 -1
@@ 79,6 79,7 @@ export fn asprintf(fmt: str, args: formattable...) str = {
// returned string is borrowed from this buffer.
export fn bsprintf(buf: []u8, fmt: str, args: formattable...) str = {
	let sink = bufio::fixed(buf, io::mode::WRITE);
	defer io::close(sink);
	let l = fprintf(sink, fmt, args...) as size;
	return strings::fromutf8_unsafe(buf[..l]);
};


@@ 134,6 135,7 @@ export fn asprint(args: formattable...) str = {
// is borrowed from this buffer.
export fn bsprint(buf: []u8, args: formattable...) str = {
	let sink = bufio::fixed(buf, io::mode::WRITE);
	defer io::close(sink);
	assert(fprint(sink, args...) is size);
	return strings::fromutf8_unsafe(buf);
};


@@ 145,7 147,7 @@ export fn fprintln(s: *io::stream, args: formattable...) (io::error | size) = {
};

// Formats values for printing using the default format modifiers and writes
// them to an [io::stream] separated by spaces 
// them to an [io::stream] separated by spaces
export fn fprint(s: *io::stream, args: formattable...) (io::error | size) = {
	let mod = modifiers { base = strconv::base::DEC, ... };
	let n = 0z;

M hare/lex/+test.ha => hare/lex/+test.ha +5 -3
@@ 6,6 6,7 @@ use strings;

@test fn unget() void = {
	let buf = bufio::fixed(strings::toutf8("z"), mode::READ);
	defer io::close(buf);
	let lexer = init(buf, "<test>");
	unget(&lexer, 'x');
	unget(&lexer, 'y');


@@ 58,6 59,7 @@ fn vassert(expected: value, actual: value) bool = {

fn lextest(in: str, expected: []token) void = {
	let buf = bufio::fixed(strings::toutf8(in), mode::READ);
	defer io::close(buf);
	let lexer = init(buf, "<test>");
	for (let i = 0z; i < len(expected); i += 1) {
		let etok = expected[i];


@@ 171,9 173,9 @@ fn loc(line: uint, col: uint) location = location {
@test fn keywords() void = {
	let keywords = bmap[..ltok::LAST_KEYWORD+1];
	for (let i = 0z; i < len(keywords); i += 1) {
		let lexer = init(bufio::fixed(
			strings::toutf8(keywords[i]), mode::READ),
			"<test>");
		let buf = bufio::fixed(strings::toutf8(keywords[i]), mode::READ);
		defer io::close(buf);
		let lexer = init(buf, "<test>");
		let tok = lex(&lexer) as token;
		assert(tok.0 == i: ltok);
	};

M hare/parse/+test.ha => hare/parse/+test.ha +10 -0
@@ 3,6 3,7 @@ use fmt;
use hare::ast;
use hare::lex;
use hare::unparse;
use io;
use io::{mode};
use strings;
use strio;


@@ 11,6 12,7 @@ use strio;
	{
		const in = "foo";
		let buf = bufio::fixed(strings::toutf8(in), mode::READ);
		defer io::close(buf);
		let lexer = lex::init(buf, "<test>");
		let ident = ident(&lexer) as ast::ident;
		defer ast::ident_free(ident);


@@ 23,6 25,7 @@ use strio;
	{
		const in = "foo::bar";
		let buf = bufio::fixed(strings::toutf8(in), mode::READ);
		defer io::close(buf);
		let lexer = lex::init(buf, "<test>");
		let ident = ident(&lexer) as ast::ident;
		defer ast::ident_free(ident);


@@ 35,6 38,7 @@ use strio;
	{
		const in = "foo::bar::baz";
		let buf = bufio::fixed(strings::toutf8(in), mode::READ);
		defer io::close(buf);
		let lexer = lex::init(buf, "<test>");
		let ident = ident(&lexer) as ast::ident;
		defer ast::ident_free(ident);


@@ 48,6 52,7 @@ use strio;
	{
		const in = "foo::bar;";
		let buf = bufio::fixed(strings::toutf8(in), mode::READ);
		defer io::close(buf);
		let lexer = lex::init(buf, "<test>");
		let ident = ident(&lexer) as ast::ident;
		defer ast::ident_free(ident);


@@ 62,6 67,7 @@ use strio;
	{
		const in = "use foo;";
		let buf = bufio::fixed(strings::toutf8(in), mode::READ);
		defer io::close(buf);
		let lexer = lex::init(buf, "<test>");
		let mods = imports(&lexer) as []ast::import;
		defer for (let i = 0z; i < len(mods); i += 1) {


@@ 84,6 90,7 @@ use strio;
			"use baz::bat;\n\n"
			"export fn main() void = void;";
		let buf = bufio::fixed(strings::toutf8(in), mode::READ);
		defer io::close(buf);
		let lexer = lex::init(buf, "<test>");
		let mods = imports(&lexer) as []ast::import;
		defer for (let i = 0z; i < len(mods); i += 1) {


@@ 113,6 120,7 @@ use strio;
			"use qux = quux::corge;\n"
			"export fn main() void = void;";
		let buf = bufio::fixed(strings::toutf8(in), mode::READ);
		defer io::close(buf);
		let lexer = lex::init(buf, "<test>");
		let mods = imports(&lexer) as []ast::import;
		defer for (let i = 0z; i < len(mods); i += 1) {


@@ 144,6 152,7 @@ use strio;
			"use quux::corge::{grault, garply,};\n"
			"export fn main() void = void;";
		let buf = bufio::fixed(strings::toutf8(in), mode::READ);
		defer io::close(buf);
		let lexer = lex::init(buf, "<test>");
		let mods = imports(&lexer) as []ast::import;
		defer for (let i = 0z; i < len(mods); i += 1) {


@@ 174,6 183,7 @@ use strio;

fn roundtrip(src: str) void = {
	let buf = bufio::fixed(strings::toutf8(src), mode::READ);
	defer io::close(buf);
	let lexer = lex::init(buf, "<test>");
	let u = ast::subunit {
		imports = [],

M strio/fixed.ha => strio/fixed.ha +1 -0
@@ 57,6 57,7 @@ fn fixed_close(s: *io::stream) void = {
@test fn fixed() void = {
	static let buf: [1024]u8 = [0...];
	let stream = fixed(buf);
	defer io::close(stream);
	io::write(stream, strings::toutf8("hello ")) as size;
	io::write(stream, strings::toutf8("world")) as size;
	assert(string(stream) == "hello world");

M unix/passwd/passwd.ha => unix/passwd/passwd.ha +1 -1
@@ 103,7 103,7 @@ export fn getuser(username: str) (pwent | void) = {
	let buf = bufio::fixed(strings::toutf8(
		"sircmpwn:x:1000:1000:sircmpwn's comment:/home/sircmpwn:/bin/mrsh\n"
		"alex:x:1001:1001::/home/alex:/bin/zsh"), io::mode::READ);
	defer free(buf);
	defer io::close(buf);

	let ent = nextpw(buf) as pwent;
	defer pwent_finish(ent);