From 43f4f3610e5258afbfb9e313c989e1e52f477c38 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 12 Sep 2024 04:09:52 -0400 Subject: For single-member structs/enums, don't print the member name --- enums.c | 16 +++++++++++----- structs.c | 5 +++++ test/enums.tm | 4 ++-- test/import.tm | 2 +- test/optionals.tm | 4 ++-- test/structs.tm | 3 +++ test/threads.tm | 2 +- 7 files changed, 25 insertions(+), 11 deletions(-) diff --git a/enums.c b/enums.c index a0817b01..77ad1c14 100644 --- a/enums.c +++ b/enums.c @@ -42,11 +42,17 @@ static CORD compile_str_method(env_t *env, ast_t *ast) continue; } - for (arg_ast_t *field = tag->fields; field; field = field->next) { - type_t *field_t = get_arg_ast_type(env, field); - CORD field_str = expr_as_text(env, CORD_all("obj->$", tag->name, ".$", field->name), field_t, "use_color"); - str_func = CORD_all(str_func, ", Text(\"", field->name, "=\"), ", field_str); - if (field->next) str_func = CORD_cat(str_func, ", Text(\", \")"); + if (tag->fields && !tag->fields->next) { // Single-member tags don't need to print member names: + type_t *field_t = get_arg_ast_type(env, tag->fields); + CORD field_str = expr_as_text(env, CORD_all("obj->$", tag->name, ".$", tag->fields->name), field_t, "use_color"); + str_func = CORD_all(str_func, ", ", field_str); + } else { + for (arg_ast_t *field = tag->fields; field; field = field->next) { + type_t *field_t = get_arg_ast_type(env, field); + CORD field_str = expr_as_text(env, CORD_all("obj->$", tag->name, ".$", field->name), field_t, "use_color"); + str_func = CORD_all(str_func, ", Text(\"", field->name, "=\"), ", field_str); + if (field->next) str_func = CORD_cat(str_func, ", Text(\", \")"); + } } str_func = CORD_cat(str_func, ", Text(\")\"));\n"); } diff --git a/structs.c b/structs.c index cc3de838..7aa2ddf9 100644 --- a/structs.c +++ b/structs.c @@ -23,6 +23,11 @@ static CORD compile_str_method(env_t *env, ast_t *ast) if (def->secret) { CORD_appendf(&str_func, "\treturn use_color ? Text(\"\\x1b[0;1m%s\\x1b[m(\\x1b[2m...\\x1b[m)\") : Text(\"%s(...)\");\n}", name, name); + } else if (def->fields && !def->fields->next) { // Single-member structs don't need to print names: + type_t *field_type = get_arg_ast_type(env, def->fields); + CORD field_str = expr_as_text(env, CORD_cat("obj->$", def->fields->name), field_type, "use_color"); + str_func = CORD_all(str_func, "\treturn Text$concat(use_color ? Text(\"\\x1b[0;1m", name, "\\x1b[m(\") : Text(\"", name, "(\"), ", + field_str, ", Text(\")\"));\n}\n"); } else { CORD_appendf(&str_func, "\treturn Text$concat(use_color ? Text(\"\\x1b[0;1m%s\\x1b[m(\") : Text(\"%s(\")", name, name); for (arg_ast_t *field = def->fields; field; field = field->next) { diff --git a/test/enums.tm b/test/enums.tm index 98811408..a96f5784 100644 --- a/test/enums.tm +++ b/test/enums.tm @@ -19,7 +19,7 @@ func main(): >> Foo.Zero = Foo.Zero >> Foo.One(123) - = Foo.One(x=123) + = Foo.One(123) >> Foo.Two(123, 456) = Foo.Two(x=123, y=456) @@ -59,7 +59,7 @@ func main(): >> choose_text(Foo.Four(1,2,3,4)) = "Four" >> choose_text(Foo.Last("XX")) - = "else: Foo.Last(t=\"XX\")" + = "else: Foo.Last(\"XX\")" i := 1 cases := [Foo.One(1), Foo.One(2), Foo.Zero] diff --git a/test/import.tm b/test/import.tm index 9c878002..c7797963 100644 --- a/test/import.tm +++ b/test/import.tm @@ -14,7 +14,7 @@ func main(): >> [:ImportedType] >> returns_imported_type() - = ImportedType(name="Hello") + = ImportedType("Hello") >> needs_initializing # imported from ./use_import.tm = 999999999999999999 diff --git a/test/optionals.tm b/test/optionals.tm index c0c600a7..5c18d58d 100644 --- a/test/optionals.tm +++ b/test/optionals.tm @@ -215,12 +215,12 @@ func main(): !! ... !! Enums: >> yep := Enum.maybe(yes) - = Enum.Y(y=123)? + = Enum.Y(123)? >> nope := Enum.maybe(no) = !Enum >> if yep: >> yep - = Enum.Y(y=123) + = Enum.Y(123) else: fail("Falsey: $yep") >> if nope: fail("Truthy: $nope") diff --git a/test/structs.tm b/test/structs.tm index 53d356f5..d68bbe8b 100644 --- a/test/structs.tm +++ b/test/structs.tm @@ -1,4 +1,5 @@ +struct Single(x:Int) struct Pair(x,y:Int) struct Mixed(x:Int, text:Text) struct LinkedList(x:Int, next=!@LinkedList) @@ -8,6 +9,8 @@ struct CorecursiveA(other:@CorecursiveB?) struct CorecursiveB(other=!@CorecursiveA) func test_literals(): + >> Single(123) + = Single(123) >> x := Pair(10, 20) = Pair(x=10, y=20) >> y := Pair(y=20, 10) diff --git a/test/threads.tm b/test/threads.tm index 2f259a0e..2ac2b679 100644 --- a/test/threads.tm +++ b/test/threads.tm @@ -22,7 +22,7 @@ func main(): jobs := |:Job; max_size=2| >> jobs:give(Increment(5)) >> jobs:peek() - = Job.Increment(x=5) + = Job.Increment(5) results := |:Int; max_size| >> thread := Thread.new(func(): -- cgit v1.2.3