~sircmpwn/harebot

319ea157cb027ca80ccfad4271b53c24bf69be23 — Drew DeVault 5 months ago 650deea
Update match..case syntax
2 files changed, 22 insertions(+), 21 deletions(-)

M haredoc.ha
M main.ha
M haredoc.ha => haredoc.ha +16 -16
@@ 50,7 50,7 @@ fn handle_haredoc(
	let decl = "";
	let dirname = if (len(id) < 2) id else id[..len(id) - 1];
	const version = match (module::lookup(&ctx, id)) {
	case ver: module::version =>
	case let ver: module::version =>
		let path = module::identpath(id);
		defer free(path);
		let link = fmt::asprintf("{}: https://docs.harelang.org/{}",


@@ 58,7 58,7 @@ fn handle_haredoc(
		defer free(link);
		irc::privmsg(state.conn, link, msg.params[0])!;
		return;
	case err: module::error =>
	case let err: module::error =>
		let ver = module::lookup(&ctx, dirname)?;
		assert(len(id) >= 2);
		decl = id[len(id) - 1];


@@ 89,7 89,7 @@ fn handle_haredoc(
		defer free(resp);
		irc::privmsg(state.conn, resp, msg.params[0])!;
		return;
	case d: *ast::decl =>
	case let d: *ast::decl =>
		yield d;
	};



@@ 104,9 104,9 @@ fn handle_haredoc(

fn scan(path: str) (ast::subunit | error) = {
	const input = match (os::open(path)) {
	case f: io::file =>
	case let f: io::file =>
		yield f;
	case err: fs::error =>
	case let err: fs::error =>
		fmt::fatal("Error reading {}: {}", path, fs::strerror(err));
	};
	defer io::close(input);


@@ 120,21 120,21 @@ fn has_decl(decl: ast::decl, name: str) bool = {
	};

	match (decl.decl) {
	case d: []ast::decl_const =>
	case let d: []ast::decl_const =>
		for (let i = 0z; i < len(d); i += 1) {
			if (len(d[i].ident) == 1 && d[i].ident[0] == name) {
				return true;
			};
		};
	case d: ast::decl_func =>
	case let d: ast::decl_func =>
		return len(d.ident) == 1 && d.ident[0] == name;
	case d: []ast::decl_global =>
	case let d: []ast::decl_global =>
		for (let i = 0z; i < len(d); i += 1) {
			if (len(d[i].ident) == 1 && d[i].ident[0] == name) {
				return true;
			};
		};
	case d: []ast::decl_type =>
	case let d: []ast::decl_type =>
		for (let i = 0z; i < len(d); i += 1) {
			if (len(d[i].ident) == 1 && d[i].ident[0] == name) {
				return true;


@@ 148,7 148,7 @@ fn has_decl(decl: ast::decl, name: str) bool = {
fn unparse_hare(out: io::handle, d: ast::decl) (size | io::error) = {
	let n = 0z;
	match (d.decl) {
	case g: []ast::decl_global =>
	case let g: []ast::decl_global =>
		n += fmt::fprint(out,
			if (g[0].is_const) "const " else "let ")?;
		for (let i = 0z; i < len(g); i += 1) {


@@ 163,20 163,20 @@ fn unparse_hare(out: io::handle, d: ast::decl) (size | io::error) = {
				n += fmt::fprint(out, ", ")?;
			};
		};
	case t: []ast::decl_type =>
	case let t: []ast::decl_type =>
		n += fmt::fprint(out, "type ")?;
		for (let i = 0z; i < len(t); i += 1) {
			n += unparse::ident(out, t[i].ident)?;
			n += fmt::fprint(out, " = ")?;
			match (t[i]._type.repr) {
			case a: ast::alias_type =>
			case let a: ast::alias_type =>
				if (a.unwrap) {
					n += fmt::fprintf(out, "...")?;
				};
				n += unparse::ident(out, a.ident)?;
			case b: ast::builtin_type =>
			case let b: ast::builtin_type =>
				n += fmt::fprint(out, unparse::builtin_type(b))?;
			case e: ast::enum_type =>
			case let e: ast::enum_type =>
				n += fmt::fprint(out, "enum")?;
			case (ast::func_type | ast::list_type) =>
				n += unparse::_type(out, 0, t[i]._type)?;


@@ 187,7 187,7 @@ fn unparse_hare(out: io::handle, d: ast::decl) (size | io::error) = {
				n += fmt::fprint(out, ", ")?;
			};
		};
	case c: []ast::decl_const =>
	case let c: []ast::decl_const =>
		n += fmt::fprint(out, "def ")?;
		for (let i = 0z; i < len(c); i += 1) {
			n += unparse::ident(out, c[i].ident)?;


@@ 197,7 197,7 @@ fn unparse_hare(out: io::handle, d: ast::decl) (size | io::error) = {
				n += fmt::fprint(out, ", ")?;
			};
		};
	case f: ast::decl_func =>
	case let f: ast::decl_func =>
		n += fmt::fprint(out, switch (f.attrs) {
		case ast::fndecl_attrs::NONE =>
			yield "";

M main.ha => main.ha +6 -5
@@ 1,6 1,7 @@
use fmt;
use hare::module;
use hare::parse;
use io;
use net::irc;
use os;
use shlex;


@@ 26,9 27,9 @@ export fn main() void = {
	};

	let conn = match (irc::dial(network, &conf)) {
	case conn: *irc::client =>
	case let conn: *irc::client =>
		yield conn;
	case err: irc::error =>
	case let err: irc::error =>
		fmt::fatal("Failed to dial IRC server: {}",
			irc::strerror(err));
	};


@@ 67,7 68,7 @@ fn handle_privmsg(
) (void | irc::error) = {
	let state = user: *state;
	const cmd = match (shlex::split(msg.params[1])) {
	case items: []str =>
	case let items: []str =>
		yield items;
	case shlex::syntaxerr =>
		return;


@@ 86,9 87,9 @@ fn handle_privmsg(

	match (handler(state, msg, cmd)) {
	case void => void;
	case err: parse::error =>
	case let err: parse::error =>
		irc::privmsg(state.conn, parse::strerror(err), msg.params[0])!;
	case err: module::error =>
	case let err: module::error =>
		irc::privmsg(state.conn, module::strerror(err), msg.params[0])!;
	};
};