For single-member structs/enums, don't print the member name

This commit is contained in:
Bruce Hill 2024-09-12 04:09:52 -04:00
parent fb6dc0a8b9
commit 43f4f3610e
7 changed files with 25 additions and 11 deletions

16
enums.c
View File

@ -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");
}

View File

@ -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) {

View File

@ -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]

View File

@ -14,7 +14,7 @@ func main():
>> [:ImportedType]
>> returns_imported_type()
= ImportedType(name="Hello")
= ImportedType("Hello")
>> needs_initializing # imported from ./use_import.tm
= 999999999999999999

View File

@ -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")

View File

@ -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)

View File

@ -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():