From 1bcf6bdc9a2652366bc152a1d98524743623bdf5 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 17 Feb 2024 17:35:16 -0500 Subject: [PATCH] Add cord methods for getting typestrings --- builtins/array.c | 3 +++ builtins/floats.c | 2 ++ builtins/string.c | 9 ++++++++- builtins/table.c | 4 ++++ builtins/types.c | 5 +++-- 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/builtins/array.c b/builtins/array.c index d2c5444..249281c 100644 --- a/builtins/array.c +++ b/builtins/array.c @@ -271,6 +271,9 @@ public bool Array_equal(const array_t *x, const array_t *y, const TypeInfo *type public CORD Array_cord(const array_t *arr, bool colorize, const TypeInfo *type) { + if (!arr) + return CORD_all("[", generic_cord(NULL, false, type->ArrayInfo.item), "]"); + TypeInfo *item_type = type->ArrayInfo.item; CORD c = "["; for (int64_t i = 0; i < arr->length; i++) { diff --git a/builtins/floats.c b/builtins/floats.c index cb71828..1533b00 100644 --- a/builtins/floats.c +++ b/builtins/floats.c @@ -16,6 +16,7 @@ public CORD Num__cord(const double *f, bool colorize, const TypeInfo *type) { (void)type; + if (!f) return "Num"; CORD c; if (colorize) CORD_sprintf(&c, "\x1b[35m%g\x1b[33;2m\x1b[m", *f); else CORD_sprintf(&c, "%g", *f); @@ -104,6 +105,7 @@ public struct { public CORD Num32__cord(float *f, bool colorize, const TypeInfo *type) { (void)type; + if (!f) return "Num32"; CORD c; if (colorize) CORD_sprintf(&c, "\x1b[35m%g_f32\x1b[m", *f); else CORD_sprintf(&c, "%g_f32", *f); diff --git a/builtins/string.c b/builtins/string.c index 31998d6..4cfa00e 100644 --- a/builtins/string.c +++ b/builtins/string.c @@ -21,6 +21,13 @@ extern const void *SSS_HASH_VECTOR; +public CORD Str__cord(const void *str, bool colorize, const TypeInfo *info) +{ + (void)info; + if (!str) return "Str"; + return Str__quoted(*(CORD*)str, colorize); +} + public CORD Str__quoted(CORD str, bool colorize) { // Note: it's important to have unicode strings not get broken up with @@ -256,7 +263,7 @@ public Str_namespace_t Str_type = { .align=alignof(CORD), .tag=CustomInfo, .CustomInfo={ - .cord=(void*)Str__quoted, + .cord=(void*)Str__cord, .compare=(void*)Str__compare, .equal=(void*)Str__equal, .hash=(void*)Str__hash, diff --git a/builtins/table.c b/builtins/table.c index 7df98e5..cd52cf1 100644 --- a/builtins/table.c +++ b/builtins/table.c @@ -488,6 +488,10 @@ public CORD Table_cord(const table_t *t, bool colorize, const TypeInfo *type) { assert(type->tag == TableInfo); auto table = type->TableInfo; + + if (!t) + return CORD_all("{", generic_cord(NULL, false, table.key), "=>", generic_cord(NULL, false, table.value), "}"); + int64_t value_offset = table.value_offset; CORD c = "{"; for (int64_t i = 0, length = Table_length(t); i < length; i++) { diff --git a/builtins/types.c b/builtins/types.c index 24c4de9..b8f9053 100644 --- a/builtins/types.c +++ b/builtins/types.c @@ -15,9 +15,10 @@ extern const void *SSS_HASH_VECTOR; -public CORD Type__cord(void *type_namespace, bool colorize, const TypeInfo *type) +public CORD Type__cord(void *typeinfo, bool colorize, const TypeInfo *type) { - (void)type_namespace; + if (!typeinfo) return "TypeInfo"; + if (!colorize) return type->TypeInfoInfo.type_str; CORD c;