diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-11-29 12:55:14 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-11-29 12:55:14 -0500 |
| commit | 4b5e4cd1f21582f5e5fa682ab4e4bff252963468 (patch) | |
| tree | e8fecb01f444c1d392c09255adba5cf6b312b326 /stdlib/types.h | |
| parent | 0b0e0a0a1d41e9574de8dc17c688a4894c5e7f92 (diff) | |
Change how types handle metamethods
Diffstat (limited to 'stdlib/types.h')
| -rw-r--r-- | stdlib/types.h | 40 |
1 files changed, 11 insertions, 29 deletions
diff --git a/stdlib/types.h b/stdlib/types.h index a8a9b4e6..652a2e70 100644 --- a/stdlib/types.h +++ b/stdlib/types.h @@ -9,10 +9,13 @@ typedef struct TypeInfo_s TypeInfo_t; -typedef uint64_t (*hash_fn_t)(const void*, const TypeInfo_t*); -typedef int32_t (*compare_fn_t)(const void*, const void*, const TypeInfo_t*); -typedef bool (*equal_fn_t)(const void*, const void*, const TypeInfo_t*); -typedef Text_t (*text_fn_t)(const void*, bool, const TypeInfo_t*); +typedef struct { + uint64_t (*hash)(const void*, const TypeInfo_t*); + int32_t (*compare)(const void*, const void*, const TypeInfo_t*); + bool (*equal)(const void*, const void*, const TypeInfo_t*); + Text_t (*as_text)(const void*, bool, const TypeInfo_t*); + bool (*is_none)(const void*, const TypeInfo_t*); +} metamethods_t; typedef struct { const char *name; @@ -21,16 +24,12 @@ typedef struct { struct TypeInfo_s { int64_t size, align; + metamethods_t metamethods; struct { // Anonymous tagged union for convenience - enum { Invalid, CustomInfo, StructInfo, EnumInfo, PointerInfo, TextInfo, ArrayInfo, ChannelInfo, TableInfo, FunctionInfo, - OptionalInfo, TypeInfoInfo, OpaqueInfo, CStringInfo } tag; + enum { OpaqueInfo, StructInfo, EnumInfo, PointerInfo, TextInfo, ArrayInfo, ChannelInfo, TableInfo, FunctionInfo, + OptionalInfo, TypeInfoInfo } tag; union { - struct { - equal_fn_t equal; - compare_fn_t compare; - hash_fn_t hash; - text_fn_t as_text; - } CustomInfo; + struct {} OpaqueInfo; struct { const char *sigil; const TypeInfo_t *pointed; @@ -53,7 +52,6 @@ struct TypeInfo_s { struct { const TypeInfo_t *type; } OptionalInfo; - struct {} OpaqueInfo; struct { const char *name; int num_tags; @@ -69,24 +67,8 @@ struct TypeInfo_s { }; }; -#define Pointer$info(sigil_expr, pointed_info) &((TypeInfo_t){.size=sizeof(void*), .align=__alignof__(void*), \ - .tag=PointerInfo, .PointerInfo={.sigil=sigil_expr, .pointed=pointed_info}}) -#define Array$info(item_info) &((TypeInfo_t){.size=sizeof(Array_t), .align=__alignof__(Array_t), \ - .tag=ArrayInfo, .ArrayInfo.item=item_info}) -#define Set$info(item_info) &((TypeInfo_t){.size=sizeof(Table_t), .align=__alignof__(Table_t), \ - .tag=TableInfo, .TableInfo.key=item_info, .TableInfo.value=&Void$info}) -#define Channel$info(item_info) &((TypeInfo_t){.size=sizeof(Channel_t), .align=__alignof__(Channel_t), \ - .tag=ChannelInfo, .ChannelInfo.item=item_info}) -#define Table$info(key_expr, value_expr) &((TypeInfo_t){.size=sizeof(Table_t), .align=__alignof__(Table_t), \ - .tag=TableInfo, .TableInfo.key=key_expr, .TableInfo.value=value_expr}) -#define Function$info(typestr) &((TypeInfo_t){.size=sizeof(void*), .align=__alignof__(void*), \ - .tag=FunctionInfo, .FunctionInfo.type_str=typestr}) -#define Closure$info(typestr) &((TypeInfo_t){.size=sizeof(void*[2]), .align=__alignof__(void*), \ - .tag=FunctionInfo, .FunctionInfo.type_str=typestr}) #define Type$info(typestr) &((TypeInfo_t){.size=sizeof(TypeInfo_t), .align=__alignof__(TypeInfo_t), \ .tag=TypeInfoInfo, .TypeInfoInfo.type_str=typestr}) -#define Optional$info(_size, _align, t) &((TypeInfo_t){.size=_size, .align=_align, \ - .tag=OptionalInfo, .OptionalInfo.type=t}) extern const TypeInfo_t Void$info; extern const TypeInfo_t Abort$info; |
