aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-02-17 17:35:16 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-02-17 17:35:16 -0500
commit1bcf6bdc9a2652366bc152a1d98524743623bdf5 (patch)
tree9e5a724b1c4a4167fd8472d93e0e530c6ed04c02
parentdbd7502a1dcee6b99434f39393cb356efa542ddc (diff)
Add cord methods for getting typestrings
-rw-r--r--builtins/array.c3
-rw-r--r--builtins/floats.c2
-rw-r--r--builtins/string.c9
-rw-r--r--builtins/table.c4
-rw-r--r--builtins/types.c5
5 files changed, 20 insertions, 3 deletions
diff --git a/builtins/array.c b/builtins/array.c
index d2c5444e..249281ce 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 cb71828a..1533b00d 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 31998d66..4cfa00ee 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 7df98e50..cd52cf1d 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 24c4de92..b8f9053b 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;