hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 8af6338b5b4a1e7dc3e40362d17e44d357bc33bf
parent 989dddb3ffcb2c93d3be39c7ea0e5a452e181966
Author: Sebastian <sebastian@sebsite.pw>
Date:   Wed, 17 May 2023 13:39:13 -0400

hare::*: permit C-style variadism with no named params

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mhare/parse/+test/unit.ha | 1+
Mhare/parse/type.ha | 2--
Mhare/unparse/type.ha | 27++++++++++++---------------
3 files changed, 13 insertions(+), 17 deletions(-)

diff --git a/hare/parse/+test/unit.ha b/hare/parse/+test/unit.ha @@ -125,6 +125,7 @@ fn tup_to_import(tup: import_tuple) ast::import = ast::import { "def foo::bar = void;\n\n" "@symbol(\".f9$oo\") fn foo(bar: int, baz: int...) void;\n\n" "@test fn foo(int, ...) void;\n\n" + "@test fn foo(...) void;\n\n" "fn foo(bar) void;\n\n" "fn foo(bar::baz) void;\n\n" "export fn main() void = void;\n\n" diff --git a/hare/parse/type.ha b/hare/parse/type.ha @@ -18,8 +18,6 @@ fn prototype(lexer: *lex::lexer) (ast::func_type | error) = { case void => yield void; case lex::token => - synassert(loc, len(params) > 0, - "Expected at least one non-variadic parameter for C-style variadism")?; variadism = ast::variadism::C; try(lexer, ltok::COMMA)?; want(lexer, ltok::RPAREN)?; diff --git a/hare/unparse/type.ha b/hare/unparse/type.ha @@ -129,22 +129,19 @@ export fn prototype( }; indent -= 1; n += newline(out, indent)?; - } else for (let i = 0z; i < len(t.params); i += 1) { - const param = t.params[i]; - if (param.name != "") { - n += fmt::fprintf(out, "{}: ", param.name)?; - }; - n += fmt::fprint(out, typenames[i])?; - if (i + 1 == len(t.params)) { - switch (t.variadism) { - case variadism::NONE => void; - case variadism::HARE => - n += fmt::fprint(out, "...")?; - case variadism::C => - n += fmt::fprint(out, ", ...")?; + } else { + for (let i = 0z; i < len(t.params); i += 1) { + const param = t.params[i]; + if (param.name != "") { + n += fmt::fprintf(out, "{}: ", param.name)?; }; - } else { - n += fmt::fprint(out, ", ")?; + n += fmt::fprint(out, typenames[i])?; + if (i + 1 < len(t.params) || t.variadism == variadism::C) { + n += fmt::fprint(out, ", ")?; + }; + }; + if (t.variadism != variadism::NONE) { + n += fmt::fprint(out, "...")?; }; };