Rename 'cord' to 'as_str'

This commit is contained in:
Bruce Hill 2024-02-17 17:44:17 -05:00
parent 1bcf6bdc9a
commit 1dcfbdc5c7
14 changed files with 48 additions and 43 deletions

View File

@ -272,14 +272,14 @@ 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), "]");
return CORD_all("[", generic_as_str(NULL, false, type->ArrayInfo.item), "]");
TypeInfo *item_type = type->ArrayInfo.item;
CORD c = "[";
for (int64_t i = 0; i < arr->length; i++) {
if (i > 0)
c = CORD_cat(c, ", ");
CORD item_cord = generic_cord(arr->data + i*arr->stride, colorize, item_type);
CORD item_cord = generic_as_str(arr->data + i*arr->stride, colorize, item_type);
c = CORD_cat(c, item_cord);
}
c = CORD_cat(c, "]");

View File

@ -19,7 +19,7 @@ array_t Array_slice(array_t *array, int64_t first, int64_t stride, int64_t lengt
uint32_t Array_hash(const array_t *arr, const TypeInfo *type);
int32_t Array_compare(const array_t *x, const array_t *y, const TypeInfo *type);
bool Array_equal(const array_t *x, const array_t *y, const TypeInfo *type);
CORD Array_cord(const array_t *arr, bool colorize, const TypeInfo *type);
CORD Array_as_str(const array_t *arr, bool colorize, const TypeInfo *type);
// Due to some C language weirdness, the type of "foo" is inferred to be `char[3]` instead of `const char*`
// This is a hacky workaround to ensure that __typeof("foo") => `const char *`

View File

@ -15,7 +15,7 @@
extern const void *SSS_HASH_VECTOR;
static CORD Bool_cord(const bool *b, bool colorize, const TypeInfo *type)
static CORD Bool__as_str(const bool *b, bool colorize, const TypeInfo *type)
{
(void)type;
if (!b) return "Bool";
@ -32,7 +32,7 @@ public struct {
.size=sizeof(bool),
.align=alignof(bool),
.tag=CustomInfo,
.CustomInfo={.cord=(void*)Bool_cord},
.CustomInfo={.as_str=(void*)Bool__as_str},
},
};

View File

@ -14,7 +14,7 @@
#include "string.h"
#include "types.h"
public CORD Num__cord(const double *f, bool colorize, const TypeInfo *type) {
public CORD Num__as_str(const double *f, bool colorize, const TypeInfo *type) {
(void)type;
if (!f) return "Num";
CORD c;
@ -81,7 +81,7 @@ public struct {
.CustomInfo={
.compare=(void*)Num__compare,
.equal=(void*)Num__equal,
.cord=(void*)Num__cord,
.as_str=(void*)Num__as_str,
},
},
.NaN=NAN, ._2_sqrt_pi=M_2_SQRTPI, .e=M_E, .half_pi=M_PI_2, .inf=1./0., .inverse_half_pi=M_2_PI,
@ -103,7 +103,7 @@ public struct {
.scientific=Num__scientific,
};
public CORD Num32__cord(float *f, bool colorize, const TypeInfo *type) {
public CORD Num32__as_str(float *f, bool colorize, const TypeInfo *type) {
(void)type;
if (!f) return "Num32";
CORD c;
@ -174,7 +174,7 @@ public struct {
.CustomInfo={
.compare=(void*)Num32__compare,
.equal=(void*)Num32__equal,
.cord=(void*)Num32__cord,
.as_str=(void*)Num32__as_str,
},
},
.NaN=NAN, ._2_sqrt_pi=M_2_SQRTPI, .e=M_E, .half_pi=M_PI_2, .inf=1./0., .inverse_half_pi=M_2_PI,

View File

@ -14,6 +14,7 @@
#include "table.h"
#include "pointer.h"
#include "string.h"
#include "types.h"
extern bool USE_COLOR;
extern const void *SSS_HASH_VECTOR;
@ -78,16 +79,17 @@ public bool generic_equal(const void *x, const void *y, const TypeInfo *type)
}
}
public CORD generic_cord(const void *obj, bool colorize, const TypeInfo *type)
public CORD generic_as_str(const void *obj, bool colorize, const TypeInfo *type)
{
switch (type->tag) {
case PointerInfo: return Pointer__cord(obj, colorize, type);
case ArrayInfo: return Array_cord(obj, colorize, type);
case TableInfo: return Table_cord(obj, colorize, type);
case ArrayInfo: return Array_as_str(obj, colorize, type);
case TableInfo: return Table_as_str(obj, colorize, type);
case TypeInfoInfo: return Type__as_str(obj, colorize, type);
case CustomInfo:
if (!type->CustomInfo.cord)
if (!type->CustomInfo.as_str)
builtin_fail("No cord function provided for type!\n");
return type->CustomInfo.cord(obj, colorize, type);
return type->CustomInfo.as_str(obj, colorize, type);
default: errx(1, "Invalid type tag: %d", type->tag);
}
}
@ -127,12 +129,12 @@ public void __doctest(CORD label, void *expr, TypeInfo *type, CORD expected, con
CORD_fprintf(stderr, USE_COLOR ? "\x1b[33;1m>>> \x1b[0m%.*s\x1b[m\n" : ">>> %.*s\n", (end - start), file->text + start);
if (expr) {
CORD expr_str = generic_cord(expr, USE_COLOR, type);
CORD type_name = generic_cord(NULL, false, type);
CORD expr_str = generic_as_str(expr, USE_COLOR, type);
CORD type_name = generic_as_str(NULL, false, type);
CORD_fprintf(stderr, USE_COLOR ? "\x1b[2m%r\x1b[0m %r \x1b[2m: %r\x1b[m\n" : "%r %r : %r\n", label, expr_str, type_name);
if (expected) {
CORD expr_plain = USE_COLOR ? generic_cord(expr, false, type) : expr_str;
CORD expr_plain = USE_COLOR ? generic_as_str(expr, false, type) : expr_str;
bool success = (CORD_cmp(expr_str, expected) == 0);
if (!success && CORD_chr(expected, 0, ':')) {
expr_plain = heap_strf("%s : %s", expr_plain, type_name);

View File

@ -4,11 +4,16 @@
#include <stdbool.h>
#include <stdint.h>
#include "string.h"
#include "types.h"
void builtin_say(CORD str, CORD end);
void builtin_fail(CORD fmt, ...);
CORD builtin_last_err();
void builtin_doctest(const char *label, CORD expr, const char *type, bool use_color, const char *expected, const char *filename, int start, int end);
uint32_t generic_hash(const void *obj, const TypeInfo *type);
int32_t generic_compare(const void *x, const void *y, const TypeInfo *type);
bool generic_equal(const void *x, const void *y, const TypeInfo *type);
CORD generic_as_str(const void *obj, bool colorize, const TypeInfo *type);
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0

View File

@ -16,7 +16,7 @@ extern const void *SSS_HASH_VECTOR;
#define str(a) #a
#define DEFINE_INT_TYPE(c_type, KindOfInt, fmt, abs_fn, min_val, max_val)\
public CORD KindOfInt ## __cord(const c_type *i, bool colorize, const TypeInfo *type) { \
public CORD KindOfInt ## __as_str(const c_type *i, bool colorize, const TypeInfo *type) { \
(void)type; \
if (!i) return #KindOfInt; \
CORD c; \
@ -61,7 +61,7 @@ extern const void *SSS_HASH_VECTOR;
.size=sizeof(c_type), \
.align=alignof(c_type), \
.tag=CustomInfo, \
.CustomInfo={.compare=(void*)KindOfInt##__compare, .cord=(void*)KindOfInt##__cord}, \
.CustomInfo={.compare=(void*)KindOfInt##__compare, .as_str=(void*)KindOfInt##__as_str}, \
}, \
.min=min_val, \
.max=max_val, \

View File

@ -15,7 +15,7 @@
extern const void *SSS_HASH_VECTOR;
public CORD Memory__cord(const void *p, bool colorize, const TypeInfo *type) {
public CORD Memory__as_str(const void *p, bool colorize, const TypeInfo *type) {
(void)type;
if (!p) return "Memory";
CORD cord;
@ -27,7 +27,7 @@ public TypeInfo Memory_type = {
.size=0,
.align=0,
.tag=CustomInfo,
.CustomInfo={.cord=(void*)Memory__cord},
.CustomInfo={.as_str=(void*)Memory__as_str},
};
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0

View File

@ -9,9 +9,10 @@
#include <sys/param.h>
#include <err.h>
#include "types.h"
#include "../util.h"
#include "../SipHash/halfsiphash.h"
#include "functions.h"
#include "types.h"
extern const void *SSS_HASH_VECTOR;
@ -24,7 +25,7 @@ public CORD Pointer__cord(const void *x, bool colorize, const TypeInfo *type) {
auto ptr_info = type->PointerInfo;
const void *ptr = *(const void**)x;
if (!ptr) {
CORD typename = generic_cord(NULL, false, ptr_info.pointed);
CORD typename = generic_as_str(NULL, false, ptr_info.pointed);
return colorize ? CORD_asprintf("\x1b[34;1m!%s\x1b[m", typename) : CORD_cat(ptr_info.sigil, typename);
}
@ -42,7 +43,7 @@ public CORD Pointer__cord(const void *x, bool colorize, const TypeInfo *type) {
{ // Stringify with this pointer flagged as a recursive one:
recursion_t my_recursion = {.ptr=ptr, .next=recursion};
recursion = &my_recursion;
pointed = generic_cord(ptr, colorize, ptr_info.pointed);
pointed = generic_as_str(ptr, colorize, ptr_info.pointed);
recursion = recursion->next;
}
return colorize ? CORD_asprintf("\x1b[34;1m%s%r\x1b[m", ptr_info.sigil, pointed) : CORD_cat(ptr_info.sigil, pointed);

View File

@ -21,7 +21,7 @@
extern const void *SSS_HASH_VECTOR;
public CORD Str__cord(const void *str, bool colorize, const TypeInfo *info)
public CORD Str__as_str(const void *str, bool colorize, const TypeInfo *info)
{
(void)info;
if (!str) return "Str";
@ -263,7 +263,7 @@ public Str_namespace_t Str_type = {
.align=alignof(CORD),
.tag=CustomInfo,
.CustomInfo={
.cord=(void*)Str__cord,
.as_str=(void*)Str__as_str,
.compare=(void*)Str__compare,
.equal=(void*)Str__equal,
.hash=(void*)Str__hash,

View File

@ -484,13 +484,13 @@ public uint32_t Table_hash(const table_t *t, const TypeInfo *type)
return hash;
}
public CORD Table_cord(const table_t *t, bool colorize, const TypeInfo *type)
public CORD Table_as_str(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), "}");
return CORD_all("{", generic_as_str(NULL, false, table.key), "=>", generic_as_str(NULL, false, table.value), "}");
int64_t value_offset = table.value_offset;
CORD c = "{";
@ -498,19 +498,19 @@ public CORD Table_cord(const table_t *t, bool colorize, const TypeInfo *type)
if (i > 0)
c = CORD_cat(c, ", ");
void *entry = GET_ENTRY(t, i);
c = CORD_cat(c, generic_cord(entry, colorize, table.key));
c = CORD_cat(c, generic_as_str(entry, colorize, table.key));
c = CORD_cat(c, "=>");
c = CORD_cat(c, generic_cord(entry + value_offset, colorize, table.value));
c = CORD_cat(c, generic_as_str(entry + value_offset, colorize, table.value));
}
if (t->fallback) {
c = CORD_cat(c, "; fallback=");
c = CORD_cat(c, Table_cord(t->fallback, colorize, type));
c = CORD_cat(c, Table_as_str(t->fallback, colorize, type));
}
if (t->default_value) {
c = CORD_cat(c, t->fallback ? "; default=" : "; default=");
c = CORD_cat(c, generic_cord(t->default_value, colorize, table.value));
c = CORD_cat(c, generic_as_str(t->default_value, colorize, table.value));
}
c = CORD_cat(c, "}");

View File

@ -20,7 +20,7 @@ void Table_mark_copy_on_write(table_t *t);
int32_t Table_compare(const table_t *x, const table_t *y, const TypeInfo *type);
bool Table_equal(const table_t *x, const table_t *y, const TypeInfo *type);
uint32_t Table_hash(const table_t *t, const TypeInfo *type);
CORD Table_cord(const table_t *t, bool colorize, const TypeInfo *type);
CORD Table_as_str(const table_t *t, bool colorize, const TypeInfo *type);
void *Table_str_entry(const table_t *t, int64_t n);
void *Table_str_get(const table_t *t, const char *key);

View File

@ -15,7 +15,7 @@
extern const void *SSS_HASH_VECTOR;
public CORD Type__cord(void *typeinfo, bool colorize, const TypeInfo *type)
public CORD Type__as_str(const void *typeinfo, bool colorize, const TypeInfo *type)
{
if (!typeinfo) return "TypeInfo";
@ -33,7 +33,7 @@ public struct {
.size=sizeof(TypeInfo),
.align=alignof(TypeInfo),
.tag=CustomInfo,
.CustomInfo={.cord=(void*)Type__cord},
.TypeInfoInfo.type_str="TypeInfo",
},
};
@ -44,7 +44,7 @@ public struct {
TypeInfo type;
} Abort_type = {.type={.size=0, .align=0}};
public CORD Func__cord(const void *fn, bool colorize, const TypeInfo *type)
public CORD Func__as_str(const void *fn, bool colorize, const TypeInfo *type)
{
(void)fn;
CORD c = type->TypeInfoInfo.type_str;

View File

@ -10,7 +10,7 @@ struct TypeInfo;
typedef uint32_t (*hash_fn_t)(const void*, const struct TypeInfo*);
typedef int32_t (*compare_fn_t)(const void*, const void*, const struct TypeInfo*);
typedef bool (*equal_fn_t)(const void*, const void*, const struct TypeInfo*);
typedef CORD (*cord_fn_t)(const void*, bool, const struct TypeInfo*);
typedef CORD (*str_fn_t)(const void*, bool, const struct TypeInfo*);
typedef struct TypeInfo {
int64_t size, align;
@ -21,7 +21,7 @@ typedef struct TypeInfo {
equal_fn_t equal;
compare_fn_t compare;
hash_fn_t hash;
cord_fn_t cord;
str_fn_t as_str;
} CustomInfo;
struct {
const char *sigil;
@ -41,9 +41,6 @@ typedef struct TypeInfo {
};
} TypeInfo;
uint32_t generic_hash(const void *obj, const TypeInfo *type);
int32_t generic_compare(const void *x, const void *y, const TypeInfo *type);
bool generic_equal(const void *x, const void *y, const TypeInfo *type);
CORD generic_cord(const void *obj, bool colorize, const TypeInfo *type);
CORD Type__as_str(const void *typeinfo, bool colorize, const TypeInfo *type);
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0