Fix up some builtins

This commit is contained in:
Bruce Hill 2024-02-17 17:29:56 -05:00
parent 5c49314ed4
commit dbd7502a1d
12 changed files with 53 additions and 63 deletions

View File

@ -41,7 +41,7 @@ tags:
ctags *.[ch] **/*.[ch]
clean:
rm -f nextlang *.o
rm -f nextlang *.o builtins/*.o
%.1: %.1.md
pandoc --lua-filter=.pandoc/bold-code.lua -s $< -t man -o $@

View File

@ -18,6 +18,7 @@ extern const void *SSS_HASH_VECTOR;
static CORD Bool_cord(const bool *b, bool colorize, const TypeInfo *type)
{
(void)type;
if (!b) return "Bool";
if (colorize)
return *b ? "\x1b[35myes\x1b[m" : "\x1b[35mno\x1b[m";
else
@ -28,7 +29,6 @@ public struct {
TypeInfo type;
} Bool_type = {
.type={
.name="Bool",
.size=sizeof(bool),
.align=alignof(bool),
.tag=CustomInfo,

View File

@ -32,18 +32,12 @@ static bool Num__equal(const double *x, const double *y, const TypeInfo *type) {
return *x == *y;
}
public Str_t Num__format(double f, int64_t precision) {
int len = snprintf(NULL, 0, "%.*f", (int)precision, f);
char *str = GC_MALLOC_ATOMIC(len + 1);
snprintf(str, len+1, "%.*f", (int)precision, f);
return (Str_t){.data=str, .length=len, .stride=1};
public CORD Num__format(double f, int64_t precision) {
return CORD_asprintf("%.*f", (int)precision, f);
}
public Str_t Num__scientific(double f, int64_t precision) {
int len = snprintf(NULL, 0, "%.*e", (int)precision, f);
char *str = GC_MALLOC_ATOMIC(len + 1);
snprintf(str, len+1, "%.*e", (int)precision, f);
return (Str_t){.data=str, .length=len, .stride=1};
public CORD Num__scientific(double f, int64_t precision) {
return CORD_asprintf("%.*e", (int)precision, f);
}
public double Num__mod(double num, double modulus) {
@ -76,11 +70,10 @@ public struct {
// Binary functions:
double_binary_fn_t atan2, copysign, dist, hypot, maxmag, minmag, mod, nextafter, pow, remainder;
// Odds and ends:
Str_t (*format)(double f, int64_t precision);
Str_t (*scientific)(double f, int64_t precision);
CORD (*format)(double f, int64_t precision);
CORD (*scientific)(double f, int64_t precision);
} Num_type = {
.type=(TypeInfo){
.name="Num",
.size=sizeof(double),
.align=alignof(double),
.tag=CustomInfo,
@ -127,18 +120,12 @@ static bool Num32__equal(const float *x, const float *y, const TypeInfo *type) {
return *x == *y;
}
public Str_t Num32__format(float f, int64_t precision) {
int len = snprintf(NULL, 0, "%.*f", (int)precision, f);
char *str = GC_MALLOC_ATOMIC(len + 1);
snprintf(str, len+1, "%.*f", (int)precision, f);
return (Str_t){.data=str, .length=len, .stride=1};
public CORD Num32__format(float f, int64_t precision) {
return CORD_asprintf("%.*f", (int)precision, f);
}
public Str_t Num32__scientific(float f, int64_t precision) {
int len = snprintf(NULL, 0, "%.*e", (int)precision, f);
char *str = GC_MALLOC_ATOMIC(len + 1);
snprintf(str, len+1, "%.*e", (int)precision, f);
return (Str_t){.data=str, .length=len, .stride=1};
public CORD Num32__scientific(float f, int64_t precision) {
return CORD_asprintf("%.*e", (int)precision, f);
}
public float Num32__mod(float num, float modulus) {
@ -175,11 +162,10 @@ public struct {
// Binary functions:
float_binary_fn_t atan2, copysign, dist, hypot, maxmag, minmag, mod, nextafter, pow, remainder;
// Odds and ends:
Str_t (*format)(float f, int64_t precision);
Str_t (*scientific)(float f, int64_t precision);
CORD (*format)(float f, int64_t precision);
CORD (*scientific)(float f, int64_t precision);
} Num32_type = {
.type=(TypeInfo){
.name="Num32",
.size=sizeof(float),
.align=alignof(float),
.tag=CustomInfo,

View File

@ -86,7 +86,7 @@ public CORD generic_cord(const void *obj, bool colorize, const TypeInfo *type)
case TableInfo: return Table_cord(obj, colorize, type);
case CustomInfo:
if (!type->CustomInfo.cord)
builtin_fail("No cord function provided for type: %s!\n", type->name);
builtin_fail("No cord function provided for type!\n");
return type->CustomInfo.cord(obj, colorize, type);
default: errx(1, "Invalid type tag: %d", type->tag);
}

View File

@ -18,6 +18,7 @@ extern const void *SSS_HASH_VECTOR;
#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) { \
(void)type; \
if (!i) return #KindOfInt; \
CORD c; \
if (colorize) CORD_sprintf(&c, "\x1b[35m%"fmt"\x1b[33;2m\x1b[m", *i); \
else CORD_sprintf(&c, "%"fmt, *i); \
@ -27,25 +28,16 @@ extern const void *SSS_HASH_VECTOR;
(void)type; \
return (*x > *y) - (*x < *y); \
} \
public Str_t KindOfInt ## __format(c_type i, int64_t digits) { \
int len = snprintf(NULL, 0, "%0*" fmt, (int)digits, i); \
char *str = GC_MALLOC_ATOMIC(len + 1); \
snprintf(str, len+1, "%0*" fmt, (int)digits, i); \
return (Str_t){.data=str, .length=len, .stride=1}; \
public CORD KindOfInt ## __format(c_type i, int64_t digits) { \
return CORD_asprintf("%0*" fmt, (int)digits, i); \
} \
public Str_t KindOfInt ## __hex(c_type i, int64_t digits, bool uppercase, bool prefix) { \
public CORD KindOfInt ## __hex(c_type i, int64_t digits, bool uppercase, bool prefix) { \
const char *hex_fmt = uppercase ? (prefix ? "0x%0.*lX" : "%0.*lX") : (prefix ? "0x%0.*lx" : "%0.*lx"); \
int len = snprintf(NULL, 0, hex_fmt, (int)digits, (uint64_t)i); \
char *str = GC_MALLOC_ATOMIC(len + 1); \
snprintf(str, len+1, hex_fmt, (int)digits, (uint64_t)i); \
return (Str_t){.data=str, .length=len, .stride=1}; \
return CORD_asprintf(hex_fmt, (int)digits, (uint64_t)i); \
} \
public Str_t KindOfInt ## __octal(c_type i, int64_t digits, bool prefix) { \
public CORD KindOfInt ## __octal(c_type i, int64_t digits, bool prefix) { \
const char *octal_fmt = prefix ? "0o%0.*lo" : "%0.*lo"; \
int len = snprintf(NULL, 0, octal_fmt, (int)digits, (uint64_t)i); \
char *str = GC_MALLOC_ATOMIC(len + 1); \
snprintf(str, len+1, octal_fmt, (int)digits, (uint64_t)i); \
return (Str_t){.data=str, .length=len, .stride=1}; \
return CORD_asprintf(octal_fmt, (int)digits, (uint64_t)i); \
} \
public c_type KindOfInt ## __random(int64_t min, int64_t max) { \
if (min > max) builtin_fail("Random min (%ld) is larger than max (%ld)", min, max); \
@ -60,13 +52,12 @@ extern const void *SSS_HASH_VECTOR;
TypeInfo type; \
c_type min, max; \
c_type (*abs)(c_type i); \
Str_t (*format)(c_type i, int64_t digits); \
Str_t (*hex)(c_type i, int64_t digits, bool uppercase, bool prefix); \
Str_t (*octal)(c_type i, int64_t digits, bool prefix); \
CORD (*format)(c_type i, int64_t digits); \
CORD (*hex)(c_type i, int64_t digits, bool uppercase, bool prefix); \
CORD (*octal)(c_type i, int64_t digits, bool prefix); \
c_type (*random)(int64_t min, int64_t max); \
} KindOfInt##_type = { \
.type={ \
.name=#KindOfInt, \
.size=sizeof(c_type), \
.align=alignof(c_type), \
.tag=CustomInfo, \

View File

@ -17,13 +17,13 @@ extern const void *SSS_HASH_VECTOR;
public CORD Memory__cord(const void *p, bool colorize, const TypeInfo *type) {
(void)type;
if (!p) return "Memory";
CORD cord;
CORD_sprintf(&cord, colorize ? "\x1b[0;34;1mMemory<%p>\x1b[m" : "Memory<%p>", p);
CORD_sprintf(&cord, colorize ? "\x1b[0;34;1mMemory<%p>\x1b[m" : "Memory<%p>", *(const void**)p);
return cord;
}
public TypeInfo Memory_type = {
.name="Memory",
.size=0,
.align=0,
.tag=CustomInfo,

View File

@ -23,8 +23,10 @@ typedef struct recursion_s {
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)
return colorize ? CORD_asprintf("\x1b[34;1m!%s\x1b[m", ptr_info.pointed->name) : CORD_cat(ptr_info.sigil, ptr_info.pointed->name);
if (!ptr) {
CORD typename = generic_cord(NULL, false, ptr_info.pointed);
return colorize ? CORD_asprintf("\x1b[34;1m!%s\x1b[m", typename) : CORD_cat(ptr_info.sigil, typename);
}
// Check for recursive references, so if `x.foo = x`, then it prints as
// `@Foo{foo=@..1}` instead of overflowing the stack:

View File

@ -252,7 +252,6 @@ public CORD Str__join(CORD glue, Str_Array_t pieces)
public Str_namespace_t Str_type = {
.type={
.name="Str",
.size=sizeof(CORD),
.align=alignof(CORD),
.tag=CustomInfo,

View File

@ -46,7 +46,6 @@
extern const void *SSS_HASH_VECTOR;
TypeInfo MemoryPointer_typeinfo = {
.name="@Memory",
.size=sizeof(void*),
.align=alignof(void*),
.tag=PointerInfo,
@ -57,7 +56,6 @@ TypeInfo MemoryPointer_typeinfo = {
};
TypeInfo StrToVoidStarTable_type = {
.name="{Str=>@Memory}",
.size=sizeof(table_t),
.align=alignof(table_t),
.tag=TableInfo,

View File

@ -19,9 +19,9 @@ public CORD Type__cord(void *type_namespace, bool colorize, const TypeInfo *type
{
(void)type_namespace;
if (!colorize)
return CORD_from_char_star(type->name);
return type->TypeInfoInfo.type_str;
CORD c;
CORD_sprintf(&c, "\x1b[36;1m%s\x1b[m", type->name);
CORD_sprintf(&c, "\x1b[36;1m%s\x1b[m", type->TypeInfoInfo.type_str);
return c;
}
@ -29,7 +29,6 @@ public struct {
TypeInfo type;
} TypeInfo_type = {
.type={
.name="TypeInfo",
.size=sizeof(TypeInfo),
.align=alignof(TypeInfo),
.tag=CustomInfo,
@ -39,15 +38,15 @@ public struct {
public struct {
TypeInfo type;
} Void_type = {.type={.name="Void", .size=0, .align=0}};
} Void_type = {.type={.size=0, .align=0}};
public struct {
TypeInfo type;
} Abort_type = {.type={.name="Abort", .size=0, .align=0}};
} Abort_type = {.type={.size=0, .align=0}};
public CORD Func__cord(const void *fn, bool colorize, const TypeInfo *type)
{
(void)fn;
CORD c = type->name;
CORD c = type->TypeInfoInfo.type_str;
if (colorize)
CORD_sprintf(&c, "\x1b[32;1m%r\x1b[m", c);
return c;

View File

@ -13,10 +13,9 @@ typedef bool (*equal_fn_t)(const void*, const void*, const struct TypeInfo*);
typedef CORD (*cord_fn_t)(const void*, bool, const struct TypeInfo*);
typedef struct TypeInfo {
const char *name;
int64_t size, align;
struct { // Anonymous tagged union for convenience
enum { CustomInfo, PointerInfo, ArrayInfo, TableInfo } tag;
enum { CustomInfo, PointerInfo, ArrayInfo, TableInfo, TypeInfoInfo, } tag;
union {
struct {
equal_fn_t equal;
@ -35,6 +34,9 @@ typedef struct TypeInfo {
struct TypeInfo *key, *value;
int64_t entry_size, value_offset;
} TableInfo;
struct {
const char *type_str;
} TypeInfoInfo;
};
};
} TypeInfo;

View File

@ -414,4 +414,17 @@ CORD compile(env_t *env, ast_t *ast)
return NULL;
}
// CORD compile_type_info(env_t *env, type_t *t)
// {
// switch (t->tag) {
// case BoolType: return "&Bool$Info";
// case IntType: return CORD_asprintf("&Int%ld$Info", Match(t, IntType)->bits);
// case NumType: return CORD_asprintf("&Num%ld$Info", Match(t, NumType)->bits);
// case StringType: return CORD_all("&", Match(t, StringType)->dsl ? Match(t, StringType)->dsl : "Str", "$Info");
// case StructType: return CORD_all("&", Match(t, StructType)->name, "$Info");
// case EnumType: return CORD_all("&", Match(t, EnumType)->name, "$Info");
// case ArrayType: return CORD_all("&((TypeInfo){", Match(t, EnumType)->name, "$Info");
// }
// }
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0