tomo/stdlib/optionals.c

95 lines
3.2 KiB
C
Raw Normal View History

2024-09-10 22:31:31 -07:00
// Optional types
#include <pthread.h>
#include "bools.h"
2024-09-15 12:35:51 -07:00
#include "bytes.h"
2024-09-10 22:31:31 -07:00
#include "datatypes.h"
2024-09-11 08:08:15 -07:00
#include "integers.h"
2024-09-13 16:59:28 -07:00
#include "metamethods.h"
2024-11-17 11:49:03 -08:00
#include "moments.h"
#include "nums.h"
#include "patterns.h"
2024-09-10 22:31:31 -07:00
#include "text.h"
2024-09-29 17:06:09 -07:00
#include "threads.h"
2024-09-10 22:31:31 -07:00
#include "util.h"
2024-11-29 15:09:12 -08:00
public PUREFUNC bool is_none(const void *obj, const TypeInfo_t *non_optional_type)
2024-09-10 22:31:31 -07:00
{
2024-11-29 09:55:14 -08:00
if (non_optional_type->metamethods.is_none)
return non_optional_type->metamethods.is_none(obj, non_optional_type);
2024-09-10 22:31:31 -07:00
2024-11-29 09:55:14 -08:00
return *(bool*)(obj + non_optional_type->size);
}
PUREFUNC public uint64_t Optional$hash(const void *obj, const TypeInfo_t *type)
{
2024-11-29 15:09:12 -08:00
return is_none(obj, type->OptionalInfo.type) ? 0 : generic_hash(obj, type->OptionalInfo.type);
2024-11-29 09:55:14 -08:00
}
PUREFUNC public int32_t Optional$compare(const void *x, const void *y, const TypeInfo_t *type)
{
if (x == y) return 0;
2024-11-29 15:09:12 -08:00
bool x_is_null = is_none(x, type->OptionalInfo.type);
bool y_is_null = is_none(y, type->OptionalInfo.type);
2024-11-29 09:55:14 -08:00
if (x_is_null && y_is_null) return 0;
else if (x_is_null != y_is_null) return (int32_t)y_is_null - (int32_t)x_is_null;
else return generic_compare(x, y, type->OptionalInfo.type);
}
PUREFUNC public bool Optional$equal(const void *x, const void *y, const TypeInfo_t *type)
{
if (x == y) return true;
2024-11-29 15:09:12 -08:00
bool x_is_null = is_none(x, type->OptionalInfo.type);
bool y_is_null = is_none(y, type->OptionalInfo.type);
2024-11-29 09:55:14 -08:00
if (x_is_null && y_is_null) return true;
else if (x_is_null != y_is_null) return false;
else return generic_equal(x, y, type->OptionalInfo.type);
2024-09-10 22:31:31 -07:00
}
public Text_t Optional$as_text(const void *obj, bool colorize, const TypeInfo_t *type)
2024-09-10 22:31:31 -07:00
{
if (!obj)
return Text$concat(generic_as_text(obj, colorize, type->OptionalInfo.type), Text("?"));
2024-11-29 15:09:12 -08:00
if (is_none(obj, type->OptionalInfo.type))
2024-12-07 13:04:25 -08:00
return colorize ? Text("\x1b[31mnone\x1b[m") : Text("none");
return generic_as_text(obj, colorize, type->OptionalInfo.type);
2024-09-10 22:31:31 -07:00
}
2024-11-29 15:09:12 -08:00
public void Optional$serialize(const void *obj, FILE *out, Table_t *pointers, const TypeInfo_t *type)
{
bool has_value = !is_none(obj, type->OptionalInfo.type);
fputc((int)has_value, out);
if (has_value)
_serialize(obj, out, pointers, type->OptionalInfo.type);
}
public void Optional$deserialize(FILE *in, void *outval, Array_t *pointers, const TypeInfo_t *type)
{
bool has_value = (bool)fgetc(in);
const TypeInfo_t *nonnull = type->OptionalInfo.type;
if (has_value) {
memset(outval, 0, (size_t)type->size);
_deserialize(in, outval, pointers, nonnull);
} else {
if (nonnull->tag == TextInfo)
*(Text_t*)outval = NONE_TEXT;
2024-11-29 15:09:12 -08:00
else if (nonnull->tag == ArrayInfo)
*(Array_t*)outval = (Array_t){.length=-1};
else if (nonnull->tag == TableInfo)
*(Table_t*)outval = (Table_t){.entries={.length=-1}};
else if (nonnull == &Num$info)
*(double*)outval = NAN;
else if (nonnull == &Num32$info)
*(float*)outval = NAN;
else if (nonnull->tag == StructInfo || (nonnull->tag == OpaqueInfo && type->size > nonnull->size))
memset(outval + type->size, -1, (size_t)(type->size - nonnull->size));
else
memset(outval, 0, (size_t)type->size);
}
}
2024-09-10 22:31:31 -07:00
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1