Add cord methods for getting typestrings

This commit is contained in:
Bruce Hill 2024-02-17 17:35:16 -05:00
parent dbd7502a1d
commit 1bcf6bdc9a
5 changed files with 20 additions and 3 deletions

View File

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

View File

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

View File

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

View File

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

View File

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