diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-04-30 13:18:47 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-04-30 13:18:47 -0400 |
| commit | 2e27b88c1b9fa8ec9b9f243909f5b793b376f207 (patch) | |
| tree | f70433489c8a1fbf3bae6bf4cce364f6bdf8c9f7 /builtins | |
| parent | 3c0a8f0b899a343f43caf9c95147b2cf77a7b525 (diff) | |
Improved syntax for optionals
Diffstat (limited to 'builtins')
| -rw-r--r-- | builtins/pointer.c | 14 | ||||
| -rw-r--r-- | builtins/types.h | 5 |
2 files changed, 13 insertions, 6 deletions
diff --git a/builtins/pointer.c b/builtins/pointer.c index 7fe8c160..d71811b0 100644 --- a/builtins/pointer.c +++ b/builtins/pointer.c @@ -22,7 +22,8 @@ public CORD Pointer$as_text(const void *x, bool colorize, const TypeInfo *type) auto ptr_info = type->PointerInfo; if (!x) { CORD typename = generic_as_text(NULL, false, ptr_info.pointed); - return colorize ? CORD_asprintf("\x1b[34;1m%s%s\x1b[m", ptr_info.sigil, typename) : CORD_cat(ptr_info.sigil, typename); + CORD c = colorize ? CORD_asprintf("\x1b[34;1m%s%s\x1b[m", ptr_info.sigil, typename) : CORD_cat(ptr_info.sigil, typename); + return ptr_info.is_optional ? CORD_cat(c, "?") : c; } const void *ptr = *(const void**)x; if (!ptr) { @@ -36,8 +37,11 @@ public CORD Pointer$as_text(const void *x, bool colorize, const TypeInfo *type) int32_t depth = 0; for (recursion_t *r = recursion; r; r = r->next) { ++depth; - if (r->ptr == ptr) - return CORD_asprintf(colorize ? "\x1b[34;1m%s..%d\x1b[m" : "%s..%d", ptr_info.sigil, depth); + if (r->ptr == ptr) { + CORD c = CORD_asprintf(colorize ? "\x1b[34;1m%s..%d\x1b[m" : "%s..%d", ptr_info.sigil, depth); + if (ptr_info.is_optional) c = CORD_cat(c, colorize ? "\x1b[34;1m?\x1b[m" : "?"); + return c; + } } CORD pointed; @@ -47,7 +51,9 @@ public CORD Pointer$as_text(const void *x, bool colorize, const TypeInfo *type) pointed = generic_as_text(ptr, colorize, ptr_info.pointed); recursion = recursion->next; } - return colorize ? CORD_asprintf("\x1b[34;1m%s\x1b[m%r", ptr_info.sigil, pointed) : CORD_cat(ptr_info.sigil, pointed); + CORD c = colorize ? CORD_asprintf("\x1b[34;1m%s\x1b[m%r", ptr_info.sigil, pointed) : CORD_cat(ptr_info.sigil, pointed); + if (ptr_info.is_optional) c = CORD_cat(c, colorize ? "\x1b[34;1m?\x1b[m" : "?"); + return c; } public int32_t Pointer$compare(const void *x, const void *y, const TypeInfo *type) { diff --git a/builtins/types.h b/builtins/types.h index ee49d9ac..3b517fd3 100644 --- a/builtins/types.h +++ b/builtins/types.h @@ -28,6 +28,7 @@ typedef struct TypeInfo { } CustomInfo; struct { const char *sigil; + bool is_optional; const struct TypeInfo *pointed; } PointerInfo; struct { @@ -50,8 +51,8 @@ typedef struct TypeInfo { }; } TypeInfo; -#define $PointerInfo(sigil_expr, pointed_info) &((TypeInfo){.size=sizeof(void*), .align=__alignof__(void*), \ - .tag=PointerInfo, .PointerInfo.sigil=sigil_expr, .PointerInfo.pointed=pointed_info}) +#define $PointerInfo(sigil_expr, pointed_info, opt) &((TypeInfo){.size=sizeof(void*), .align=__alignof__(void*), \ + .tag=PointerInfo, .PointerInfo={.sigil=sigil_expr, .pointed=pointed_info, .is_optional=opt}}) #define $ArrayInfo(item_info) &((TypeInfo){.size=sizeof(array_t), .align=__alignof__(array_t), \ .tag=ArrayInfo, .ArrayInfo.item=item_info}) #define $TableInfo(key_expr, value_expr) &((TypeInfo){.size=sizeof(table_t), .align=__alignof__(table_t), \ |
