breaking changes in type system
This commit is contained in:
parent
305854e721
commit
95fec8d166
@ -12,6 +12,7 @@ Array_define(i32)
|
||||
Array_define(u32)
|
||||
Array_define(i64)
|
||||
Array_define(u64)
|
||||
Array_define(Pointer)
|
||||
|
||||
Array_define(Unitype)
|
||||
|
||||
|
||||
@ -19,6 +19,7 @@ Array_declare(i32)
|
||||
Array_declare(u32)
|
||||
Array_declare(i64)
|
||||
Array_declare(u64)
|
||||
Array_declare(Pointer)
|
||||
|
||||
Array_declare(Unitype)
|
||||
|
||||
|
||||
@ -6,34 +6,32 @@ extern "C" {
|
||||
|
||||
#include "../base/base.h"
|
||||
|
||||
#define Array_declare(type)\
|
||||
typedef struct Array_##type{\
|
||||
type* values;\
|
||||
u32 length;\
|
||||
bool allocatedOnHeap;\
|
||||
} Array_##type;\
|
||||
#define Array_declare(type) \
|
||||
STRUCT(Array_##type, \
|
||||
type* values; \
|
||||
u32 length; \
|
||||
bool allocatedOnHeap; \
|
||||
) \
|
||||
\
|
||||
ktid_declare(Array_##type);\
|
||||
static inline Array_##type Array_##type##_allocValues(u32 length){ \
|
||||
return (Array_##type) { \
|
||||
.values=(type*)malloc(sizeof(type)*length), \
|
||||
.length=length, \
|
||||
.allocatedOnHeap=true \
|
||||
}; \
|
||||
} \
|
||||
\
|
||||
static inline Array_##type Array_##type##_allocValues(u32 length){\
|
||||
return (Array_##type) {\
|
||||
.values=(type*)malloc(sizeof(type)*length),\
|
||||
.length=length,\
|
||||
.allocatedOnHeap=true\
|
||||
};\
|
||||
}\
|
||||
static inline Array_##type Array_##type##_fromBuffer(type* buffer, u32 bufferLength, bool allocatedOnHeap){ \
|
||||
return (Array_##type) { \
|
||||
.values=buffer, \
|
||||
.length=bufferLength, \
|
||||
.allocatedOnHeap=allocatedOnHeap \
|
||||
}; \
|
||||
} \
|
||||
\
|
||||
static inline Array_##type Array_##type##_fromBuffer(type* buffer, u32 bufferLength, bool allocatedOnHeap){\
|
||||
return (Array_##type) {\
|
||||
.values=buffer,\
|
||||
.length=bufferLength,\
|
||||
.allocatedOnHeap=allocatedOnHeap\
|
||||
};\
|
||||
}\
|
||||
\
|
||||
static inline void Array_##type##_free(Array_##type* array){\
|
||||
if(array->allocatedOnHeap)\
|
||||
free(array->values);\
|
||||
static inline void Array_##type##_free(Array_##type* array){ \
|
||||
if(array->allocatedOnHeap) \
|
||||
free(array->values); \
|
||||
}
|
||||
|
||||
#if __cplusplus
|
||||
|
||||
@ -6,8 +6,8 @@ extern "C" {
|
||||
|
||||
#include "../base/base.h"
|
||||
|
||||
#define Array_define(type)\
|
||||
ktid_define(Array_##type);
|
||||
#define Array_define(type) \
|
||||
kt_define(Array_##type, (freeMembers_t)Array_##type##_free, NULL);
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
|
||||
@ -12,3 +12,4 @@ Autoarr_define(u32)
|
||||
Autoarr_define(i32)
|
||||
Autoarr_define(u64)
|
||||
Autoarr_define(i64)
|
||||
Autoarr_define(Pointer)
|
||||
|
||||
@ -20,7 +20,7 @@ Autoarr_declare(i32)
|
||||
Autoarr_declare(u32)
|
||||
Autoarr_declare(i64)
|
||||
Autoarr_declare(u64)
|
||||
|
||||
Autoarr_declare(Pointer)
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
|
||||
@ -13,19 +13,19 @@ Autoarr_declare(Unitype)
|
||||
void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr);
|
||||
void ____Autoarr_free_Unitype_(void* ar);
|
||||
|
||||
#define Autoarr_foreach(ar,elem,codeblock)({\
|
||||
if(ar->blocks_count>0) {\
|
||||
typeof(**ar->values) elem;\
|
||||
for(u32 blockI=0;blockI<ar->blocks_count-1;blockI++)\
|
||||
for(u32 elemI=0;elemI<ar->max_block_length;elemI++){\
|
||||
elem=ar->values[blockI][elemI];\
|
||||
(codeblock);\
|
||||
}\
|
||||
for(u32 elemI=0;elemI<ar->block_length;elemI++){\
|
||||
elem=ar->values[ar->blocks_count-1][elemI];\
|
||||
(codeblock);\
|
||||
}\
|
||||
}\
|
||||
#define Autoarr_foreach(ar,elem,codeblock)({ \
|
||||
if(ar->blocks_count>0) { \
|
||||
typeof(**ar->values) elem; \
|
||||
for(u32 blockI=0;blockI<ar->blocks_count-1;blockI++) \
|
||||
for(u32 elemI=0;elemI<ar->max_block_length;elemI++){ \
|
||||
elem=ar->values[blockI][elemI]; \
|
||||
(codeblock); \
|
||||
} \
|
||||
for(u32 elemI=0;elemI<ar->block_length;elemI++){ \
|
||||
elem=ar->values[ar->blocks_count-1][elemI]; \
|
||||
(codeblock); \
|
||||
} \
|
||||
} \
|
||||
})
|
||||
|
||||
#if __cplusplus
|
||||
|
||||
@ -6,64 +6,62 @@ extern "C" {
|
||||
|
||||
#include "../base/base.h"
|
||||
|
||||
#define Autoarr_declare(type)\
|
||||
#define Autoarr_declare(type) \
|
||||
\
|
||||
struct Autoarr_##type;\
|
||||
struct Autoarr_##type; \
|
||||
\
|
||||
typedef struct {\
|
||||
void (*add)(struct Autoarr_##type* ar, type element);\
|
||||
type (*get)(struct Autoarr_##type* ar, u32 index);\
|
||||
type* (*getptr)(struct Autoarr_##type* ar, u32 index);\
|
||||
void (*set)(struct Autoarr_##type* ar, u32 index, type element);\
|
||||
void (*freear)(struct Autoarr_##type* ar, bool freePtr);\
|
||||
type* (*toArray)(struct Autoarr_##type* ar);\
|
||||
} __functions_list_t_##type;\
|
||||
STRUCT(__functions_list_t_##type, \
|
||||
void (*add)(struct Autoarr_##type* ar, type element); \
|
||||
type (*get)(struct Autoarr_##type* ar, u32 index); \
|
||||
type* (*getptr)(struct Autoarr_##type* ar, u32 index); \
|
||||
void (*set)(struct Autoarr_##type* ar, u32 index, type element); \
|
||||
void (*freear)(struct Autoarr_##type* ar, bool freePtr); \
|
||||
type* (*toArray)(struct Autoarr_##type* ar); \
|
||||
) \
|
||||
\
|
||||
typedef struct Autoarr_##type{\
|
||||
u16 blocks_count;\
|
||||
u16 max_blocks_count;\
|
||||
u16 block_length;\
|
||||
u16 max_block_length;\
|
||||
type** values;\
|
||||
__functions_list_t_##type* functions;\
|
||||
} Autoarr_##type;\
|
||||
STRUCT(Autoarr_##type, \
|
||||
u16 blocks_count; \
|
||||
u16 max_blocks_count; \
|
||||
u16 block_length; \
|
||||
u16 max_block_length; \
|
||||
type** values; \
|
||||
__functions_list_t_##type* functions; \
|
||||
) \
|
||||
\
|
||||
ktid_declare(Autoarr_##type);\
|
||||
\
|
||||
Autoarr_##type* __Autoarr_create_##type(u16 max_blocks_count, u16 max_block_length);\
|
||||
void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr);\
|
||||
Autoarr_##type* __Autoarr_create_##type(u16 max_blocks_count, u16 max_block_length); \
|
||||
void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr); \
|
||||
void ____Autoarr_free_##type(void* ar);
|
||||
|
||||
#define Autoarr(type) Autoarr_##type
|
||||
|
||||
#define Autoarr_create(type, max_blocks_count, max_block_length)\
|
||||
#define Autoarr_create(type, max_blocks_count, max_block_length) \
|
||||
__Autoarr_create_##type(max_blocks_count, max_block_length)
|
||||
#define Autoarr_add(autoarr, element)\
|
||||
#define Autoarr_add(autoarr, element) \
|
||||
autoarr->functions->add(autoarr, element)
|
||||
#define Autoarr_get(autoarr, index)\
|
||||
#define Autoarr_get(autoarr, index) \
|
||||
autoarr->functions->get(autoarr,index)
|
||||
#define Autoarr_getptr(autoarr, index)\
|
||||
#define Autoarr_getptr(autoarr, index) \
|
||||
autoarr->functions->getptr(autoarr,index)
|
||||
#define Autoarr_set(autoarr, index, element)\
|
||||
#define Autoarr_set(autoarr, index, element) \
|
||||
autoarr->functions->set(autoarr, index, element)
|
||||
#define Autoarr_free(autoarr, freePtr)\
|
||||
#define Autoarr_free(autoarr, freePtr) \
|
||||
autoarr->functions->freear(autoarr, freePtr)
|
||||
#define Autoarr_toArray(autoarr)\
|
||||
#define Autoarr_toArray(autoarr) \
|
||||
autoarr->functions->toArray(autoarr)
|
||||
|
||||
#define Autoarr_length(autoarr) \
|
||||
(u32)(!autoarr->blocks_count ? 0 : \
|
||||
autoarr->max_block_length*(autoarr->blocks_count-1)+autoarr->block_length)
|
||||
#define Autoarr_max_length(autoarr)\
|
||||
#define Autoarr_max_length(autoarr) \
|
||||
(u32)(autoarr->max_block_length*autoarr->max_blocks_count)
|
||||
|
||||
#define Autoarr_pop(AR){\
|
||||
if(AR->block_length==1){\
|
||||
AR->blocks_count--;\
|
||||
AR->block_length=AR->max_block_length;\
|
||||
free(AR->values[AR->blocks_count]);\
|
||||
}\
|
||||
else AR->block_length--;\
|
||||
#define Autoarr_pop(AR){ \
|
||||
if(AR->block_length==1){ \
|
||||
AR->blocks_count--; \
|
||||
AR->block_length=AR->max_block_length; \
|
||||
free(AR->values[AR->blocks_count]); \
|
||||
} \
|
||||
else AR->block_length--; \
|
||||
}
|
||||
|
||||
#if __cplusplus
|
||||
|
||||
@ -6,79 +6,79 @@ extern "C" {
|
||||
|
||||
#include "../base/base.h"
|
||||
|
||||
#define Autoarr_define(type)\
|
||||
#define Autoarr_define(type) \
|
||||
\
|
||||
ktid_define(Autoarr_##type);\
|
||||
kt_define(Autoarr_##type, ____Autoarr_free_##type, NULL); \
|
||||
\
|
||||
void __Autoarr_add_##type(Autoarr_##type* ar, type element){\
|
||||
if(!ar->values){\
|
||||
ar->values=malloc(ar->max_blocks_count*sizeof(type*));\
|
||||
goto create_block;\
|
||||
}\
|
||||
if(ar->block_length==ar->max_block_length){\
|
||||
if (ar->blocks_count>=ar->max_blocks_count) throw(ERR_MAXLENGTH);\
|
||||
ar->block_length=0;\
|
||||
create_block:\
|
||||
ar->values[ar->blocks_count]=malloc(ar->max_block_length*sizeof(type));\
|
||||
ar->blocks_count++;\
|
||||
}\
|
||||
ar->values[ar->blocks_count-1][ar->block_length]=element;\
|
||||
ar->block_length++;\
|
||||
}\
|
||||
void __Autoarr_add_##type(Autoarr_##type* ar, type element){ \
|
||||
if(!ar->values){ \
|
||||
ar->values=malloc(ar->max_blocks_count*sizeof(type*)); \
|
||||
goto create_block; \
|
||||
} \
|
||||
if(ar->block_length==ar->max_block_length){ \
|
||||
if (ar->blocks_count>=ar->max_blocks_count) throw(ERR_MAXLENGTH); \
|
||||
ar->block_length=0; \
|
||||
create_block: \
|
||||
ar->values[ar->blocks_count]=malloc(ar->max_block_length*sizeof(type)); \
|
||||
ar->blocks_count++; \
|
||||
} \
|
||||
ar->values[ar->blocks_count-1][ar->block_length]=element; \
|
||||
ar->block_length++; \
|
||||
} \
|
||||
\
|
||||
type __Autoarr_get_##type(Autoarr_##type* ar, u32 index){\
|
||||
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX);\
|
||||
return ar->values[index/ar->max_block_length][index%ar->max_block_length];\
|
||||
}\
|
||||
type __Autoarr_get_##type(Autoarr_##type* ar, u32 index){ \
|
||||
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \
|
||||
return ar->values[index/ar->max_block_length][index%ar->max_block_length]; \
|
||||
} \
|
||||
\
|
||||
type* __Autoarr_getptr_##type(Autoarr_##type* ar, u32 index){\
|
||||
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX);\
|
||||
return ar->values[index/ar->max_block_length]+(index%ar->max_block_length);\
|
||||
}\
|
||||
type* __Autoarr_getptr_##type(Autoarr_##type* ar, u32 index){ \
|
||||
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \
|
||||
return ar->values[index/ar->max_block_length]+(index%ar->max_block_length); \
|
||||
} \
|
||||
\
|
||||
void __Autoarr_set_##type(Autoarr_##type* ar, u32 index, type element){\
|
||||
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX);\
|
||||
ar->values[index/ar->max_block_length][index%ar->max_block_length]=element;\
|
||||
}\
|
||||
void __Autoarr_set_##type(Autoarr_##type* ar, u32 index, type element){ \
|
||||
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \
|
||||
ar->values[index/ar->max_block_length][index%ar->max_block_length]=element; \
|
||||
} \
|
||||
\
|
||||
void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr){\
|
||||
for(u16 i=0; i<ar->blocks_count;i++)\
|
||||
void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr){ \
|
||||
for(u16 i=0; i<ar->blocks_count;i++) \
|
||||
free(ar->values[i]); \
|
||||
free(ar->values);\
|
||||
if(freePtr) free(ar);\
|
||||
}\
|
||||
void ____Autoarr_free_##type(void* ar){\
|
||||
__Autoarr_free_##type((Autoarr_##type*)ar, false);\
|
||||
}\
|
||||
free(ar->values); \
|
||||
if(freePtr) free(ar); \
|
||||
} \
|
||||
void ____Autoarr_free_##type(void* ar){ \
|
||||
__Autoarr_free_##type((Autoarr_##type*)ar, false); \
|
||||
} \
|
||||
\
|
||||
type* __Autoarr_toArray_##type(Autoarr_##type* ar){\
|
||||
u32 length=Autoarr_length(ar);\
|
||||
type* array=malloc(length * sizeof(type));\
|
||||
for(u32 i=0; i<length; i++)\
|
||||
array[i]=__Autoarr_get_##type(ar, i);\
|
||||
return array;\
|
||||
}\
|
||||
type* __Autoarr_toArray_##type(Autoarr_##type* ar){ \
|
||||
u32 length=Autoarr_length(ar); \
|
||||
type* array=malloc(length * sizeof(type)); \
|
||||
for(u32 i=0; i<length; i++) \
|
||||
array[i]=__Autoarr_get_##type(ar, i); \
|
||||
return array; \
|
||||
} \
|
||||
\
|
||||
__functions_list_t_##type __functions_list_##type={\
|
||||
&__Autoarr_add_##type,\
|
||||
&__Autoarr_get_##type,\
|
||||
&__Autoarr_getptr_##type,\
|
||||
&__Autoarr_set_##type,\
|
||||
&__Autoarr_free_##type,\
|
||||
&__Autoarr_toArray_##type\
|
||||
};\
|
||||
__functions_list_t_##type __functions_list_##type={ \
|
||||
&__Autoarr_add_##type, \
|
||||
&__Autoarr_get_##type, \
|
||||
&__Autoarr_getptr_##type, \
|
||||
&__Autoarr_set_##type, \
|
||||
&__Autoarr_free_##type, \
|
||||
&__Autoarr_toArray_##type \
|
||||
}; \
|
||||
\
|
||||
Autoarr_##type* __Autoarr_create_##type(u16 max_blocks_count, u16 max_block_length){\
|
||||
Autoarr_##type* ar=malloc(sizeof(Autoarr_##type));\
|
||||
*ar=(Autoarr_##type){\
|
||||
.max_blocks_count=max_blocks_count,\
|
||||
.blocks_count=0,\
|
||||
.max_block_length=max_block_length,\
|
||||
.block_length=0,\
|
||||
.values=NULL,\
|
||||
.functions=&__functions_list_##type\
|
||||
};\
|
||||
return ar;\
|
||||
Autoarr_##type* __Autoarr_create_##type(u16 max_blocks_count, u16 max_block_length){ \
|
||||
Autoarr_##type* ar=malloc(sizeof(Autoarr_##type)); \
|
||||
*ar=(Autoarr_##type){ \
|
||||
.max_blocks_count=max_blocks_count, \
|
||||
.blocks_count=0, \
|
||||
.max_block_length=max_block_length, \
|
||||
.block_length=0, \
|
||||
.values=NULL, \
|
||||
.functions=&__functions_list_##type \
|
||||
}; \
|
||||
return ar; \
|
||||
}
|
||||
|
||||
#if __cplusplus
|
||||
|
||||
@ -154,10 +154,10 @@ Maybe __ReadList(DeserializeSharedData* shared){
|
||||
Autoarr(Unitype)* list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
|
||||
bool readingList=true;
|
||||
while (true){
|
||||
try(ReadValue((&readingList)), val, Autoarr_free(list, true))
|
||||
Autoarr_add(list,val.value);
|
||||
try(ReadValue((&readingList)), m_val, Autoarr_free(list, true))
|
||||
Autoarr_add(list,m_val.value);
|
||||
if (!readingList){
|
||||
if(val.value.typeId==ktid_Null)
|
||||
if(Unitype_isUniNull(m_val.value))
|
||||
Autoarr_pop(list);
|
||||
break;
|
||||
}
|
||||
@ -275,7 +275,7 @@ Maybe __ReadValue(DeserializeSharedData* shared, bool* readingList){
|
||||
case ';':
|
||||
case ',':
|
||||
if(valueStr.length!=0){
|
||||
if(value.typeId!=ktid_Null)
|
||||
if(!Unitype_isUniNull(value))
|
||||
safethrow_wrongchar(c,Unitype_free(value));
|
||||
try(ParseValue(valueStr),maybeParsed,;)
|
||||
value=maybeParsed.value;
|
||||
|
||||
@ -2,10 +2,10 @@
|
||||
#include "../String/StringBuilder.h"
|
||||
|
||||
|
||||
typedef struct SerializeSharedData{
|
||||
STRUCT(SerializeSharedData,
|
||||
StringBuilder* sh_builder;
|
||||
u8 sh_tabs;
|
||||
} SerializeSharedData;
|
||||
)
|
||||
#define b shared->sh_builder
|
||||
#define tabs shared->sh_tabs
|
||||
|
||||
@ -46,7 +46,7 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){
|
||||
else if(u.typeId==ktid_name(bool)){
|
||||
StringBuilder_append_cptr(b, u.Bool ? "true" : "false");
|
||||
}
|
||||
else if(u.typeId==ktid_Null){
|
||||
else if(Unitype_isUniNull(u)){
|
||||
safethrow("Null isn't supported in DtsodV24",;);
|
||||
}
|
||||
else if(u.typeId==ktid_ptrName(Autoarr_Unitype)){
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
#include "../String/StringBuilder.h"
|
||||
#include "io_includes.h"
|
||||
|
||||
ktid_define(File);
|
||||
kt_define(File, (freeMembers_t)file_close, NULL)
|
||||
|
||||
bool file_exists(const char* path){
|
||||
if(path[0]=='.'){
|
||||
@ -61,10 +61,10 @@ Maybe file_close(File* file){
|
||||
return MaybeNull;
|
||||
}
|
||||
|
||||
#define ioWriteCheck()\
|
||||
if(rezult==EOF)\
|
||||
safethrow(ERR_IO_EOF,;);\
|
||||
if(rezult!=0)\
|
||||
#define ioWriteCheck() \
|
||||
if(rezult==EOF) \
|
||||
safethrow(ERR_IO_EOF,;); \
|
||||
if(rezult!=0) \
|
||||
safethrow(ERR_IO,;);
|
||||
|
||||
Maybe file_writeChar(File* file, char byte){
|
||||
|
||||
@ -9,14 +9,14 @@ extern "C" {
|
||||
#include "../String/string.h"
|
||||
|
||||
typedef FILE File;
|
||||
ktid_declare(File);
|
||||
kt_declare(File);
|
||||
|
||||
bool file_exists(const char* path);
|
||||
|
||||
///@return Maybe<void>
|
||||
Maybe file_delete(const char* path, bool recursive);
|
||||
|
||||
PACK_ENUM(FileOpenMode,
|
||||
PACKED_ENUM(FileOpenMode,
|
||||
// open a file for reading
|
||||
FileOpenMode_Read=1,
|
||||
// (re)create a file for writing
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#include "Hashtable.h"
|
||||
|
||||
ktid_define(Hashtable);
|
||||
kt_define(Hashtable, __Hashtable_free, NULL);
|
||||
|
||||
// amount of rows
|
||||
static const u16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521};
|
||||
@ -95,7 +95,7 @@ Unitype Hashtable_get(Hashtable* ht, char* key){
|
||||
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){
|
||||
Unitype u=Hashtable_get(ht,key);
|
||||
*output=u;
|
||||
return u.typeId!=ktid_Null;
|
||||
return Unitype_isUniNull(u);
|
||||
}
|
||||
|
||||
void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u){
|
||||
|
||||
@ -4,14 +4,14 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../base/base.h"
|
||||
#include "../HashFunctions/hash.h"
|
||||
#include "KeyValuePair.h"
|
||||
|
||||
typedef struct Hashtable{
|
||||
STRUCT(Hashtable,
|
||||
u8 hein; // height=HT_HEIGHTS[hein]
|
||||
Autoarr(KVPair)** rows; // Autoarr[height]
|
||||
} Hashtable;
|
||||
ktid_declare(Hashtable);
|
||||
)
|
||||
|
||||
Hashtable* Hashtable_create();
|
||||
void Hashtable_free(Hashtable* ht);
|
||||
@ -33,12 +33,12 @@ Unitype* Hashtable_getptr(Hashtable* ht, char* key);
|
||||
Unitype Hashtable_get(Hashtable* ht, char* key);
|
||||
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output);
|
||||
|
||||
#define Hashtable_foreach(HT, EL, codeblock)({\
|
||||
u16 hmax=Hashtable_height(HT);\
|
||||
for(u16 h=0; h<hmax; h++){\
|
||||
Autoarr(KVPair)* AR=HT->rows[h];\
|
||||
Autoarr_foreach(AR, EL, codeblock);\
|
||||
}\
|
||||
#define Hashtable_foreach(HT, EL, codeblock)({ \
|
||||
u16 hmax=Hashtable_height(HT); \
|
||||
for(u16 h=0; h<hmax; h++){ \
|
||||
Autoarr(KVPair)* AR=HT->rows[h]; \
|
||||
Autoarr_foreach(AR, EL, codeblock); \
|
||||
} \
|
||||
})
|
||||
|
||||
#if __cplusplus
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#include "KeyValuePair.h"
|
||||
|
||||
ktid_define(KVPair);
|
||||
kt_define(KVPair, __KVPair_free, NULL);
|
||||
|
||||
Autoarr_define(KVPair)
|
||||
|
||||
|
||||
@ -7,11 +7,10 @@ extern "C" {
|
||||
#include "../base/base.h"
|
||||
#include "../Autoarr/Autoarr.h"
|
||||
|
||||
typedef struct KVPair{
|
||||
STRUCT(KVPair,
|
||||
char* key;
|
||||
Unitype value;
|
||||
} KVPair;
|
||||
ktid_declare(KVPair);
|
||||
)
|
||||
|
||||
Autoarr_declare(KVPair)
|
||||
|
||||
|
||||
@ -1,12 +1,11 @@
|
||||
#include "SearchTree.h"
|
||||
|
||||
ktid_define(STNode);
|
||||
kt_define(STNode, __STNode_free, NULL);
|
||||
|
||||
STNode* STNode_create(){
|
||||
STNode* node=malloc(sizeof(STNode));
|
||||
node->branches=NULL;
|
||||
node->value.typeId=ktid_Null;
|
||||
node->value.UInt64=0;
|
||||
node->value=UniNull;
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
@ -7,11 +7,10 @@ extern "C" {
|
||||
#include "../base/base.h"
|
||||
#include "../String/string.h"
|
||||
|
||||
typedef struct SearchTreeNode{
|
||||
struct SearchTreeNode**** branches; // *STNode[8][8][4]
|
||||
STRUCT(STNode,
|
||||
struct STNode**** branches; // *STNode[8][8][4]
|
||||
Unitype value;
|
||||
} STNode;
|
||||
ktid_declare(STNode);
|
||||
)
|
||||
|
||||
STNode* STNode_create();
|
||||
void STNode_free(STNode* node);
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
#include "StringBuilder.h"
|
||||
|
||||
Autoarr_define(string)
|
||||
|
||||
ktid_define(StringBuilder);
|
||||
kt_define(StringBuilder, __StringBuilder_free, NULL);
|
||||
|
||||
#define BL_C 32
|
||||
#define BL_L 1024
|
||||
|
||||
@ -7,13 +7,10 @@ extern "C" {
|
||||
#include "../Autoarr/Autoarr.h"
|
||||
#include "string.h"
|
||||
|
||||
Autoarr_declare(string)
|
||||
|
||||
typedef struct StringBuilder{
|
||||
STRUCT(StringBuilder,
|
||||
Autoarr(string)* compl_bufs;
|
||||
Autoarr(i8)* curr_buf;
|
||||
} StringBuilder;
|
||||
ktid_declare(StringBuilder);
|
||||
)
|
||||
|
||||
StringBuilder* StringBuilder_create(void);
|
||||
void StringBuilder_free(StringBuilder* b);
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
#include "string.h"
|
||||
|
||||
ktid_define(string);
|
||||
Array_define(string);
|
||||
kt_define(string, NULL, NULL);
|
||||
Array_define(string)
|
||||
Autoarr_define(string)
|
||||
|
||||
// copies str content to new char pointer value (adding '\0' at the end)
|
||||
char* string_extract(string str){
|
||||
|
||||
@ -6,15 +6,17 @@ extern "C" {
|
||||
|
||||
#include "../base/base.h"
|
||||
#include "../Array/Array.h"
|
||||
#include "../Autoarr/Autoarr.h"
|
||||
|
||||
// my fixed length string struct
|
||||
// doesn't store '\0' at the end
|
||||
typedef struct string{
|
||||
STRUCT(string,
|
||||
char* ptr; // char pointer
|
||||
u64 length; // amount of chars in ptr value
|
||||
} string;
|
||||
ktid_declare(string);
|
||||
Array_declare(string);
|
||||
)
|
||||
|
||||
Array_declare(string)
|
||||
Autoarr_declare(string)
|
||||
|
||||
static const string stringNull={NULL,0};
|
||||
|
||||
|
||||
@ -5,12 +5,13 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
#include "std.h"
|
||||
#include "type_system/typedef_macros.h"
|
||||
|
||||
PACK_ENUM(Endian,
|
||||
PACKED_ENUM(Endian,
|
||||
UnknownEndian=0,
|
||||
LittleEndian=1,
|
||||
BigEndian=2
|
||||
);
|
||||
)
|
||||
|
||||
Endian getEndian();
|
||||
|
||||
|
||||
@ -5,9 +5,9 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
#include "std.h"
|
||||
#include "type_system/unitype.h"
|
||||
#include "type_system/type_system.h"
|
||||
|
||||
PACK_ENUM(ErrorId,
|
||||
PACKED_ENUM(ErrorId,
|
||||
SUCCESS, // not an error
|
||||
ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX,
|
||||
ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR,
|
||||
@ -20,10 +20,10 @@ char* errname(ErrorId err);
|
||||
char* __genErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname);
|
||||
char* __extendErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname);
|
||||
|
||||
typedef struct Maybe{
|
||||
STRUCT(Maybe,
|
||||
Unitype value;
|
||||
char* errmsg;
|
||||
} Maybe;
|
||||
)
|
||||
|
||||
// return it if func doesn't return anything
|
||||
// .value .errmsg
|
||||
@ -42,47 +42,47 @@ void printMaybe(Maybe e);
|
||||
char* __doNothing(char* a);
|
||||
char* __unknownErr( );
|
||||
|
||||
#define __stringify_err(E) _Generic(\
|
||||
(E),\
|
||||
char*: __doNothing,\
|
||||
int: errname,\
|
||||
default: __unknownErr\
|
||||
#define __stringify_err(E) _Generic( \
|
||||
(E), \
|
||||
char*: __doNothing, \
|
||||
int: errname, \
|
||||
default: __unknownErr \
|
||||
)(E)
|
||||
|
||||
#if __cplusplus
|
||||
#define throw_id(E) __EXIT(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__)))
|
||||
#define throw_msg(E) __EXIT(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__)))
|
||||
|
||||
#define safethrow_id(E, FREEMEM) { FREEMEM;\
|
||||
__RETURN_EXCEPTION(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__)));\
|
||||
#define safethrow_id(E, FREEMEM) { FREEMEM; \
|
||||
__RETURN_EXCEPTION(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__))); \
|
||||
}
|
||||
#define safethrow_msg(E, FREEMEM) { FREEMEM;\
|
||||
__RETURN_EXCEPTION(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__)));\
|
||||
#define safethrow_msg(E, FREEMEM) { FREEMEM; \
|
||||
__RETURN_EXCEPTION(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__))); \
|
||||
}
|
||||
|
||||
#define try_cpp(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){\
|
||||
freeMem;\
|
||||
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\
|
||||
return _rezult;\
|
||||
#define try_cpp(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){ \
|
||||
freeMem; \
|
||||
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__); \
|
||||
return _rezult; \
|
||||
}
|
||||
|
||||
#else
|
||||
#define throw(E) __EXIT(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__)))
|
||||
|
||||
#define safethrow(E, FREEMEM) { FREEMEM;\
|
||||
__RETURN_EXCEPTION(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__)));\
|
||||
#define safethrow(E, FREEMEM) { FREEMEM; \
|
||||
__RETURN_EXCEPTION(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__))); \
|
||||
}
|
||||
|
||||
#define try(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){\
|
||||
freeMem;\
|
||||
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\
|
||||
return _rezult;\
|
||||
#define try(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){ \
|
||||
freeMem; \
|
||||
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__); \
|
||||
return _rezult; \
|
||||
}
|
||||
#endif
|
||||
|
||||
#define tryLast(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){\
|
||||
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\
|
||||
__EXIT(_rezult.errmsg);\
|
||||
#define tryLast(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){ \
|
||||
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__); \
|
||||
__EXIT(_rezult.errmsg); \
|
||||
}
|
||||
|
||||
#if __cplusplus
|
||||
|
||||
@ -2,40 +2,40 @@
|
||||
|
||||
#include "std.h"
|
||||
|
||||
#define __optime_print(opname, t)\
|
||||
char tnames[3][3]={"s\0","ms","us"};\
|
||||
i32 tni=0;\
|
||||
if(t>1000000){\
|
||||
t/=1000000;\
|
||||
tni=0;\
|
||||
} else if(t>1000){\
|
||||
t/=1000;\
|
||||
tni=1;\
|
||||
} else tni=2;\
|
||||
kprintf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%f \e[93m%s\n",\
|
||||
#define __optime_print(opname, t) \
|
||||
char tnames[3][3]={"s\0","ms","us"}; \
|
||||
i32 tni=0; \
|
||||
if(t>1000000){ \
|
||||
t/=1000000; \
|
||||
tni=0; \
|
||||
} else if(t>1000){ \
|
||||
t/=1000; \
|
||||
tni=1; \
|
||||
} else tni=2; \
|
||||
kprintf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%f \e[93m%s\n", \
|
||||
opname, t, tnames[tni]);
|
||||
|
||||
#ifdef CLOCK_REALTIME
|
||||
/// executes codeblock and prints execution time
|
||||
/// u64 op_i is counter of the internal loop
|
||||
/// uses non-standard high-precision clock
|
||||
#define optime(opname,repeats,codeblock) ({\
|
||||
struct timespec start, stop;\
|
||||
clock_gettime(CLOCK_REALTIME, &start);\
|
||||
for(u64 op_i=0;op_i<(u64)repeats;op_i++)\
|
||||
(codeblock);\
|
||||
clock_gettime(CLOCK_REALTIME, &stop);\
|
||||
f64 t=(f64)(stop.tv_sec-start.tv_sec)*1000000+(f64)(stop.tv_nsec-start.tv_nsec)/1000;\
|
||||
__optime_print(opname,t)\
|
||||
#define optime(opname,repeats,codeblock) ({ \
|
||||
struct timespec start, stop; \
|
||||
clock_gettime(CLOCK_REALTIME, &start); \
|
||||
for(u64 op_i=0;op_i<(u64)repeats;op_i++) \
|
||||
(codeblock); \
|
||||
clock_gettime(CLOCK_REALTIME, &stop); \
|
||||
f64 t=(f64)(stop.tv_sec-start.tv_sec)*1000000+(f64)(stop.tv_nsec-start.tv_nsec)/1000; \
|
||||
__optime_print(opname,t) \
|
||||
})
|
||||
#else
|
||||
/// uses standard low precision clock
|
||||
#define optime(opname,repeats,codeblock) ({\
|
||||
clock_t start=clock();\
|
||||
for(u64 op_i=0;op_i<(u64)repeats;op_i++)\
|
||||
(codeblock);\
|
||||
clock_t stop=clock();\
|
||||
f64 t=(f64)(stop-start)/CLOCKS_PER_SEC*1000000;\
|
||||
__optime_print(opname,t)\
|
||||
#define optime(opname,repeats,codeblock) ({ \
|
||||
clock_t start=clock(); \
|
||||
for(u64 op_i=0;op_i<(u64)repeats;op_i++) \
|
||||
(codeblock); \
|
||||
clock_t stop=clock(); \
|
||||
f64 t=(f64)(stop-start)/CLOCKS_PER_SEC*1000000; \
|
||||
__optime_print(opname,t) \
|
||||
})
|
||||
#endif
|
||||
|
||||
@ -22,6 +22,7 @@ typedef int64_t i64;
|
||||
typedef uint64_t u64;
|
||||
typedef float f32;
|
||||
typedef double f64;
|
||||
typedef void* Pointer;
|
||||
|
||||
// Usually bool from stdbool.h is defined as macro,
|
||||
// so in other macros like ktid_##TYPE it will be replaced by _Bool.
|
||||
@ -72,27 +73,27 @@ typedef u8 bool;
|
||||
#endif
|
||||
|
||||
|
||||
#define __count_args(\
|
||||
a0, a1, a2, a3, a4, a5, a6, a7 ,\
|
||||
a8, a9, a10,a11,a12,a13,a14,a15,\
|
||||
a16,a17,a18,a19,a20,a21,a22,a23,\
|
||||
a24,a25,a26,a27,a28,a29,a30,a31,\
|
||||
a32,a33,a34,a35,a36,a37,a38,a39,\
|
||||
a40,a41,a42,a43,a44,a45,a46,a47,\
|
||||
a48,a49,a50,a51,a52,a53,a54,a55,\
|
||||
a56,a57,a58,a59,a60,a61,a62,a63,\
|
||||
#define __count_args( \
|
||||
a0, a1, a2, a3, a4, a5, a6, a7 , \
|
||||
a8, a9, a10,a11,a12,a13,a14,a15, \
|
||||
a16,a17,a18,a19,a20,a21,a22,a23, \
|
||||
a24,a25,a26,a27,a28,a29,a30,a31, \
|
||||
a32,a33,a34,a35,a36,a37,a38,a39, \
|
||||
a40,a41,a42,a43,a44,a45,a46,a47, \
|
||||
a48,a49,a50,a51,a52,a53,a54,a55, \
|
||||
a56,a57,a58,a59,a60,a61,a62,a63, \
|
||||
a64,...) a64
|
||||
// Macro for counting variadic arguments (max 64)
|
||||
// (see usage in kprint.h)
|
||||
#define count_args(ARGS...) __count_args(\
|
||||
ARGS,\
|
||||
64,63,62,61,60,59,58,57,\
|
||||
56,55,54,53,52,51,50,49,\
|
||||
48,47,46,45,44,43,42,41,\
|
||||
40,39,38,37,36,35,34,33,\
|
||||
32,31,30,29,28,27,26,25,\
|
||||
24,23,22,21,20,19,18,17,\
|
||||
16,15,14,13,12,11,10,9,\
|
||||
#define count_args(ARGS...) __count_args( \
|
||||
ARGS, \
|
||||
64,63,62,61,60,59,58,57, \
|
||||
56,55,54,53,52,51,50,49, \
|
||||
48,47,46,45,44,43,42,41, \
|
||||
40,39,38,37,36,35,34,33, \
|
||||
32,31,30,29,28,27,26,25, \
|
||||
24,23,22,21,20,19,18,17, \
|
||||
16,15,14,13,12,11,10,9, \
|
||||
8, 7, 6, 5, 4, 3, 2, 1, 0)
|
||||
|
||||
/*
|
||||
@ -113,16 +114,12 @@ You can even embed it into macro in header (see kprint.h)
|
||||
#define PRAGMA_WARNING_POP _PRAGMA(GCC diagnostic pop)
|
||||
#define W_INT_CONVERSION "-Wint-conversion"
|
||||
#endif
|
||||
#define WARNING_DISABLE(WARNING, CODE)\
|
||||
PRAGMA_WARNING_PUSH\
|
||||
PRAGMA_WARNING_DISABLE(WARNING)\
|
||||
CODE;\
|
||||
#define WARNING_DISABLE(WARNING, CODE) \
|
||||
PRAGMA_WARNING_PUSH \
|
||||
PRAGMA_WARNING_DISABLE(WARNING) \
|
||||
CODE; \
|
||||
PRAGMA_WARNING_POP
|
||||
|
||||
#define PACK_ENUM(ENUM_NAME, ENUM_MEMBERS...) typedef enum ENUM_NAME {\
|
||||
ENUM_MEMBERS\
|
||||
} __attribute__((__packed__)) ENUM_NAME;
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
@ -4,17 +4,21 @@ For using some kerep capabilities, such as generic structs, unitype, and kprint,
|
||||
|
||||
## type id
|
||||
|
||||
Every registered type has its own id (`ktid`), which should be declared in header file and defined in source file.
|
||||
Example:
|
||||
Every registered type has its own `ktDescriptor` and `ktid` is an index of the descriptor in descriptors array.
|
||||
Descriptor should be declared in header file.
|
||||
Following macro declares `typedef struct` and `ktDescriptor`
|
||||
```c
|
||||
//someStruct.h
|
||||
typedef struct { } someStruct;
|
||||
ktid_declare(someStruct);
|
||||
STRUCT(someStruct,
|
||||
i32 i; i32 j; i32 k;
|
||||
);
|
||||
```
|
||||
then you need to define descriptor in a source file
|
||||
```c
|
||||
//someStruct.c
|
||||
ktid_define(someStruct);
|
||||
kt_define(someStruct);
|
||||
```
|
||||
and register it.
|
||||
|
||||
## type descriptors
|
||||
|
||||
|
||||
@ -2,18 +2,20 @@
|
||||
#include "../base.h"
|
||||
#include "../../kprint/kprint_format.h"
|
||||
|
||||
|
||||
// accepts char* (ptr to char) and char** (ptr to string)
|
||||
char* __toString_char(void* c, u32 fmt) {
|
||||
//*c=char
|
||||
// *c=char*
|
||||
if(kp_fmt_dataFormat(fmt)==kp_s){
|
||||
return cptr_copy(*(char**)c); // to avoid segmentation fault on free() when *c allocalet on stack
|
||||
}
|
||||
// *c=char
|
||||
if(kp_fmt_dataFormat(fmt)==kp_c){
|
||||
char* cc=malloc(2);
|
||||
cc[0]=*(char*)c;
|
||||
cc[1]=0;
|
||||
return cc;
|
||||
}
|
||||
// *c=cstring
|
||||
else if(kp_fmt_dataFormat(fmt)==kp_s){
|
||||
return cptr_copy(*(char**)c);
|
||||
}
|
||||
else throw(ERR_FORMAT);
|
||||
}
|
||||
|
||||
@ -61,23 +63,23 @@ char* toString_u64(u64 n, bool withPostfix, bool uppercase){
|
||||
return cptr_copy((char*)str+i);
|
||||
}
|
||||
|
||||
#define _toString_float_impl(bufsize, maxPrecision) {\
|
||||
char str[bufsize];\
|
||||
if(precision>maxPrecision)\
|
||||
throw("too big precision");\
|
||||
if(precision==0)\
|
||||
precision=toString_float_default_precision;\
|
||||
i32 cn=IFMSC(\
|
||||
sprintf_s(str, bufsize, "%.*f", precision, n),\
|
||||
sprintf(str, "%.*f", precision, n)\
|
||||
);\
|
||||
/* remove trailing zeroes except .0*/\
|
||||
while(str[cn-1]=='0' && str[cn-2]!='.')\
|
||||
cn--;\
|
||||
if(withPostfix)\
|
||||
str[cn++]= uppercase ? 'F' : 'f';\
|
||||
str[cn]='\0';\
|
||||
return cptr_copy(str);\
|
||||
#define _toString_float_impl(bufsize, maxPrecision) { \
|
||||
char str[bufsize]; \
|
||||
if(precision>maxPrecision) \
|
||||
throw("too big precision"); \
|
||||
if(precision==0) \
|
||||
precision=toString_float_default_precision; \
|
||||
i32 cn=IFMSC( \
|
||||
sprintf_s(str, bufsize, "%.*f", precision, n), \
|
||||
sprintf(str, "%.*f", precision, n) \
|
||||
); \
|
||||
/* remove trailing zeroes except .0*/ \
|
||||
while(str[cn-1]=='0' && str[cn-2]!='.') \
|
||||
cn--; \
|
||||
if(withPostfix) \
|
||||
str[cn++]= uppercase ? 'F' : 'f'; \
|
||||
str[cn]='\0'; \
|
||||
return cptr_copy(str); \
|
||||
}
|
||||
|
||||
char* toString_f32(f32 n, u8 precision, bool withPostfix, bool uppercase)
|
||||
@ -86,15 +88,15 @@ char* toString_f32(f32 n, u8 precision, bool withPostfix, bool uppercase)
|
||||
char* toString_f64(f64 n, u8 precision, bool withPostfix, bool uppercase)
|
||||
_toString_float_impl(512, toString_f64_max_precision)
|
||||
|
||||
#define byte_to_bits(byte) {\
|
||||
str[cn++]='0' + (u8)((byte>>7)&1); /* 8th bit */\
|
||||
str[cn++]='0' + (u8)((byte>>6)&1); /* 7th bit */\
|
||||
str[cn++]='0' + (u8)((byte>>5)&1); /* 6th bit */\
|
||||
str[cn++]='0' + (u8)((byte>>4)&1); /* 5th bit */\
|
||||
str[cn++]='0' + (u8)((byte>>3)&1); /* 4th bit */\
|
||||
str[cn++]='0' + (u8)((byte>>2)&1); /* 3th bit */\
|
||||
str[cn++]='0' + (u8)((byte>>1)&1); /* 2th bit */\
|
||||
str[cn++]='0' + (u8)((byte>>0)&1); /* 1th bit */\
|
||||
#define byte_to_bits(byte) { \
|
||||
str[cn++]='0' + (u8)((byte>>7)&1); /* 8th bit */ \
|
||||
str[cn++]='0' + (u8)((byte>>6)&1); /* 7th bit */ \
|
||||
str[cn++]='0' + (u8)((byte>>5)&1); /* 6th bit */ \
|
||||
str[cn++]='0' + (u8)((byte>>4)&1); /* 5th bit */ \
|
||||
str[cn++]='0' + (u8)((byte>>3)&1); /* 4th bit */ \
|
||||
str[cn++]='0' + (u8)((byte>>2)&1); /* 3th bit */ \
|
||||
str[cn++]='0' + (u8)((byte>>1)&1); /* 2th bit */ \
|
||||
str[cn++]='0' + (u8)((byte>>0)&1); /* 1th bit */ \
|
||||
}
|
||||
|
||||
char* toString_bin(void* _bytes, u32 size, bool inverse, bool withPrefix){
|
||||
@ -163,60 +165,72 @@ char* toString_hex(void* _bytes, u32 size, bool inverse, bool withPrefix, bool u
|
||||
}
|
||||
|
||||
|
||||
#define __toString_i32_def(BITS) char* __toString_i##BITS(void* _n, u32 f){\
|
||||
switch(kp_fmt_dataFormat(f)){\
|
||||
case kp_i: ;\
|
||||
i##BITS n=*(i##BITS*)_n;\
|
||||
return toString_i64(n);\
|
||||
case kp_b:\
|
||||
return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f));\
|
||||
case kp_h:\
|
||||
return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f));\
|
||||
default:\
|
||||
kprintf("\n%u\n", kp_fmt_dataFormat(f));\
|
||||
throw(ERR_FORMAT);\
|
||||
return NULL;\
|
||||
}\
|
||||
#define __toString_i32_def(BITS) char* __toString_i##BITS(void* _n, u32 f){ \
|
||||
switch(kp_fmt_dataFormat(f)){ \
|
||||
case kp_i: ; \
|
||||
i##BITS n=*(i##BITS*)_n; \
|
||||
return toString_i64(n); \
|
||||
case kp_b: \
|
||||
return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f)); \
|
||||
case kp_h: \
|
||||
return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f)); \
|
||||
default: \
|
||||
kprintf("\n%u\n", kp_fmt_dataFormat(f)); \
|
||||
throw(ERR_FORMAT); \
|
||||
return NULL; \
|
||||
} \
|
||||
}
|
||||
__toString_i32_def(8)
|
||||
__toString_i32_def(16)
|
||||
__toString_i32_def(32)
|
||||
__toString_i32_def(64)
|
||||
|
||||
#define __toString_u_def(BITS) char* __toString_u##BITS(void* _n, u32 f){\
|
||||
switch(kp_fmt_dataFormat(f)){\
|
||||
case kp_u: ;\
|
||||
u##BITS n=*(u##BITS*)_n;\
|
||||
return toString_u64(n, kp_fmt_withPostfix(f), kp_fmt_isUpper(f));\
|
||||
case kp_b:\
|
||||
return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f));\
|
||||
case kp_h:\
|
||||
return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f));\
|
||||
default:\
|
||||
kprintf("\n%u\n", kp_fmt_dataFormat(f));\
|
||||
throw(ERR_FORMAT);\
|
||||
return NULL;\
|
||||
}\
|
||||
#define __toString_u_def(BITS) char* __toString_u##BITS(void* _n, u32 f){ \
|
||||
switch(kp_fmt_dataFormat(f)){ \
|
||||
case kp_u: ; \
|
||||
u##BITS n=*(u##BITS*)_n; \
|
||||
return toString_u64(n, kp_fmt_withPostfix(f), kp_fmt_isUpper(f)); \
|
||||
case kp_b: \
|
||||
return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f)); \
|
||||
case kp_h: \
|
||||
return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f)); \
|
||||
default: \
|
||||
kprintf("\n%u\n", kp_fmt_dataFormat(f)); \
|
||||
throw(ERR_FORMAT); \
|
||||
return NULL; \
|
||||
} \
|
||||
}
|
||||
__toString_u_def(8)
|
||||
__toString_u_def(16)
|
||||
__toString_u_def(32)
|
||||
__toString_u_def(64)
|
||||
// __toString_u_def(64)
|
||||
char* __toString_u64(void* _n, u32 f){
|
||||
switch(kp_fmt_dataFormat(f)){
|
||||
case kp_u: ;
|
||||
u64 n=*(u64*)_n;
|
||||
return toString_u64(n, kp_fmt_withPostfix(f), kp_fmt_isUpper(f));
|
||||
case kp_b:
|
||||
return toString_bin(_n, 64/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f));
|
||||
case kp_h:
|
||||
return toString_hex(_n, 64/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f));
|
||||
default:
|
||||
kprintf("\n%u\n", kp_fmt_dataFormat(f)); throw(ERR_FORMAT); return NULL; }
|
||||
}
|
||||
|
||||
#define __toString_float_def(BITS) char* __toString_f##BITS(void* _n, u32 f){\
|
||||
switch(kp_fmt_dataFormat(f)){\
|
||||
case kp_f: ;\
|
||||
f##BITS n=*(f##BITS*)_n;\
|
||||
return toString_f64(n, toString_float_default_precision, kp_fmt_withPostfix(f), kp_fmt_isUpper(f));\
|
||||
case kp_b:\
|
||||
return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f));\
|
||||
case kp_h:\
|
||||
return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f));\
|
||||
default:\
|
||||
kprintf("\n%u\n", kp_fmt_dataFormat(f));\
|
||||
throw(ERR_FORMAT);\
|
||||
return NULL;\
|
||||
}\
|
||||
#define __toString_float_def(BITS) char* __toString_f##BITS(void* _n, u32 f){ \
|
||||
switch(kp_fmt_dataFormat(f)){ \
|
||||
case kp_f: ; \
|
||||
f##BITS n=*(f##BITS*)_n; \
|
||||
return toString_f64(n, toString_float_default_precision, kp_fmt_withPostfix(f), kp_fmt_isUpper(f)); \
|
||||
case kp_b: \
|
||||
return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f)); \
|
||||
case kp_h: \
|
||||
return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f)); \
|
||||
default: \
|
||||
kprintf("\n%u\n", kp_fmt_dataFormat(f)); \
|
||||
throw(ERR_FORMAT); \
|
||||
return NULL; \
|
||||
} \
|
||||
}
|
||||
|
||||
__toString_float_def(32)
|
||||
|
||||
@ -6,8 +6,8 @@ extern "C" {
|
||||
|
||||
#include "../errors.h"
|
||||
|
||||
// char and cstring
|
||||
// has different output for fmtChar and fmtString
|
||||
// accepts char* (ptr to char) and char** (ptr to string)
|
||||
// uses format kp_s and kp_c to determine what type is <c> argument
|
||||
char* __toString_char(void* c, u32 fmt);
|
||||
|
||||
// bool
|
||||
|
||||
@ -4,85 +4,92 @@
|
||||
#include "../../SearchTree/SearchTree.h"
|
||||
#include "../../Hashtable/Hashtable.h"
|
||||
#include "../../String/StringBuilder.h"
|
||||
#include "../../Filesystem/filesystem.h"
|
||||
#include "base_toString.h"
|
||||
|
||||
void ktDescriptors_initKerepTypes(){
|
||||
// null
|
||||
__kt_register("Null", sizeof(NULL), NULL, NULL);
|
||||
ktid_Null=ktid_last;
|
||||
// base types
|
||||
kt_register(char, NULL, __toString_char);
|
||||
kt_register(bool, NULL, __toString_bool);
|
||||
kt_register(f32, NULL, __toString_f32);
|
||||
kt_register(f64, NULL, __toString_f64);
|
||||
kt_register(i8, NULL, __toString_i8);
|
||||
kt_register(u8, NULL, __toString_u8);
|
||||
kt_register(i16, NULL, __toString_i16);
|
||||
kt_register(u16, NULL, __toString_u16);
|
||||
kt_register(i32, NULL, __toString_i32);
|
||||
kt_register(u32, NULL, __toString_u32);
|
||||
kt_register(i64, NULL, __toString_i64);
|
||||
kt_register(u64, NULL, __toString_u64);
|
||||
kt_register(Pointer);
|
||||
if(ktid_Pointer!=0) // this can break UnitypeNull
|
||||
throw("ktid_Pointer!=0, you must init kerep types before any other types");
|
||||
|
||||
kt_register(char);
|
||||
kt_register(bool);
|
||||
kt_register(f32);
|
||||
kt_register(f64);
|
||||
kt_register(i8);
|
||||
kt_register(u8);
|
||||
kt_register(i16);
|
||||
kt_register(u16);
|
||||
kt_register(i32);
|
||||
kt_register(u32);
|
||||
kt_register(i64);
|
||||
kt_register(u64);
|
||||
|
||||
// ktDescriptor
|
||||
kt_register(ktDescriptor, NULL, NULL);
|
||||
|
||||
kt_register(ktDescriptor);
|
||||
|
||||
// base type arrays
|
||||
kt_register(Array_char, (freeMembers_t)Array_char_free, NULL);
|
||||
kt_register(Array_bool, (freeMembers_t)Array_bool_free, NULL);
|
||||
kt_register(Array_f32, (freeMembers_t)Array_f32_free, NULL);
|
||||
kt_register(Array_f64, (freeMembers_t)Array_f64_free, NULL);
|
||||
kt_register(Array_i8, (freeMembers_t)Array_i8_free, NULL);
|
||||
kt_register(Array_u8, (freeMembers_t)Array_u8_free, NULL);
|
||||
kt_register(Array_i16, (freeMembers_t)Array_i16_free, NULL);
|
||||
kt_register(Array_u16, (freeMembers_t)Array_u16_free, NULL);
|
||||
kt_register(Array_i32, (freeMembers_t)Array_i32_free, NULL);
|
||||
kt_register(Array_u32, (freeMembers_t)Array_u32_free, NULL);
|
||||
kt_register(Array_i64, (freeMembers_t)Array_i64_free, NULL);
|
||||
kt_register(Array_u64, (freeMembers_t)Array_u64_free, NULL);
|
||||
kt_register(Array_char);
|
||||
kt_register(Array_bool);
|
||||
kt_register(Array_f32);
|
||||
kt_register(Array_f64);
|
||||
kt_register(Array_i8);
|
||||
kt_register(Array_u8);
|
||||
kt_register(Array_i16);
|
||||
kt_register(Array_u16);
|
||||
kt_register(Array_i32);
|
||||
kt_register(Array_u32);
|
||||
kt_register(Array_i64);
|
||||
kt_register(Array_u64);
|
||||
kt_register(Array_Pointer);
|
||||
|
||||
// base type autoarrs
|
||||
kt_register(Autoarr_char, ____Autoarr_free_char, NULL);
|
||||
kt_register(Autoarr_bool, ____Autoarr_free_bool, NULL);
|
||||
kt_register(Autoarr_f32, ____Autoarr_free_f32, NULL);
|
||||
kt_register(Autoarr_f64, ____Autoarr_free_f64, NULL);
|
||||
kt_register(Autoarr_i8, ____Autoarr_free_i8, NULL);
|
||||
kt_register(Autoarr_u8, ____Autoarr_free_u8, NULL);
|
||||
kt_register(Autoarr_i16, ____Autoarr_free_i16, NULL);
|
||||
kt_register(Autoarr_u16, ____Autoarr_free_u16, NULL);
|
||||
kt_register(Autoarr_i32, ____Autoarr_free_i32, NULL);
|
||||
kt_register(Autoarr_u32, ____Autoarr_free_u32, NULL);
|
||||
kt_register(Autoarr_i64, ____Autoarr_free_i64, NULL);
|
||||
kt_register(Autoarr_u64, ____Autoarr_free_u64, NULL);
|
||||
kt_register(Autoarr_char);
|
||||
kt_register(Autoarr_bool);
|
||||
kt_register(Autoarr_f32);
|
||||
kt_register(Autoarr_f64);
|
||||
kt_register(Autoarr_i8);
|
||||
kt_register(Autoarr_u8);
|
||||
kt_register(Autoarr_i16);
|
||||
kt_register(Autoarr_u16);
|
||||
kt_register(Autoarr_i32);
|
||||
kt_register(Autoarr_u32);
|
||||
kt_register(Autoarr_i64);
|
||||
kt_register(Autoarr_u64);
|
||||
kt_register(Autoarr_Pointer);
|
||||
|
||||
// Unitype
|
||||
kt_register(Unitype, __UnitypePtr_free, NULL);
|
||||
kt_register(Array_Unitype, __Array_Unitype_free_, NULL);
|
||||
kt_register(Autoarr_Unitype, ____Autoarr_free_Unitype_, NULL);
|
||||
kt_register(Unitype);
|
||||
kt_register(Array_Unitype);
|
||||
kt_register(Autoarr_Unitype);
|
||||
// replacing autogenerated freear() function to custom
|
||||
Autoarr_Unitype* _uar=Autoarr_create(Unitype, 1, 1);
|
||||
_uar->functions->freear=__Autoarr_free_Unitype_;
|
||||
Autoarr_free(_uar, true);
|
||||
|
||||
// SearchTreeNode
|
||||
kt_register(STNode, __STNode_free, NULL);
|
||||
// STNode
|
||||
kt_register(STNode);
|
||||
|
||||
// KeyValuePair
|
||||
kt_register(KVPair, __KVPair_free, NULL);
|
||||
kt_register(Autoarr_KVPair, ____Autoarr_free_KVPair_, NULL);
|
||||
kt_register(KVPair);
|
||||
kt_register(Autoarr_KVPair);
|
||||
// replacing autogenerated freear() function to custom
|
||||
Autoarr_KVPair* _kvpar=Autoarr_create(KVPair, 1, 1);
|
||||
_kvpar->functions->freear=__Autoarr_free_KVPair_;
|
||||
Autoarr_free(_kvpar, true);
|
||||
|
||||
// Hashtable
|
||||
kt_register(Hashtable, __Hashtable_free, NULL);
|
||||
kt_register(Hashtable);
|
||||
|
||||
// string
|
||||
kt_register(string, NULL, NULL);
|
||||
kt_register(Array_string, (freeMembers_t)Array_string_free, NULL);
|
||||
kt_register(Autoarr_string, ____Autoarr_free_string, NULL);
|
||||
kt_register(string);
|
||||
kt_register(Array_string);
|
||||
kt_register(Autoarr_string);
|
||||
|
||||
// StringBuilder
|
||||
kt_register(StringBuilder, __StringBuilder_free, NULL);
|
||||
kt_register(StringBuilder);
|
||||
|
||||
//File
|
||||
kt_register(File);
|
||||
}
|
||||
|
||||
@ -6,16 +6,40 @@ extern "C" {
|
||||
|
||||
#include "../std.h"
|
||||
#include "ktid.h"
|
||||
#include "typedef_macros.h"
|
||||
|
||||
#define kt_declare(TYPE)\
|
||||
ktid_declare(TYPE);\
|
||||
extern ktDescriptor ktDescriptor_##TYPE; \
|
||||
extern ktDescriptor ktDescriptor_##TYPE##_Ptr;
|
||||
|
||||
#define kt_define(TYPE, FREE_FUNC, TOSTRING_FUNC)\
|
||||
ktid_define(TYPE); \
|
||||
ktDescriptor ktDescriptor_##TYPE={ \
|
||||
.name=#TYPE, \
|
||||
.id=ktid_undefined, \
|
||||
.size=sizeof(TYPE), \
|
||||
.freeMembers=FREE_FUNC, \
|
||||
.toString=TOSTRING_FUNC \
|
||||
}; \
|
||||
ktDescriptor ktDescriptor_##TYPE##_Ptr={\
|
||||
.name=#TYPE "_Ptr", \
|
||||
.id=ktid_undefined, \
|
||||
.size=sizeof(TYPE), \
|
||||
.freeMembers=FREE_FUNC, \
|
||||
.toString=TOSTRING_FUNC \
|
||||
};
|
||||
|
||||
typedef void (*freeMembers_t)(void*);
|
||||
typedef char* (*toString_t)(void* obj, u32 fmt);
|
||||
typedef struct ktDescriptor{
|
||||
|
||||
STRUCT(ktDescriptor,
|
||||
char* name;
|
||||
ktid id;
|
||||
u16 size;
|
||||
freeMembers_t freeMembers; // NULL or function which frees all struct members
|
||||
toString_t toString; // NULL or function which generates string representaion of object
|
||||
} ktDescriptor;
|
||||
)
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
|
||||
@ -1,62 +1,57 @@
|
||||
#include "../../Autoarr/Autoarr.h"
|
||||
#include "type_system.h"
|
||||
#include "base_toString.h"
|
||||
|
||||
Autoarr_declare(ktDescriptor)
|
||||
Autoarr_define(ktDescriptor)
|
||||
kt_define(Pointer, free, __toString_u64);
|
||||
kt_define(char,NULL, __toString_char);
|
||||
kt_define(bool,NULL, __toString_bool);
|
||||
kt_define(f32, NULL, __toString_f32);
|
||||
kt_define(f64, NULL, __toString_f64);
|
||||
kt_define(i8, NULL, __toString_i8);
|
||||
kt_define(u8, NULL, __toString_u8);
|
||||
kt_define(i16, NULL, __toString_i16);
|
||||
kt_define(u16, NULL, __toString_u16);
|
||||
kt_define(i32, NULL, __toString_i32);
|
||||
kt_define(u32, NULL, __toString_u32);
|
||||
kt_define(i64, NULL, __toString_i64);
|
||||
kt_define(u64, NULL, __toString_u64);
|
||||
|
||||
ktid ktid_Null=-1;
|
||||
kt_define(ktDescriptor, NULL, NULL);
|
||||
|
||||
ktid_define(char);
|
||||
ktid_define(bool);
|
||||
ktid_define(f32);
|
||||
ktid_define(f64);
|
||||
ktid_define(i8);
|
||||
ktid_define(u8);
|
||||
ktid_define(i16);
|
||||
ktid_define(u16);
|
||||
ktid_define(i32);
|
||||
ktid_define(u32);
|
||||
ktid_define(i64);
|
||||
ktid_define(u64);
|
||||
|
||||
ktid_define(ktDescriptor);
|
||||
typedef ktDescriptor* ktDescriptor_Ptr;
|
||||
|
||||
// type descriptors are stored here during initialization
|
||||
Autoarr(ktDescriptor)* __ktDescriptors=NULL;
|
||||
Autoarr(Pointer)* __descriptorPointers=NULL;
|
||||
// here type descriptors are stored when initialization is complited
|
||||
ktDescriptor* typeDescriptors=NULL;
|
||||
ktDescriptor** typeDescriptors=NULL;
|
||||
ktid ktid_last=-1;
|
||||
|
||||
typedef enum{
|
||||
ENUM(ktDescriptorsState,
|
||||
NotInitialized, Initializing, Initialized
|
||||
} ktDescriptorsState;
|
||||
)
|
||||
ktDescriptorsState initState=NotInitialized;
|
||||
|
||||
void ktDescriptors_beginInit(){
|
||||
kprintf("\e[94mtype descriptors initializing...\n");
|
||||
__ktDescriptors=Autoarr_create(ktDescriptor, 256, 256);
|
||||
if(__ktDescriptors==NULL) throw(ERR_NULLPTR);
|
||||
__descriptorPointers=Autoarr_create(Pointer, 256, 256);
|
||||
if(__descriptorPointers==NULL)
|
||||
throw(ERR_NULLPTR);
|
||||
}
|
||||
|
||||
void ktDescriptors_endInit(){
|
||||
typeDescriptors=Autoarr_toArray(__ktDescriptors);
|
||||
Autoarr_free(__ktDescriptors,true);
|
||||
typeDescriptors=(ktDescriptor**)Autoarr_toArray(__descriptorPointers);
|
||||
Autoarr_free(__descriptorPointers,true);
|
||||
if(typeDescriptors==NULL) throw(ERR_NULLPTR);
|
||||
kprintf("\e[92minitialized %u type descriptors\n", ktid_last);
|
||||
}
|
||||
|
||||
void __kt_register(char* name, i16 size, void (*freeMembers)(void*), char* (*toString)(void*, u32)){
|
||||
ktDescriptor typeDesc={
|
||||
.name=name,
|
||||
.size=size,
|
||||
.id=++ktid_last,
|
||||
.freeMembers=freeMembers,
|
||||
.toString=toString
|
||||
};
|
||||
Autoarr_add(__ktDescriptors, typeDesc);
|
||||
void __kt_register(ktDescriptor* descriptor){
|
||||
descriptor->id=++ktid_last;
|
||||
Autoarr_add(__descriptorPointers, descriptor);
|
||||
}
|
||||
|
||||
ktDescriptor ktDescriptor_get(ktid id){
|
||||
if(id>ktid_last) {
|
||||
ktDescriptor* ktDescriptor_get(ktid id){
|
||||
if(id>ktid_last || id==ktid_undefined) {
|
||||
kprintf("\ntype id: %u\n",id);
|
||||
throw("invalid type id");
|
||||
}
|
||||
|
||||
@ -9,39 +9,38 @@ extern "C" {
|
||||
#include "ktDescriptor.h"
|
||||
|
||||
extern ktid ktid_last;
|
||||
void __kt_register(char* name, i16 size, void (*freeMembers)(void*), char* (*toString)(void*, u32));
|
||||
void __kt_register(ktDescriptor* descriptor);
|
||||
|
||||
#define kt_register(TYPE, FREE_MEMBERS_FUNC, TO_STRING_FUNC)\
|
||||
__kt_register(#TYPE, sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\
|
||||
ktid_##TYPE=ktid_last;\
|
||||
__kt_register(#TYPE "*", sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\
|
||||
#define kt_register(TYPE) \
|
||||
__kt_register(&ktDescriptor_##TYPE); \
|
||||
ktid_##TYPE=ktid_last; \
|
||||
__kt_register(&ktDescriptor_##TYPE##_Ptr); \
|
||||
ktid_##TYPE##_Ptr=ktid_last;
|
||||
|
||||
void ktDescriptors_beginInit();
|
||||
void ktDescriptors_endInit();
|
||||
|
||||
/// @param id id of registered type
|
||||
ktDescriptor ktDescriptor_get(ktid id);
|
||||
ktDescriptor* ktDescriptor_get(ktid id);
|
||||
|
||||
// call it to free heap-allocated ktDescriptors array
|
||||
void ktDescriptors_free();
|
||||
|
||||
extern ktid ktid_Null;
|
||||
kt_declare(Pointer);
|
||||
kt_declare(char);
|
||||
kt_declare(bool);
|
||||
kt_declare(f32);
|
||||
kt_declare(f64);
|
||||
kt_declare(i8);
|
||||
kt_declare(u8);
|
||||
kt_declare(i16);
|
||||
kt_declare(u16);
|
||||
kt_declare(i32);
|
||||
kt_declare(u32);
|
||||
kt_declare(i64);
|
||||
kt_declare(u64);
|
||||
|
||||
ktid_declare(char);
|
||||
ktid_declare(bool);
|
||||
ktid_declare(f32);
|
||||
ktid_declare(f64);
|
||||
ktid_declare(i8);
|
||||
ktid_declare(u8);
|
||||
ktid_declare(i16);
|
||||
ktid_declare(u16);
|
||||
ktid_declare(i32);
|
||||
ktid_declare(u32);
|
||||
ktid_declare(i64);
|
||||
ktid_declare(u64);
|
||||
|
||||
ktid_declare(ktDescriptor);
|
||||
kt_declare(ktDescriptor);
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
|
||||
@ -5,17 +5,20 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../std.h"
|
||||
#include "typedef_macros.h"
|
||||
|
||||
typedef u16 ktid;
|
||||
static const ktid ktid_undefined=-1;
|
||||
|
||||
#define ktid_name(TYPE) ktid_##TYPE
|
||||
#define ktid_ptrName(TYPE) ktid_##TYPE##_Ptr
|
||||
|
||||
#define ktid_declare(TYPE)\
|
||||
extern ktid ktid_##TYPE;\
|
||||
#define ktid_declare(TYPE) \
|
||||
extern ktid ktid_##TYPE; \
|
||||
extern ktid ktid_##TYPE##_Ptr;
|
||||
|
||||
#define ktid_define(TYPE)\
|
||||
ktid ktid_##TYPE=-1;\
|
||||
#define ktid_define(TYPE) \
|
||||
ktid ktid_##TYPE=-1; \
|
||||
ktid ktid_##TYPE##_Ptr=-1;
|
||||
|
||||
#if __cplusplus
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "init.h"
|
||||
#include "ktid.h"
|
||||
#include "ktDescriptor.h"
|
||||
#include "kt_functions.h"
|
||||
#include "unitype.h"
|
||||
#include "typedef_macros.h"
|
||||
|
||||
14
src/base/type_system/typedef_macros.h
Normal file
14
src/base/type_system/typedef_macros.h
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#define ENUM(ENUM_NAME, ENUM_MEMBERS...) typedef enum ENUM_NAME { \
|
||||
ENUM_MEMBERS \
|
||||
} ENUM_NAME;
|
||||
|
||||
#define PACKED_ENUM(ENUM_NAME, ENUM_MEMBERS...) typedef enum ENUM_NAME { \
|
||||
ENUM_MEMBERS \
|
||||
} __attribute__((__packed__)) ENUM_NAME;
|
||||
|
||||
#define STRUCT(STRUCT_NAME, STRUCT_MEMBERS...) typedef struct STRUCT_NAME { \
|
||||
STRUCT_MEMBERS \
|
||||
} STRUCT_NAME; \
|
||||
kt_declare(STRUCT_NAME);
|
||||
@ -1,52 +1,90 @@
|
||||
#include "../base.h"
|
||||
#include "../../kprint/kprint_format.h"
|
||||
|
||||
ktid_define(Unitype);
|
||||
|
||||
char* __Unitype_toString(void* _u, u32 fmt){
|
||||
return Unitype_toString(*(Unitype*)_u, fmt);
|
||||
}
|
||||
|
||||
kt_define(Unitype, __UnitypePtr_free, __Unitype_toString);
|
||||
|
||||
void Unitype_free(Unitype u){
|
||||
ktDescriptor type=ktDescriptor_get(u.typeId);
|
||||
if(type.freeMembers)
|
||||
type.freeMembers(u.VoidPtr);
|
||||
if(u.typeId==ktid_undefined){
|
||||
if(u.VoidPtr!=NULL)
|
||||
throw("unitype with undefined typeId has value");
|
||||
return;
|
||||
}
|
||||
|
||||
ktDescriptor* type=ktDescriptor_get(u.typeId);
|
||||
if(type->freeMembers)
|
||||
type->freeMembers(u.VoidPtr);
|
||||
if(u.allocatedInHeap)
|
||||
free(u.VoidPtr);
|
||||
}
|
||||
void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); }
|
||||
|
||||
char* toString_Unitype(void* _u, u32 fmt){
|
||||
Unitype* u=_u;
|
||||
ktDescriptor type=ktDescriptor_get(u->typeId);
|
||||
char* valuestr=type.toString(_u, fmt);
|
||||
char* rezult=cptr_concat("{ type: ", type.name,
|
||||
", allocated on heap: ", (u->allocatedInHeap ? "true" : "false"),
|
||||
|
||||
char* Unitype_toString(Unitype u, u32 fmt){
|
||||
if(u.typeId==ktid_undefined){
|
||||
if(u.VoidPtr!=NULL)
|
||||
throw("unitype with undefined typeId has value");
|
||||
return cptr_copy("{ERROR_TYPE}");
|
||||
}
|
||||
|
||||
if(fmt==0) {
|
||||
if(u.typeId==ktid_name(bool) ||
|
||||
u.typeId==ktid_name(i8) || u.typeId==ktid_name(i16) ||
|
||||
u.typeId==ktid_name(i32) || u.typeId==ktid_name(i64)){
|
||||
// auto format set
|
||||
fmt=kp_i;
|
||||
// replaces value with pointer to value to pass into toString_i64(void*, u32)
|
||||
i64 value=u.Int64;
|
||||
u.VoidPtr=&value;
|
||||
}
|
||||
else if(u.typeId==ktid_name(u8) || u.typeId==ktid_name(u16) ||
|
||||
u.typeId==ktid_name(u32) || u.typeId==ktid_name(u64)){
|
||||
fmt=kp_u;
|
||||
u64 value=u.UInt64;
|
||||
u.VoidPtr=&value;
|
||||
}
|
||||
else if(u.typeId==ktid_name(f32) || u.typeId==ktid_name(f64)){
|
||||
fmt=kp_f;
|
||||
f64 value=u.Float64;
|
||||
u.VoidPtr=&value;
|
||||
}
|
||||
else if(u.typeId==ktid_name(char)){
|
||||
fmt=kp_c;
|
||||
i64 value=u.Int64;
|
||||
u.VoidPtr=&value;
|
||||
}
|
||||
else if(u.typeId==ktid_ptrName(char)){
|
||||
char* value=u.VoidPtr;
|
||||
u.VoidPtr=&value;
|
||||
fmt=kp_s;
|
||||
}
|
||||
else if(u.typeId==ktid_name(Pointer)){
|
||||
if(u.VoidPtr==NULL)
|
||||
return cptr_copy("{ UniNull }");
|
||||
fmt=kp_h;
|
||||
}
|
||||
}
|
||||
|
||||
ktDescriptor* type=ktDescriptor_get(u.typeId);
|
||||
char* valuestr;
|
||||
if(type->toString)
|
||||
valuestr=type->toString(u.VoidPtr, fmt);
|
||||
else valuestr="ERR_NO_TOSTRING_FUNC";
|
||||
char* rezult=cptr_concat("{ type: ", type->name,
|
||||
", allocated on heap: ", (u.allocatedInHeap ? "true" : "false"),
|
||||
", value:", valuestr, " }");
|
||||
free(valuestr);
|
||||
if(type->toString)
|
||||
free(valuestr);
|
||||
return rezult;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define BUFSIZE 64
|
||||
char* sprintuni(Unitype v){
|
||||
char* buf=malloc(BUFSIZE);
|
||||
ktDescriptor type=ktDescriptor_get(v.typeId);
|
||||
if(v.typeId==ktid_Null)
|
||||
sprintf_s(buf, BUFSIZE, "{Null}");
|
||||
else if(v.typeId==ktid_name(f64))
|
||||
sprintf_s(buf, BUFSIZE, "{%s : %lf}", type.name,v.Float64);
|
||||
else if(v.typeId==ktid_name(bool) || v.typeId==ktid_name(u64))
|
||||
sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%llu", "%lu") "}", type.name,v.UInt64);
|
||||
else if(v.typeId==ktid_name(i64))
|
||||
sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%lld", "%ld") "}", type.name,v.Int64);
|
||||
else if(v.typeId==ktid_ptrName(char)){
|
||||
size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2;
|
||||
buf=realloc(buf, newBUFSIZE);
|
||||
sprintf_s(buf, BUFSIZE, "{%s : \"%s\"}", type.name,(char*)v.VoidPtr);
|
||||
}
|
||||
else sprintf_s(buf, BUFSIZE, "{%s : %p}", type.name,v.VoidPtr);
|
||||
return buf;
|
||||
}
|
||||
|
||||
void printuni(Unitype v){
|
||||
char* s=sprintuni(v);
|
||||
char* s=Unitype_toString(v,0);
|
||||
fputs(s, stdout);
|
||||
fputc('\n',stdout);
|
||||
free(s);
|
||||
}
|
||||
@ -5,8 +5,9 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
#include "ktid.h"
|
||||
#include "typedef_macros.h"
|
||||
|
||||
typedef struct Unitype{
|
||||
STRUCT(Unitype,
|
||||
union {
|
||||
i64 Int64;
|
||||
u64 UInt64;
|
||||
@ -17,11 +18,10 @@ typedef struct Unitype{
|
||||
};
|
||||
ktid typeId;
|
||||
bool allocatedInHeap; // should Unitype_free call free() to VoidPtr*
|
||||
} Unitype;
|
||||
ktid_declare(Unitype);
|
||||
)
|
||||
|
||||
|
||||
#define __UniDef(FIELD, TYPE, VAL) (Unitype){\
|
||||
#define __UniDef(FIELD, TYPE, VAL) (Unitype){ \
|
||||
.FIELD=VAL, .typeId=ktid_name(TYPE), .allocatedInHeap=false}
|
||||
|
||||
#define UniInt64(VAL) __UniDef(Int64, i64, VAL)
|
||||
@ -29,20 +29,23 @@ ktid_declare(Unitype);
|
||||
#define UniFloat64(VAL) __UniDef(Float64, f64, VAL)
|
||||
#define UniBool(VAL) __UniDef(Bool, bool, VAL)
|
||||
|
||||
#define UniStackPtr(TYPE, VAL) (Unitype){\
|
||||
#define UniStackPtr(TYPE, VAL) (Unitype){ \
|
||||
.VoidPtr=VAL, .typeId=ktid_ptrName(TYPE), .allocatedInHeap=false}
|
||||
#define UniHeapPtr(TYPE, VAL) (Unitype){\
|
||||
#define UniHeapPtr(TYPE, VAL) (Unitype){ \
|
||||
.VoidPtr=VAL, .typeId=ktid_ptrName(TYPE), .allocatedInHeap=true}
|
||||
|
||||
#define UniNull (Unitype){.Int64=0, .typeId=ktid_Null, .allocatedInHeap=false}
|
||||
// 0==ktid_Pointer
|
||||
#define UniNull (Unitype){.Int64=0, .typeId=0, .allocatedInHeap=false}
|
||||
#define UniTrue UniBool(true)
|
||||
#define UniFalse UniBool(false)
|
||||
|
||||
#define Unitype_isUniNull(UNI) (UNI.typeId==ktid_Pointer && UNI.VoidPtr==NULL)
|
||||
|
||||
// frees VoidPtr value or does nothing if type isn't pointer
|
||||
void Unitype_free(Unitype u);
|
||||
void __UnitypePtr_free(void* u);
|
||||
char* Unitype_toString(Unitype v, u32 fmt);
|
||||
void printuni(Unitype v);
|
||||
char* sprintuni(Unitype v);
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
|
||||
@ -47,4 +47,4 @@ I don't really like printf function (and its variants), so i made safer and more
|
||||
Maybe m=MaybeNull;
|
||||
kprint(kp_fgBlue|kp_s, "Maybe: ", kp_fgGreen|ktid_MaybePtr, &m);
|
||||
```
|
||||
output: <span style="color:blue">Maybe:</span> <span style="color:lightgreen">{value={0, ktid_Null}}</span>
|
||||
output: <span style="color:blue">Maybe:</span> <span style="color:lightgreen">{value: { Pointer, 0x0 }}</span>
|
||||
|
||||
@ -15,7 +15,7 @@ ktid __typeFromFormat(kp_fmt f){
|
||||
case kp_f:
|
||||
return ktid_name(f64);
|
||||
case kp_c:
|
||||
return ktid_char;
|
||||
return ktid_name(char);
|
||||
case kp_s:
|
||||
return ktid_ptrName(char);
|
||||
default:
|
||||
@ -26,12 +26,12 @@ ktid __typeFromFormat(kp_fmt f){
|
||||
Maybe __next_toString(kp_fmt f, __kp_value_union* object){
|
||||
// detecting type
|
||||
ktid typeId=__typeFromFormat(f);
|
||||
if(typeId==-1)
|
||||
safethrow("typeId is not set, can't autodetect type",;);
|
||||
ktDescriptor typeDesc=ktDescriptor_get(typeId);
|
||||
if(!typeDesc.toString)
|
||||
if(typeId==ktid_undefined)
|
||||
safethrow("typeId is undefined, can't autodetect type",;);
|
||||
ktDescriptor* type=ktDescriptor_get(typeId);
|
||||
if(!type->toString)
|
||||
safethrow("type descriptor doesnt have toString() func",;);
|
||||
return SUCCESS(UniHeapPtr(char, typeDesc.toString(object, f)));
|
||||
return SUCCESS(UniHeapPtr(char, type->toString(object, f)));
|
||||
}
|
||||
|
||||
Maybe check_argsN(u8 n){
|
||||
@ -73,7 +73,7 @@ void __kprint(u8 n, kp_fmt* formats, __kp_value_union* objects){
|
||||
kp_fmt fmt=formats[i];
|
||||
kprint_setColor(fmt);
|
||||
tryLast(__next_toString(fmt, &objects[i]),maybeStr);
|
||||
if(fputs(maybeStr.value.VoidPtr, stdout)==EOF)\
|
||||
if(fputs(maybeStr.value.VoidPtr, stdout)==EOF) \
|
||||
throw("can't write string to stdout");
|
||||
//, Unitype_free(maybeStr.value)
|
||||
Unitype_free(maybeStr.value);
|
||||
@ -160,14 +160,14 @@ void kprint_setColor(kp_fmt f){
|
||||
#endif
|
||||
|
||||
/* Maybe ksprint_ar(u32 count, kp_fmt format, ktid typeId, void* array){
|
||||
ktDescriptor typeDesc=ktDescriptor_get(format.typeId);
|
||||
if(!typeDesc.toString)
|
||||
ktDescriptor* type=ktDescriptor_get(format.typeId);
|
||||
if(!type->toString)
|
||||
safethrow("type descriptor doesnt have toString() func",;);
|
||||
StringBuilder* strb=StringBuilder_create();
|
||||
StringBuilder_append_char(strb, '[');
|
||||
for (u16 e=1; e<count; e++){
|
||||
StringBuilder_append_char(strb, ' ');
|
||||
char* elStr=typeDesc.toString(array+typeDesc.size*e, &format);
|
||||
char* elStr=type->toString(array+type->size*e, &format);
|
||||
StringBuilder_append_cptr(strb, elStr);
|
||||
StringBuilder_append_char(strb, ',');
|
||||
}
|
||||
|
||||
@ -29,45 +29,45 @@ static inline __kp_value_union __kpVU_i(i64 f) { return (__kp_value_union){ .i64
|
||||
|
||||
#define __kpVU(V) __kpVU_selectType(V)
|
||||
|
||||
#define __kp_argsToFmts8(\
|
||||
a0, a1, a2, a3, a4, a5, a6, a7,...)\
|
||||
#define __kp_argsToFmts8( \
|
||||
a0, a1, a2, a3, a4, a5, a6, a7,...) \
|
||||
((i32[]){ a0,a2,a4,a6 })
|
||||
#define __kp_argsToObjs8(\
|
||||
a0, a1, a2, a3, a4, a5, a6, a7,...)\
|
||||
#define __kp_argsToObjs8( \
|
||||
a0, a1, a2, a3, a4, a5, a6, a7,...) \
|
||||
((__kp_value_union[]){ __kpVU(a1),__kpVU(a3),__kpVU(a5),__kpVU(a7) })
|
||||
|
||||
#define __kp_argsToFmts16(\
|
||||
a0, a1, a2, a3, a4, a5, a6, a7,\
|
||||
a8, a9, a10,a11,a12,a13,a14,a15,...)\
|
||||
#define __kp_argsToFmts16( \
|
||||
a0, a1, a2, a3, a4, a5, a6, a7, \
|
||||
a8, a9, a10,a11,a12,a13,a14,a15,...) \
|
||||
((i32[]){ a0,a2,a4,a6,a8,a10,a12,a14 })
|
||||
#define __kp_argsToObjs16(\
|
||||
a0, a1, a2, a3, a4, a5, a6, a7,\
|
||||
a8, a9, a10,a11,a12,a13,a14,a15,...)\
|
||||
#define __kp_argsToObjs16( \
|
||||
a0, a1, a2, a3, a4, a5, a6, a7, \
|
||||
a8, a9, a10,a11,a12,a13,a14,a15,...) \
|
||||
((__kp_value_union[]){ __kpVU(a1),__kpVU(a3),__kpVU(a5),__kpVU(a7),__kpVU(a9),__kpVU(a11),__kpVU(a13),__kpVU(a15) })
|
||||
|
||||
#define __kp_argsToFmts32(\
|
||||
a0, a1, a2, a3, a4, a5, a6, a7,\
|
||||
a8, a9, a10,a11,a12,a13,a14,a15,\
|
||||
a16,a17,a18,a19,a20,a21,a22,a23,\
|
||||
a24,a25,a26,a27,a28,a29,a30,a31,...)\
|
||||
#define __kp_argsToFmts32( \
|
||||
a0, a1, a2, a3, a4, a5, a6, a7, \
|
||||
a8, a9, a10,a11,a12,a13,a14,a15, \
|
||||
a16,a17,a18,a19,a20,a21,a22,a23, \
|
||||
a24,a25,a26,a27,a28,a29,a30,a31,...) \
|
||||
((i32[]){ a0,a2,a4,a6,a8,a10,a12,a14,a16,a18,a20,a22,a24,a26,a28,a30 })
|
||||
#define __kp_argsToObjs32(\
|
||||
a0, a1, a2, a3, a4, a5, a6, a7,\
|
||||
a8, a9, a10,a11,a12,a13,a14,a15,\
|
||||
a16,a17,a18,a19,a20,a21,a22,a23,\
|
||||
a24,a25,a26,a27,a28,a29,a30,a31,...)\
|
||||
#define __kp_argsToObjs32( \
|
||||
a0, a1, a2, a3, a4, a5, a6, a7, \
|
||||
a8, a9, a10,a11,a12,a13,a14,a15, \
|
||||
a16,a17,a18,a19,a20,a21,a22,a23, \
|
||||
a24,a25,a26,a27,a28,a29,a30,a31,...) \
|
||||
((__kp_value_union[]){ __kpVU(a1),__kpVU(a3),__kpVU(a5),__kpVU(a7),__kpVU(a9),__kpVU(a11),__kpVU(a13),__kpVU(a15),__kpVU(a17),__kpVU(a19),__kpVU(a21),__kpVU(a23),__kpVU(a25),__kpVU(a27),__kpVU(a29),__kpVU(a31) })
|
||||
|
||||
#define __32zeroes 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
|
||||
|
||||
#define __kp_argsToArrs(COUNT,ARGS...)\
|
||||
(kp_fmt*)(\
|
||||
COUNT<=8 ? __kp_argsToFmts8(ARGS) :\
|
||||
COUNT<=16 ? __kp_argsToFmts16(ARGS) :\
|
||||
__kp_argsToFmts32(ARGS)),\
|
||||
(__kp_value_union*)(\
|
||||
COUNT<=8 ? __kp_argsToObjs8(ARGS) :\
|
||||
COUNT<=16 ? __kp_argsToObjs16(ARGS) :\
|
||||
#define __kp_argsToArrs(COUNT,ARGS...) \
|
||||
(kp_fmt*)( \
|
||||
COUNT<=8 ? __kp_argsToFmts8(ARGS) : \
|
||||
COUNT<=16 ? __kp_argsToFmts16(ARGS) : \
|
||||
__kp_argsToFmts32(ARGS)), \
|
||||
(__kp_value_union*)( \
|
||||
COUNT<=8 ? __kp_argsToObjs8(ARGS) : \
|
||||
COUNT<=16 ? __kp_argsToObjs16(ARGS) : \
|
||||
__kp_argsToObjs32(ARGS))
|
||||
|
||||
|
||||
@ -75,8 +75,8 @@ Maybe __ksprint(u8 n, kp_fmt* formats, __kp_value_union* objects);
|
||||
|
||||
/// @param ARGS kp_fmt, value, kp_fmt, value...
|
||||
///@returns Maybe<char*>
|
||||
#define ksprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION,\
|
||||
__ksprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes))\
|
||||
#define ksprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION, \
|
||||
__ksprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) \
|
||||
)
|
||||
/*-Wint-conversion warning was produced during value to __kp_value_union conversion*/
|
||||
|
||||
@ -85,8 +85,8 @@ Maybe __kfprint(FILE* fd, u8 n, kp_fmt* formats, __kp_value_union* objects);
|
||||
/// @param FD FILE*
|
||||
/// @param ARGS kp_fmt, value, kp_fmt, value...
|
||||
///@returns Maybe<void>
|
||||
#define kfprint(FD, ARGS...) WARNING_DISABLE( W_INT_CONVERSION,\
|
||||
__kfprint(FD, count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes))\
|
||||
#define kfprint(FD, ARGS...) WARNING_DISABLE( W_INT_CONVERSION, \
|
||||
__kfprint(FD, count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) \
|
||||
)
|
||||
|
||||
void __kprint(u8 n, kp_fmt* formats, __kp_value_union* objects);
|
||||
@ -94,8 +94,8 @@ void __kprint(u8 n, kp_fmt* formats, __kp_value_union* objects);
|
||||
///can use non-catchable throw !!!
|
||||
///@param ARGS kp_fmt, value, kp_fmt, value...
|
||||
///@returns void
|
||||
#define kprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION,\
|
||||
__kprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes))\
|
||||
#define kprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION, \
|
||||
__kprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) \
|
||||
)
|
||||
|
||||
///@param f bgColor | fgColor
|
||||
|
||||
@ -8,7 +8,7 @@ extern "C" {
|
||||
// ^ ^^^^
|
||||
// | color num
|
||||
// fgColorSet flag
|
||||
PACK_ENUM(kp_fgColor,
|
||||
PACKED_ENUM(kp_fgColor,
|
||||
/// black foreground
|
||||
kp_fgBlack = 0x80000000,
|
||||
/// dark red foreground
|
||||
@ -46,7 +46,7 @@ PACK_ENUM(kp_fgColor,
|
||||
// 01000000 00000000 00000000 00000000
|
||||
// ^ ^^^^
|
||||
// bgColorSet flag color num
|
||||
PACK_ENUM(kp_bgColor,
|
||||
PACKED_ENUM(kp_bgColor,
|
||||
/// black background
|
||||
kp_bgBlack = 0x40000000,
|
||||
/// dark red background
|
||||
|
||||
@ -10,7 +10,7 @@ extern "C" {
|
||||
/// kprint_format
|
||||
typedef u32 kp_fmt;
|
||||
|
||||
PACK_ENUM(kp_dataFmt,
|
||||
PACKED_ENUM(kp_dataFmt,
|
||||
// 00000000 00000000 00000000 00000000
|
||||
// ^^^^
|
||||
// type
|
||||
|
||||
@ -8,33 +8,33 @@
|
||||
|
||||
char data[]="iojihiojopijiugbjmoihftytryfdrh";
|
||||
|
||||
#define test_hashfunc(hasht, hashf)({\
|
||||
kprintf("\e[94mfunction: \e[92m" #hashf "\n");\
|
||||
hasht h=0;\
|
||||
optime("speed test", 1, ({\
|
||||
for(u32 i=0; i<SPEED_TESTS; i++)\
|
||||
h=hashf(h, data, sizeof(data));\
|
||||
}));\
|
||||
/*kprintf("\e[94mhash of \"\e[90m%s\e[94m\": \e[92m%x\n",data, h);*/\
|
||||
Autoarr(hasht)* hashes=Autoarr_create(hasht,512,32768);\
|
||||
optime("collision test",1,({\
|
||||
u32 collisions=0;\
|
||||
for(u64 i=0;i< COLLISION_TESTS;i++){\
|
||||
hasht h=hashb(hashf, (u8*)&i, sizeof(i));\
|
||||
bool col=false;\
|
||||
Autoarr_foreach(hashes,e,({\
|
||||
if(e==h) {\
|
||||
col=true;\
|
||||
break;\
|
||||
}\
|
||||
}));\
|
||||
if(col) collisions++;\
|
||||
else Autoarr_add(hashes,h);\
|
||||
}\
|
||||
kprintf("\e[93m%u \e[94mcollisions detected in %u hashes\n", collisions, COLLISION_TESTS);\
|
||||
}));\
|
||||
Autoarr_free(hashes, true);\
|
||||
kprintf("\e[96m--------------------------------------\n");\
|
||||
#define test_hashfunc(hasht, hashf)({ \
|
||||
kprintf("\e[94mfunction: \e[92m" #hashf "\n"); \
|
||||
hasht h=0; \
|
||||
optime("speed test", 1, ({ \
|
||||
for(u32 i=0; i<SPEED_TESTS; i++) \
|
||||
h=hashf(h, data, sizeof(data)); \
|
||||
})); \
|
||||
/*kprintf("\e[94mhash of \"\e[90m%s\e[94m\": \e[92m%x\n",data, h);*/ \
|
||||
Autoarr(hasht)* hashes=Autoarr_create(hasht,512,32768); \
|
||||
optime("collision test",1,({ \
|
||||
u32 collisions=0; \
|
||||
for(u64 i=0;i< COLLISION_TESTS;i++){ \
|
||||
hasht h=hashb(hashf, (u8*)&i, sizeof(i)); \
|
||||
bool col=false; \
|
||||
Autoarr_foreach(hashes,e,({ \
|
||||
if(e==h) { \
|
||||
col=true; \
|
||||
break; \
|
||||
} \
|
||||
})); \
|
||||
if(col) collisions++; \
|
||||
else Autoarr_add(hashes,h); \
|
||||
} \
|
||||
kprintf("\e[93m%u \e[94mcollisions detected in %u hashes\n", collisions, COLLISION_TESTS); \
|
||||
})); \
|
||||
Autoarr_free(hashes, true); \
|
||||
kprintf("\e[96m--------------------------------------\n"); \
|
||||
})
|
||||
|
||||
void test_hash_functions(){
|
||||
|
||||
@ -5,11 +5,11 @@
|
||||
#endif
|
||||
|
||||
#define testColor(COLOR) \
|
||||
kprint_setColor(kp_bgBlack|kp_fg##COLOR);\
|
||||
kprintf(#COLOR " ");\
|
||||
kprint_setColor(kp_bg##COLOR|kp_fgGray);\
|
||||
kprintf(#COLOR);\
|
||||
kprint_setColor(kp_bgBlack|kp_fgBlack);\
|
||||
kprint_setColor(kp_bgBlack|kp_fg##COLOR); \
|
||||
kprintf(#COLOR " "); \
|
||||
kprint_setColor(kp_bg##COLOR|kp_fgGray); \
|
||||
kprintf(#COLOR); \
|
||||
kprint_setColor(kp_bgBlack|kp_fgBlack); \
|
||||
kprintf("\n");
|
||||
|
||||
void test_kprint_colors(){
|
||||
|
||||
@ -2,21 +2,21 @@
|
||||
#include "../src/random/krandom.h"
|
||||
|
||||
|
||||
#define test_alg(ALG, VALUE_SIZE, EXPECTED_FROM_ZERO){\
|
||||
kprintf("\e[94mrng algorithm: \e[96m" #ALG "\n");\
|
||||
void* s= ALG##_init(0);\
|
||||
u##VALUE_SIZE r=ALG##_next(s);\
|
||||
kprintf("\e[97m next from zero seed:");\
|
||||
if(r!=EXPECTED_FROM_ZERO){\
|
||||
kprintf("\e[91m " IFWIN("%llu\n","%lu\n"), (u64)r);\
|
||||
throw(ERR_UNEXPECTEDVAL);\
|
||||
}\
|
||||
kprintf("\e[92m " IFWIN("%llu\n","%lu\n"), (u64)r);\
|
||||
ALG##_free(s);\
|
||||
s= ALG##_initFromTime();\
|
||||
r=ALG##_next(s);\
|
||||
ALG##_free(s);\
|
||||
kprintf("\e[97m next from time seed:\e[92m " IFWIN("%llu\n","%lu\n"), (u64)r);\
|
||||
#define test_alg(ALG, VALUE_SIZE, EXPECTED_FROM_ZERO){ \
|
||||
kprintf("\e[94mrng algorithm: \e[96m" #ALG "\n"); \
|
||||
void* s= ALG##_init(0); \
|
||||
u##VALUE_SIZE r=ALG##_next(s); \
|
||||
kprintf("\e[97m next from zero seed:"); \
|
||||
if(r!=EXPECTED_FROM_ZERO){ \
|
||||
kprintf("\e[91m " IFWIN("%llu\n","%lu\n"), (u64)r); \
|
||||
throw(ERR_UNEXPECTEDVAL); \
|
||||
} \
|
||||
kprintf("\e[92m " IFWIN("%llu\n","%lu\n"), (u64)r); \
|
||||
ALG##_free(s); \
|
||||
s= ALG##_initFromTime(); \
|
||||
r=ALG##_next(s); \
|
||||
ALG##_free(s); \
|
||||
kprintf("\e[97m next from time seed:\e[92m " IFWIN("%llu\n","%lu\n"), (u64)r); \
|
||||
}
|
||||
|
||||
void test_rng_algorithms(){
|
||||
|
||||
@ -2,8 +2,8 @@
|
||||
|
||||
void test_type_system(){
|
||||
for(ktid id=0; id<ktid_last; id++){
|
||||
ktDescriptor d=ktDescriptor_get(id);
|
||||
ktDescriptor* type=ktDescriptor_get(id);
|
||||
kprintf("\e[37m{ id:%u name:%s size:%u freeMembers:%p toString:%p }\n",
|
||||
d.id, d.name, d.size, d.freeMembers, d.toString);
|
||||
type->id, type->name, type->size, type->freeMembers, type->toString);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user