Fix up some builtins
This commit is contained in:
parent
5c49314ed4
commit
dbd7502a1d
2
Makefile
2
Makefile
@ -41,7 +41,7 @@ tags:
|
|||||||
ctags *.[ch] **/*.[ch]
|
ctags *.[ch] **/*.[ch]
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f nextlang *.o
|
rm -f nextlang *.o builtins/*.o
|
||||||
|
|
||||||
%.1: %.1.md
|
%.1: %.1.md
|
||||||
pandoc --lua-filter=.pandoc/bold-code.lua -s $< -t man -o $@
|
pandoc --lua-filter=.pandoc/bold-code.lua -s $< -t man -o $@
|
||||||
|
@ -18,6 +18,7 @@ extern const void *SSS_HASH_VECTOR;
|
|||||||
static CORD Bool_cord(const bool *b, bool colorize, const TypeInfo *type)
|
static CORD Bool_cord(const bool *b, bool colorize, const TypeInfo *type)
|
||||||
{
|
{
|
||||||
(void)type;
|
(void)type;
|
||||||
|
if (!b) return "Bool";
|
||||||
if (colorize)
|
if (colorize)
|
||||||
return *b ? "\x1b[35myes\x1b[m" : "\x1b[35mno\x1b[m";
|
return *b ? "\x1b[35myes\x1b[m" : "\x1b[35mno\x1b[m";
|
||||||
else
|
else
|
||||||
@ -28,7 +29,6 @@ public struct {
|
|||||||
TypeInfo type;
|
TypeInfo type;
|
||||||
} Bool_type = {
|
} Bool_type = {
|
||||||
.type={
|
.type={
|
||||||
.name="Bool",
|
|
||||||
.size=sizeof(bool),
|
.size=sizeof(bool),
|
||||||
.align=alignof(bool),
|
.align=alignof(bool),
|
||||||
.tag=CustomInfo,
|
.tag=CustomInfo,
|
||||||
|
@ -32,18 +32,12 @@ static bool Num__equal(const double *x, const double *y, const TypeInfo *type) {
|
|||||||
return *x == *y;
|
return *x == *y;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Str_t Num__format(double f, int64_t precision) {
|
public CORD Num__format(double f, int64_t precision) {
|
||||||
int len = snprintf(NULL, 0, "%.*f", (int)precision, f);
|
return CORD_asprintf("%.*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 Str_t Num__scientific(double f, int64_t precision) {
|
public CORD Num__scientific(double f, int64_t precision) {
|
||||||
int len = snprintf(NULL, 0, "%.*e", (int)precision, f);
|
return CORD_asprintf("%.*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 double Num__mod(double num, double modulus) {
|
public double Num__mod(double num, double modulus) {
|
||||||
@ -76,11 +70,10 @@ public struct {
|
|||||||
// Binary functions:
|
// Binary functions:
|
||||||
double_binary_fn_t atan2, copysign, dist, hypot, maxmag, minmag, mod, nextafter, pow, remainder;
|
double_binary_fn_t atan2, copysign, dist, hypot, maxmag, minmag, mod, nextafter, pow, remainder;
|
||||||
// Odds and ends:
|
// Odds and ends:
|
||||||
Str_t (*format)(double f, int64_t precision);
|
CORD (*format)(double f, int64_t precision);
|
||||||
Str_t (*scientific)(double f, int64_t precision);
|
CORD (*scientific)(double f, int64_t precision);
|
||||||
} Num_type = {
|
} Num_type = {
|
||||||
.type=(TypeInfo){
|
.type=(TypeInfo){
|
||||||
.name="Num",
|
|
||||||
.size=sizeof(double),
|
.size=sizeof(double),
|
||||||
.align=alignof(double),
|
.align=alignof(double),
|
||||||
.tag=CustomInfo,
|
.tag=CustomInfo,
|
||||||
@ -127,18 +120,12 @@ static bool Num32__equal(const float *x, const float *y, const TypeInfo *type) {
|
|||||||
return *x == *y;
|
return *x == *y;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Str_t Num32__format(float f, int64_t precision) {
|
public CORD Num32__format(float f, int64_t precision) {
|
||||||
int len = snprintf(NULL, 0, "%.*f", (int)precision, f);
|
return CORD_asprintf("%.*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 Str_t Num32__scientific(float f, int64_t precision) {
|
public CORD Num32__scientific(float f, int64_t precision) {
|
||||||
int len = snprintf(NULL, 0, "%.*e", (int)precision, f);
|
return CORD_asprintf("%.*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 float Num32__mod(float num, float modulus) {
|
public float Num32__mod(float num, float modulus) {
|
||||||
@ -175,11 +162,10 @@ public struct {
|
|||||||
// Binary functions:
|
// Binary functions:
|
||||||
float_binary_fn_t atan2, copysign, dist, hypot, maxmag, minmag, mod, nextafter, pow, remainder;
|
float_binary_fn_t atan2, copysign, dist, hypot, maxmag, minmag, mod, nextafter, pow, remainder;
|
||||||
// Odds and ends:
|
// Odds and ends:
|
||||||
Str_t (*format)(float f, int64_t precision);
|
CORD (*format)(float f, int64_t precision);
|
||||||
Str_t (*scientific)(float f, int64_t precision);
|
CORD (*scientific)(float f, int64_t precision);
|
||||||
} Num32_type = {
|
} Num32_type = {
|
||||||
.type=(TypeInfo){
|
.type=(TypeInfo){
|
||||||
.name="Num32",
|
|
||||||
.size=sizeof(float),
|
.size=sizeof(float),
|
||||||
.align=alignof(float),
|
.align=alignof(float),
|
||||||
.tag=CustomInfo,
|
.tag=CustomInfo,
|
||||||
|
@ -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 TableInfo: return Table_cord(obj, colorize, type);
|
||||||
case CustomInfo:
|
case CustomInfo:
|
||||||
if (!type->CustomInfo.cord)
|
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);
|
return type->CustomInfo.cord(obj, colorize, type);
|
||||||
default: errx(1, "Invalid type tag: %d", type->tag);
|
default: errx(1, "Invalid type tag: %d", type->tag);
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ extern const void *SSS_HASH_VECTOR;
|
|||||||
#define DEFINE_INT_TYPE(c_type, KindOfInt, fmt, abs_fn, min_val, max_val)\
|
#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 ## __cord(const c_type *i, bool colorize, const TypeInfo *type) { \
|
||||||
(void)type; \
|
(void)type; \
|
||||||
|
if (!i) return #KindOfInt; \
|
||||||
CORD c; \
|
CORD c; \
|
||||||
if (colorize) CORD_sprintf(&c, "\x1b[35m%"fmt"\x1b[33;2m\x1b[m", *i); \
|
if (colorize) CORD_sprintf(&c, "\x1b[35m%"fmt"\x1b[33;2m\x1b[m", *i); \
|
||||||
else CORD_sprintf(&c, "%"fmt, *i); \
|
else CORD_sprintf(&c, "%"fmt, *i); \
|
||||||
@ -27,25 +28,16 @@ extern const void *SSS_HASH_VECTOR;
|
|||||||
(void)type; \
|
(void)type; \
|
||||||
return (*x > *y) - (*x < *y); \
|
return (*x > *y) - (*x < *y); \
|
||||||
} \
|
} \
|
||||||
public Str_t KindOfInt ## __format(c_type i, int64_t digits) { \
|
public CORD KindOfInt ## __format(c_type i, int64_t digits) { \
|
||||||
int len = snprintf(NULL, 0, "%0*" fmt, (int)digits, i); \
|
return CORD_asprintf("%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 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"); \
|
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); \
|
return CORD_asprintf(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}; \
|
|
||||||
} \
|
} \
|
||||||
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"; \
|
const char *octal_fmt = prefix ? "0o%0.*lo" : "%0.*lo"; \
|
||||||
int len = snprintf(NULL, 0, octal_fmt, (int)digits, (uint64_t)i); \
|
return CORD_asprintf(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}; \
|
|
||||||
} \
|
} \
|
||||||
public c_type KindOfInt ## __random(int64_t min, int64_t max) { \
|
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); \
|
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; \
|
TypeInfo type; \
|
||||||
c_type min, max; \
|
c_type min, max; \
|
||||||
c_type (*abs)(c_type i); \
|
c_type (*abs)(c_type i); \
|
||||||
Str_t (*format)(c_type i, int64_t digits); \
|
CORD (*format)(c_type i, int64_t digits); \
|
||||||
Str_t (*hex)(c_type i, int64_t digits, bool uppercase, bool prefix); \
|
CORD (*hex)(c_type i, int64_t digits, bool uppercase, bool prefix); \
|
||||||
Str_t (*octal)(c_type i, int64_t digits, bool prefix); \
|
CORD (*octal)(c_type i, int64_t digits, bool prefix); \
|
||||||
c_type (*random)(int64_t min, int64_t max); \
|
c_type (*random)(int64_t min, int64_t max); \
|
||||||
} KindOfInt##_type = { \
|
} KindOfInt##_type = { \
|
||||||
.type={ \
|
.type={ \
|
||||||
.name=#KindOfInt, \
|
|
||||||
.size=sizeof(c_type), \
|
.size=sizeof(c_type), \
|
||||||
.align=alignof(c_type), \
|
.align=alignof(c_type), \
|
||||||
.tag=CustomInfo, \
|
.tag=CustomInfo, \
|
||||||
|
@ -17,13 +17,13 @@ extern const void *SSS_HASH_VECTOR;
|
|||||||
|
|
||||||
public CORD Memory__cord(const void *p, bool colorize, const TypeInfo *type) {
|
public CORD Memory__cord(const void *p, bool colorize, const TypeInfo *type) {
|
||||||
(void)type;
|
(void)type;
|
||||||
|
if (!p) return "Memory";
|
||||||
CORD cord;
|
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;
|
return cord;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TypeInfo Memory_type = {
|
public TypeInfo Memory_type = {
|
||||||
.name="Memory",
|
|
||||||
.size=0,
|
.size=0,
|
||||||
.align=0,
|
.align=0,
|
||||||
.tag=CustomInfo,
|
.tag=CustomInfo,
|
||||||
|
@ -23,8 +23,10 @@ typedef struct recursion_s {
|
|||||||
public CORD Pointer__cord(const void *x, bool colorize, const TypeInfo *type) {
|
public CORD Pointer__cord(const void *x, bool colorize, const TypeInfo *type) {
|
||||||
auto ptr_info = type->PointerInfo;
|
auto ptr_info = type->PointerInfo;
|
||||||
const void *ptr = *(const void**)x;
|
const void *ptr = *(const void**)x;
|
||||||
if (!ptr)
|
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);
|
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
|
// Check for recursive references, so if `x.foo = x`, then it prints as
|
||||||
// `@Foo{foo=@..1}` instead of overflowing the stack:
|
// `@Foo{foo=@..1}` instead of overflowing the stack:
|
||||||
|
@ -252,7 +252,6 @@ public CORD Str__join(CORD glue, Str_Array_t pieces)
|
|||||||
|
|
||||||
public Str_namespace_t Str_type = {
|
public Str_namespace_t Str_type = {
|
||||||
.type={
|
.type={
|
||||||
.name="Str",
|
|
||||||
.size=sizeof(CORD),
|
.size=sizeof(CORD),
|
||||||
.align=alignof(CORD),
|
.align=alignof(CORD),
|
||||||
.tag=CustomInfo,
|
.tag=CustomInfo,
|
||||||
|
@ -46,7 +46,6 @@
|
|||||||
extern const void *SSS_HASH_VECTOR;
|
extern const void *SSS_HASH_VECTOR;
|
||||||
|
|
||||||
TypeInfo MemoryPointer_typeinfo = {
|
TypeInfo MemoryPointer_typeinfo = {
|
||||||
.name="@Memory",
|
|
||||||
.size=sizeof(void*),
|
.size=sizeof(void*),
|
||||||
.align=alignof(void*),
|
.align=alignof(void*),
|
||||||
.tag=PointerInfo,
|
.tag=PointerInfo,
|
||||||
@ -57,7 +56,6 @@ TypeInfo MemoryPointer_typeinfo = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
TypeInfo StrToVoidStarTable_type = {
|
TypeInfo StrToVoidStarTable_type = {
|
||||||
.name="{Str=>@Memory}",
|
|
||||||
.size=sizeof(table_t),
|
.size=sizeof(table_t),
|
||||||
.align=alignof(table_t),
|
.align=alignof(table_t),
|
||||||
.tag=TableInfo,
|
.tag=TableInfo,
|
||||||
|
@ -19,9 +19,9 @@ public CORD Type__cord(void *type_namespace, bool colorize, const TypeInfo *type
|
|||||||
{
|
{
|
||||||
(void)type_namespace;
|
(void)type_namespace;
|
||||||
if (!colorize)
|
if (!colorize)
|
||||||
return CORD_from_char_star(type->name);
|
return type->TypeInfoInfo.type_str;
|
||||||
CORD c;
|
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;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,7 +29,6 @@ public struct {
|
|||||||
TypeInfo type;
|
TypeInfo type;
|
||||||
} TypeInfo_type = {
|
} TypeInfo_type = {
|
||||||
.type={
|
.type={
|
||||||
.name="TypeInfo",
|
|
||||||
.size=sizeof(TypeInfo),
|
.size=sizeof(TypeInfo),
|
||||||
.align=alignof(TypeInfo),
|
.align=alignof(TypeInfo),
|
||||||
.tag=CustomInfo,
|
.tag=CustomInfo,
|
||||||
@ -39,15 +38,15 @@ public struct {
|
|||||||
|
|
||||||
public struct {
|
public struct {
|
||||||
TypeInfo type;
|
TypeInfo type;
|
||||||
} Void_type = {.type={.name="Void", .size=0, .align=0}};
|
} Void_type = {.type={.size=0, .align=0}};
|
||||||
public struct {
|
public struct {
|
||||||
TypeInfo type;
|
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)
|
public CORD Func__cord(const void *fn, bool colorize, const TypeInfo *type)
|
||||||
{
|
{
|
||||||
(void)fn;
|
(void)fn;
|
||||||
CORD c = type->name;
|
CORD c = type->TypeInfoInfo.type_str;
|
||||||
if (colorize)
|
if (colorize)
|
||||||
CORD_sprintf(&c, "\x1b[32;1m%r\x1b[m", c);
|
CORD_sprintf(&c, "\x1b[32;1m%r\x1b[m", c);
|
||||||
return c;
|
return c;
|
||||||
|
@ -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 CORD (*cord_fn_t)(const void*, bool, const struct TypeInfo*);
|
||||||
|
|
||||||
typedef struct TypeInfo {
|
typedef struct TypeInfo {
|
||||||
const char *name;
|
|
||||||
int64_t size, align;
|
int64_t size, align;
|
||||||
struct { // Anonymous tagged union for convenience
|
struct { // Anonymous tagged union for convenience
|
||||||
enum { CustomInfo, PointerInfo, ArrayInfo, TableInfo } tag;
|
enum { CustomInfo, PointerInfo, ArrayInfo, TableInfo, TypeInfoInfo, } tag;
|
||||||
union {
|
union {
|
||||||
struct {
|
struct {
|
||||||
equal_fn_t equal;
|
equal_fn_t equal;
|
||||||
@ -35,6 +34,9 @@ typedef struct TypeInfo {
|
|||||||
struct TypeInfo *key, *value;
|
struct TypeInfo *key, *value;
|
||||||
int64_t entry_size, value_offset;
|
int64_t entry_size, value_offset;
|
||||||
} TableInfo;
|
} TableInfo;
|
||||||
|
struct {
|
||||||
|
const char *type_str;
|
||||||
|
} TypeInfoInfo;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
} TypeInfo;
|
} TypeInfo;
|
||||||
|
13
compile.c
13
compile.c
@ -414,4 +414,17 @@ CORD compile(env_t *env, ast_t *ast)
|
|||||||
return NULL;
|
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
|
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
|
||||||
|
Loading…
Reference in New Issue
Block a user