new type system from Timerix22/new_unitype
type descriptors and rework of unitype
This commit is contained in:
commit
f35dc7d146
3
!TODO.md
Normal file
3
!TODO.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
- add toString() to type descriptor
|
||||||
|
- find better way to check typeId without hundreds of if() statements
|
||||||
|
- update autoarr to use type descriptor
|
||||||
@ -6,8 +6,8 @@ CMP_C=gcc
|
|||||||
CMP_CPP=g++
|
CMP_CPP=g++
|
||||||
STD_C=c11
|
STD_C=c11
|
||||||
STD_CPP=c++17
|
STD_CPP=c++17
|
||||||
WARN_C="-Wall -Wno-discarded-qualifiers" #-Wextra
|
WARN_C="-Wall -Wno-discarded-qualifiers"
|
||||||
WARN_CPP="-Wall -Wno-unused-variable -Wno-return-type" #-Wextra
|
WARN_CPP="-Wall"
|
||||||
SRC_C="$( find src -name '*.c')"
|
SRC_C="$( find src -name '*.c')"
|
||||||
SRC_CPP="$( find src -name '*.cpp')"
|
SRC_CPP="$( find src -name '*.cpp')"
|
||||||
TESTS_C="$( find tests -name '*.c')"
|
TESTS_C="$( find tests -name '*.c')"
|
||||||
@ -21,7 +21,7 @@ BUILD_TEST_CPP_ARGS="$BUILD_TEST_C_ARGS"
|
|||||||
BUILD_TEST_LINKER_ARGS=""
|
BUILD_TEST_LINKER_ARGS=""
|
||||||
|
|
||||||
# build_test_dbg
|
# build_test_dbg
|
||||||
TEST_DBG_FILE=$TEST_FILE
|
TEST_DBG_FILE=$TEST_FILE.dbg
|
||||||
BUILD_TEST_DBG_C_ARGS="-O0 -g"
|
BUILD_TEST_DBG_C_ARGS="-O0 -g"
|
||||||
BUILD_TEST_DBG_CPP_ARGS="$BUILD_TEST_DBG_C_ARGS"
|
BUILD_TEST_DBG_CPP_ARGS="$BUILD_TEST_DBG_C_ARGS"
|
||||||
BUILD_TEST_DBG_LINKER_ARGS=""
|
BUILD_TEST_DBG_LINKER_ARGS=""
|
||||||
|
|||||||
@ -1,19 +1,51 @@
|
|||||||
#include "Autoarr.h"
|
#include "Autoarr.h"
|
||||||
|
|
||||||
Autoarr_define(uint8);
|
Autoarr_define(char)
|
||||||
Autoarr_define(int8);
|
Autoarr_define(bool)
|
||||||
Autoarr_define(uint16);
|
Autoarr_define(float32)
|
||||||
Autoarr_define(int16);
|
Autoarr_define(float64)
|
||||||
Autoarr_define(uint32);
|
Autoarr_define(uint8)
|
||||||
Autoarr_define(int32);
|
Autoarr_define(int8)
|
||||||
Autoarr_define(uint64);
|
Autoarr_define(uint16)
|
||||||
Autoarr_define(int64);
|
Autoarr_define(int16)
|
||||||
Autoarr_define(float);
|
Autoarr_define(uint32)
|
||||||
Autoarr_define(double);
|
Autoarr_define(int32)
|
||||||
Autoarr_define(Unitype);
|
Autoarr_define(uint64)
|
||||||
|
Autoarr_define(int64)
|
||||||
|
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrChar);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrBool);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrFloat32);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrFloat64);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrInt8);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrUInt8);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrInt16);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrUInt16);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrInt32);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrUInt32);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrInt64);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrUInt64);
|
||||||
|
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrCharPtr);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrBoolPtr);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrFloat32Ptr);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrFloat64Ptr);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrInt8Ptr);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrUInt8Ptr);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrInt16Ptr);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrUInt16Ptr);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrInt32Ptr);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrUInt32Ptr);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrInt64Ptr);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrUInt64Ptr);
|
||||||
|
|
||||||
|
Autoarr_define(Unitype)
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrUnitype);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrUnitypePtr);
|
||||||
|
|
||||||
// right func to clear array of unitype values
|
// right func to clear array of unitype values
|
||||||
void Autoarr_free_Unitype(Autoarr(Unitype)* ar){
|
void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr){
|
||||||
Autoarr_foreach(ar, u,Unitype_free(u));
|
Autoarr_foreach(ar, u,Unitype_free(u));
|
||||||
Autoarr_free(ar);
|
__Autoarr_free_Unitype(ar, freePtr);
|
||||||
}
|
}
|
||||||
|
void ____Autoarr_free_Unitype_(void* ar) { __Autoarr_free_Unitype_((Autoarr(Unitype)*)ar, false); }
|
||||||
@ -7,20 +7,52 @@ extern "C" {
|
|||||||
#include "Autoarr_declare.h"
|
#include "Autoarr_declare.h"
|
||||||
#include "Autoarr_define.h"
|
#include "Autoarr_define.h"
|
||||||
|
|
||||||
Autoarr_declare(uint8)
|
Autoarr_declare(char)
|
||||||
|
Autoarr_declare(bool)
|
||||||
|
Autoarr_declare(float32)
|
||||||
|
Autoarr_declare(float64)
|
||||||
Autoarr_declare(int8)
|
Autoarr_declare(int8)
|
||||||
Autoarr_declare(uint16)
|
Autoarr_declare(uint8)
|
||||||
Autoarr_declare(int16)
|
Autoarr_declare(int16)
|
||||||
Autoarr_declare(uint32)
|
Autoarr_declare(uint16)
|
||||||
Autoarr_declare(int32)
|
Autoarr_declare(int32)
|
||||||
Autoarr_declare(uint64)
|
Autoarr_declare(uint32)
|
||||||
Autoarr_declare(int64)
|
Autoarr_declare(int64)
|
||||||
Autoarr_declare(float)
|
Autoarr_declare(uint64)
|
||||||
Autoarr_declare(double)
|
|
||||||
Autoarr_declare(Unitype)
|
|
||||||
|
|
||||||
// right func to clear array of unitype values
|
kerepTypeId_declare(kerepTypeId_AutoarrChar);
|
||||||
void Autoarr_free_Unitype(Autoarr(Unitype)* ar);
|
kerepTypeId_declare(kerepTypeId_AutoarrBool);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrFloat32);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrFloat64);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrInt8);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrUInt8);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrInt16);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrUInt16);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrInt32);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrUInt32);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrInt64);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrUInt64);
|
||||||
|
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrCharPtr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrBoolPtr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrFloat32Ptr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrFloat64Ptr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrInt8Ptr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrUInt8Ptr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrInt16Ptr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrUInt16Ptr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrInt32Ptr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrUInt32Ptr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrInt64Ptr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrUInt64Ptr);
|
||||||
|
|
||||||
|
Autoarr_declare(Unitype)
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrUnitype);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrUnitypePtr);
|
||||||
|
|
||||||
|
// this function is injected in kerep_init()
|
||||||
|
void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr);
|
||||||
|
void ____Autoarr_free_Unitype_(void* ar);
|
||||||
|
|
||||||
#define Autoarr_foreach(ar,elem,codeblock)({\
|
#define Autoarr_foreach(ar,elem,codeblock)({\
|
||||||
if(ar->blocks_count>0) {\
|
if(ar->blocks_count>0) {\
|
||||||
|
|||||||
@ -10,7 +10,7 @@ EXPORT void CALL kerep_Autoarr_KVPair_create(uint16 max_blocks_count, uint16 max
|
|||||||
}
|
}
|
||||||
|
|
||||||
EXPORT void CALL kerep_Autoarr_KVPair_free(Autoarr_KVPair* ar){
|
EXPORT void CALL kerep_Autoarr_KVPair_free(Autoarr_KVPair* ar){
|
||||||
Autoarr_free_KVPair(ar);
|
Autoarr_free(ar, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
EXPORT void CALL kerep_Autoarr_KVPair_get(Autoarr_KVPair* ar, uint32 index, KVPair* output){
|
EXPORT void CALL kerep_Autoarr_KVPair_get(Autoarr_KVPair* ar, uint32 index, KVPair* output){
|
||||||
|
|||||||
@ -9,7 +9,7 @@ EXPORT void CALL kerep_Autoarr_Unitype_create(uint16 max_blocks_count, uint16 ma
|
|||||||
}
|
}
|
||||||
|
|
||||||
EXPORT void CALL kerep_Autoarr_Unitype_free(Autoarr_Unitype* ar){
|
EXPORT void CALL kerep_Autoarr_Unitype_free(Autoarr_Unitype* ar){
|
||||||
Autoarr_free_Unitype(ar);
|
Autoarr_free(ar, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
EXPORT void CALL kerep_Autoarr_Unitype_get(Autoarr_Unitype* ar, uint32 index, Unitype* output){
|
EXPORT void CALL kerep_Autoarr_Unitype_get(Autoarr_Unitype* ar, uint32 index, Unitype* output){
|
||||||
|
|||||||
@ -15,7 +15,8 @@ typedef struct {\
|
|||||||
type (*get)(struct Autoarr_##type* ar, uint32 index);\
|
type (*get)(struct Autoarr_##type* ar, uint32 index);\
|
||||||
type* (*getptr)(struct Autoarr_##type* ar, uint32 index);\
|
type* (*getptr)(struct Autoarr_##type* ar, uint32 index);\
|
||||||
void (*set)(struct Autoarr_##type* ar, uint32 index, type element);\
|
void (*set)(struct Autoarr_##type* ar, uint32 index, type element);\
|
||||||
void (*_free)(struct Autoarr_##type* ar);\
|
void (*freear)(struct Autoarr_##type* ar, bool freePtr);\
|
||||||
|
type* (*toArray)(struct Autoarr_##type* ar);\
|
||||||
} __functions_list_t_##type;\
|
} __functions_list_t_##type;\
|
||||||
\
|
\
|
||||||
typedef struct Autoarr_##type{\
|
typedef struct Autoarr_##type{\
|
||||||
@ -27,15 +28,14 @@ typedef struct Autoarr_##type{\
|
|||||||
__functions_list_t_##type* functions;\
|
__functions_list_t_##type* functions;\
|
||||||
} Autoarr_##type;\
|
} Autoarr_##type;\
|
||||||
\
|
\
|
||||||
void __Autoarr_add_##type(Autoarr_##type* ar, type element);\
|
Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length);\
|
||||||
type __Autoarr_get_##type(Autoarr_##type* ar, uint32 index);\
|
void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr);\
|
||||||
type* __Autoarr_getptr_##type(Autoarr_##type* ar, uint32 index);\
|
void ____Autoarr_free_##type(void* ar);
|
||||||
void __Autoarr_set_##type(Autoarr_##type* ar, uint32 index, type element);\
|
|
||||||
void __Autoarr_free_##type(Autoarr_##type* ar);\
|
|
||||||
Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length);
|
|
||||||
|
|
||||||
#define Autoarr(type) Autoarr_##type
|
#define Autoarr(type) Autoarr_##type
|
||||||
|
|
||||||
|
#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)
|
autoarr->functions->add(autoarr, element)
|
||||||
#define Autoarr_get(autoarr, index)\
|
#define Autoarr_get(autoarr, index)\
|
||||||
@ -44,10 +44,10 @@ Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_bloc
|
|||||||
autoarr->functions->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)
|
autoarr->functions->set(autoarr, index, element)
|
||||||
#define Autoarr_free(autoarr)\
|
#define Autoarr_free(autoarr, freePtr)\
|
||||||
autoarr->functions->_free(autoarr)
|
autoarr->functions->freear(autoarr, freePtr)
|
||||||
#define Autoarr_create(type, max_blocks_count, max_block_length)\
|
#define Autoarr_toArray(autoarr)\
|
||||||
__Autoarr_create_##type(max_blocks_count, max_block_length)
|
autoarr->functions->toArray(autoarr)
|
||||||
|
|
||||||
#define Autoarr_length(autoarr) \
|
#define Autoarr_length(autoarr) \
|
||||||
(uint32)(!autoarr->blocks_count ? 0 : \
|
(uint32)(!autoarr->blocks_count ? 0 : \
|
||||||
|
|||||||
@ -39,11 +39,22 @@ void __Autoarr_set_##type(Autoarr_##type* ar, uint32 index, type element){\
|
|||||||
ar->values[index/ar->max_block_length][index%ar->max_block_length]=element;\
|
ar->values[index/ar->max_block_length][index%ar->max_block_length]=element;\
|
||||||
}\
|
}\
|
||||||
\
|
\
|
||||||
void __Autoarr_free_##type(Autoarr_##type* ar){\
|
void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr){\
|
||||||
for(uint16 i=0; i<ar->blocks_count;i++)\
|
for(uint16 i=0; i<ar->blocks_count;i++)\
|
||||||
free(ar->values[i]); \
|
free(ar->values[i]); \
|
||||||
free(ar->values);\
|
free(ar->values);\
|
||||||
free(ar);\
|
if(freePtr) free(ar);\
|
||||||
|
}\
|
||||||
|
void ____Autoarr_free_##type(void* ar){\
|
||||||
|
__Autoarr_free_##type((Autoarr_##type*)ar, false);\
|
||||||
|
}\
|
||||||
|
\
|
||||||
|
type* __Autoarr_toArray_##type(Autoarr_##type* ar){\
|
||||||
|
uint32 length=Autoarr_length(ar);\
|
||||||
|
type* array=malloc(length * sizeof(type));\
|
||||||
|
for(uint32 i=0; i<length; i++)\
|
||||||
|
array[i]=__Autoarr_get_##type(ar, i);\
|
||||||
|
return array;\
|
||||||
}\
|
}\
|
||||||
\
|
\
|
||||||
__functions_list_t_##type __functions_list_##type={\
|
__functions_list_t_##type __functions_list_##type={\
|
||||||
@ -51,7 +62,8 @@ __functions_list_t_##type __functions_list_##type={\
|
|||||||
&__Autoarr_get_##type,\
|
&__Autoarr_get_##type,\
|
||||||
&__Autoarr_getptr_##type,\
|
&__Autoarr_getptr_##type,\
|
||||||
&__Autoarr_set_##type,\
|
&__Autoarr_set_##type,\
|
||||||
&__Autoarr_free_##type\
|
&__Autoarr_free_##type,\
|
||||||
|
&__Autoarr_toArray_##type\
|
||||||
};\
|
};\
|
||||||
\
|
\
|
||||||
Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length){\
|
Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length){\
|
||||||
|
|||||||
@ -98,9 +98,9 @@ Maybe __ReadName(DeserializeSharedData* shared){
|
|||||||
case '}':
|
case '}':
|
||||||
if(!calledRecursively || nameStr.length!=0)
|
if(!calledRecursively || nameStr.length!=0)
|
||||||
safethrow_wrongchar(c,;);
|
safethrow_wrongchar(c,;);
|
||||||
return SUCCESS(UniPtr(CharPtr,NULL));
|
return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr,NULL));
|
||||||
case ':':
|
case ':':
|
||||||
return SUCCESS(UniPtr(CharPtr,string_extract(nameStr)));
|
return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr,string_extract(nameStr)));
|
||||||
case '$':
|
case '$':
|
||||||
if(nameStr.length!=0)
|
if(nameStr.length!=0)
|
||||||
safethrow_wrongchar(c,;);
|
safethrow_wrongchar(c,;);
|
||||||
@ -113,7 +113,7 @@ Maybe __ReadName(DeserializeSharedData* shared){
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(nameStr.length>0) safethrow(ERR_ENDOFSTR,;);
|
if(nameStr.length>0) safethrow(ERR_ENDOFSTR,;);
|
||||||
return SUCCESS(UniPtr(CharPtr,NULL));
|
return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr,NULL));
|
||||||
}
|
}
|
||||||
#define ReadName() __ReadName(shared)
|
#define ReadName() __ReadName(shared)
|
||||||
|
|
||||||
@ -137,7 +137,7 @@ Maybe __ReadString(DeserializeSharedData* shared){
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
char* str=StringBuilder_build(b).ptr;
|
char* str=StringBuilder_build(b).ptr;
|
||||||
return SUCCESS(UniPtr(CharPtr,str));
|
return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr,str));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -154,16 +154,16 @@ Maybe __ReadList(DeserializeSharedData* shared){
|
|||||||
Autoarr(Unitype)* list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
|
Autoarr(Unitype)* list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
|
||||||
bool readingList=true;
|
bool readingList=true;
|
||||||
while (true){
|
while (true){
|
||||||
try(ReadValue((&readingList)), val, Autoarr_free_Unitype(list))
|
try(ReadValue((&readingList)), val, Autoarr_free(list, true))
|
||||||
Autoarr_add(list,val.value);
|
Autoarr_add(list,val.value);
|
||||||
if (!readingList){
|
if (!readingList){
|
||||||
if(val.value.type==Null)
|
if(val.value.typeId==kerepTypeId_Null)
|
||||||
Autoarr_pop(list);
|
Autoarr_pop(list);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return SUCCESS(UniPtr(AutoarrUnitypePtr,list));
|
return SUCCESS(UniPtrHeap(kerepTypeId_AutoarrUnitypePtr,list));
|
||||||
};
|
};
|
||||||
#define ReadList() __ReadList(shared)
|
#define ReadList() __ReadList(shared)
|
||||||
|
|
||||||
@ -275,7 +275,7 @@ Maybe __ReadValue(DeserializeSharedData* shared, bool* readingList){
|
|||||||
case ';':
|
case ';':
|
||||||
case ',':
|
case ',':
|
||||||
if(valueStr.length!=0){
|
if(valueStr.length!=0){
|
||||||
if(value.type!=Null)
|
if(value.typeId!=kerepTypeId_Null)
|
||||||
safethrow_wrongchar(c,Unitype_free(value));
|
safethrow_wrongchar(c,Unitype_free(value));
|
||||||
try(ParseValue(valueStr),maybeParsed,;)
|
try(ParseValue(valueStr),maybeParsed,;)
|
||||||
value=maybeParsed.value;
|
value=maybeParsed.value;
|
||||||
@ -321,7 +321,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively) {
|
|||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
|
list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
|
||||||
Hashtable_add(dict,nameCPtr,UniPtr(AutoarrUnitypePtr,list));
|
Hashtable_add(dict,nameCPtr,UniPtrHeap(kerepTypeId_AutoarrUnitypePtr,list));
|
||||||
}
|
}
|
||||||
Autoarr_add(list,val.value);
|
Autoarr_add(list,val.value);
|
||||||
}
|
}
|
||||||
@ -331,7 +331,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively) {
|
|||||||
|
|
||||||
END:
|
END:
|
||||||
*_text=text;
|
*_text=text;
|
||||||
return SUCCESS(UniPtr(HashtablePtr,dict));
|
return SUCCESS(UniPtrHeap(kerepTypeId_HashtablePtr,dict));
|
||||||
}
|
}
|
||||||
|
|
||||||
Maybe DtsodV24_deserialize(char* _text) {
|
Maybe DtsodV24_deserialize(char* _text) {
|
||||||
|
|||||||
@ -23,70 +23,65 @@ void __AppendTabs(SerializeSharedData* shared) {
|
|||||||
Maybe __AppendValue(SerializeSharedData* shared, Unitype u);
|
Maybe __AppendValue(SerializeSharedData* shared, Unitype u);
|
||||||
#define AppendValue(UNI) __AppendValue(shared, UNI)
|
#define AppendValue(UNI) __AppendValue(shared, UNI)
|
||||||
Maybe __AppendValue(SerializeSharedData* shared, Unitype u){
|
Maybe __AppendValue(SerializeSharedData* shared, Unitype u){
|
||||||
switch(u.type){
|
if(u.typeId==kerepTypeId_Int64){
|
||||||
case Int64:
|
StringBuilder_append_int64(b,u.Int64);
|
||||||
StringBuilder_append_int64(b,u.Int64);
|
}
|
||||||
break;
|
else if(u.typeId==kerepTypeId_UInt64){
|
||||||
case UInt64:
|
StringBuilder_append_uint64(b,u.UInt64);
|
||||||
StringBuilder_append_uint64(b,u.UInt64);
|
addc('u');
|
||||||
addc('u');
|
}
|
||||||
break;
|
else if(u.typeId==kerepTypeId_Float64){
|
||||||
case Float64:
|
StringBuilder_append_float64(b,u.Float64);
|
||||||
StringBuilder_append_float64(b,u.Float64);
|
addc('f');
|
||||||
addc('f');
|
}
|
||||||
break;
|
else if(u.typeId==kerepTypeId_CharPtr){
|
||||||
case CharPtr:
|
addc('"');
|
||||||
addc('"');
|
char c;
|
||||||
char c;
|
while((c=*(char*)(u.VoidPtr++))){
|
||||||
while((c=*(char*)(u.VoidPtr++))){
|
if(c=='\"') addc('\\');
|
||||||
if(c=='\"') addc('\\');
|
addc(c);
|
||||||
addc(c);
|
}
|
||||||
}
|
addc('"');
|
||||||
addc('"');
|
}
|
||||||
break;
|
else if(u.typeId==kerepTypeId_Bool){
|
||||||
case Bool:
|
StringBuilder_append_cptr(b, u.Bool ? "true" : "false");
|
||||||
StringBuilder_append_cptr(b, u.Bool ? "true" : "false");
|
}
|
||||||
break;
|
else if(u.typeId==kerepTypeId_Null){
|
||||||
case Null:
|
safethrow("Null isn't supported in DtsodV24",;);
|
||||||
safethrow("Null isn't supported in DtsodV24",;);
|
}
|
||||||
case AutoarrUnitypePtr:
|
else if(u.typeId==kerepTypeId_AutoarrUnitypePtr){
|
||||||
if(Autoarr_length(((Autoarr_Unitype*)(u.VoidPtr)))){
|
if(Autoarr_length(((Autoarr_Unitype*)(u.VoidPtr)))){
|
||||||
|
addc('\n');
|
||||||
|
AppendTabs();
|
||||||
|
addc('[');
|
||||||
|
tabs++;
|
||||||
|
Autoarr_foreach(((Autoarr_Unitype*)(u.VoidPtr)), e, ({
|
||||||
addc('\n');
|
addc('\n');
|
||||||
AppendTabs();
|
AppendTabs();
|
||||||
addc('[');
|
try(AppendValue(e),__,;);
|
||||||
tabs++;
|
addc(',');
|
||||||
Autoarr_foreach(((Autoarr_Unitype*)(u.VoidPtr)), e, ({
|
|
||||||
addc('\n');
|
|
||||||
AppendTabs();
|
|
||||||
try(AppendValue(e),__,;);
|
|
||||||
addc(',');
|
|
||||||
}));
|
|
||||||
StringBuilder_rmchar(b);
|
|
||||||
addc('\n');
|
|
||||||
tabs--;
|
|
||||||
AppendTabs();
|
|
||||||
addc(']');
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
addc('[');
|
|
||||||
addc(']');
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case HashtablePtr:
|
|
||||||
// check hashtable is blank
|
|
||||||
Hashtable_foreach(((Hashtable*)u.VoidPtr), __, ({
|
|
||||||
goto hashtableNotBlank;
|
|
||||||
if(__.key); // weird way to disable warning
|
|
||||||
}));
|
}));
|
||||||
|
StringBuilder_rmchar(b);
|
||||||
|
addc('\n');
|
||||||
// blank hashtable
|
tabs--;
|
||||||
addc('{');
|
AppendTabs();
|
||||||
addc('}');
|
addc(']');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
addc('[');
|
||||||
|
addc(']');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(u.typeId==kerepTypeId_HashtablePtr){
|
||||||
|
// check hashtable is blank
|
||||||
|
bool hashtableNotBlank=false;
|
||||||
|
Hashtable_foreach(((Hashtable*)u.VoidPtr), __, ({
|
||||||
|
hashtableNotBlank=true;
|
||||||
|
if(__.key); // weird way to disable warning
|
||||||
break;
|
break;
|
||||||
|
}));
|
||||||
// not blank hashtable
|
|
||||||
hashtableNotBlank:
|
if(hashtableNotBlank){
|
||||||
addc('\n');
|
addc('\n');
|
||||||
AppendTabs();
|
AppendTabs();
|
||||||
addc('{');
|
addc('{');
|
||||||
@ -94,10 +89,16 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){
|
|||||||
try(__serialize(b,tabs+1,u.VoidPtr),___,;);
|
try(__serialize(b,tabs+1,u.VoidPtr),___,;);
|
||||||
AppendTabs();
|
AppendTabs();
|
||||||
addc('}');
|
addc('}');
|
||||||
break;
|
}
|
||||||
default: dbg((u.type)); safethrow(ERR_WRONGTYPE,;);
|
else {
|
||||||
|
addc('{');
|
||||||
|
addc('}');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
dbg((u.typeId));
|
||||||
|
safethrow(ERR_WRONGTYPE,;);
|
||||||
}
|
}
|
||||||
|
|
||||||
return MaybeNull;
|
return MaybeNull;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -125,5 +126,5 @@ Maybe DtsodV24_serialize(Hashtable* dtsod){
|
|||||||
StringBuilder* sb=StringBuilder_create();
|
StringBuilder* sb=StringBuilder_create();
|
||||||
try(__serialize(sb,0,dtsod),__, StringBuilder_free(sb));
|
try(__serialize(sb,0,dtsod),__, StringBuilder_free(sb));
|
||||||
char* str=StringBuilder_build(sb).ptr;
|
char* str=StringBuilder_build(sb).ptr;
|
||||||
return SUCCESS(UniPtr(CharPtr, str));
|
return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr, str));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
#include "Hashtable.h"
|
#include "Hashtable.h"
|
||||||
|
|
||||||
|
kerepTypeId_define(kerepTypeId_Hashtable);
|
||||||
|
kerepTypeId_define(kerepTypeId_HashtablePtr);
|
||||||
|
|
||||||
// amount of rows
|
// amount of rows
|
||||||
static const uint16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521};
|
static const uint16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521};
|
||||||
#define HT_HEIN_MIN 0
|
#define HT_HEIN_MIN 0
|
||||||
@ -17,10 +20,14 @@ Hashtable* Hashtable_create(){
|
|||||||
return ht;
|
return ht;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Hashtable_free(Hashtable* ht){
|
void __Hashtable_free(void* _ht){
|
||||||
|
Hashtable* ht=_ht;
|
||||||
for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++)
|
for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++)
|
||||||
Autoarr_free_KVPair(ht->rows[i]);
|
Autoarr_free(ht->rows[i], true);
|
||||||
free(ht->rows);
|
free(ht->rows);
|
||||||
|
}
|
||||||
|
void Hashtable_free(Hashtable* ht){
|
||||||
|
__Hashtable_free(ht);
|
||||||
free(ht);
|
free(ht);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,7 +50,8 @@ void Hashtable_expand(Hashtable* ht){
|
|||||||
Autoarr(KVPair)* newar=newrows[newrown];
|
Autoarr(KVPair)* newar=newrows[newrown];
|
||||||
Autoarr_add(newar,p);
|
Autoarr_add(newar,p);
|
||||||
}
|
}
|
||||||
Autoarr_free(ar);
|
// there is no need to free array values, because they are copied into new array
|
||||||
|
__Autoarr_free_KVPair(ar, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(ht->rows);
|
free(ht->rows);
|
||||||
@ -88,7 +96,7 @@ Unitype Hashtable_get(Hashtable* ht, char* key){
|
|||||||
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){
|
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){
|
||||||
Unitype u=Hashtable_get(ht,key);
|
Unitype u=Hashtable_get(ht,key);
|
||||||
*output=u;
|
*output=u;
|
||||||
return u.type!=Null;
|
return u.typeId!=kerepTypeId_Null;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u){
|
void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u){
|
||||||
|
|||||||
@ -11,9 +11,12 @@ typedef struct Hashtable{
|
|||||||
uint8 hein; // height=HT_HEIGHTS[hein]
|
uint8 hein; // height=HT_HEIGHTS[hein]
|
||||||
Autoarr(KVPair)** rows; // Autoarr[height]
|
Autoarr(KVPair)** rows; // Autoarr[height]
|
||||||
} Hashtable;
|
} Hashtable;
|
||||||
|
kerepTypeId_declare(kerepTypeId_Hashtable);
|
||||||
|
kerepTypeId_declare(kerepTypeId_HashtablePtr);
|
||||||
|
|
||||||
Hashtable* Hashtable_create();
|
Hashtable* Hashtable_create();
|
||||||
void Hashtable_free(Hashtable* ht);
|
void Hashtable_free(Hashtable* ht);
|
||||||
|
void __Hashtable_free(void* ht);
|
||||||
|
|
||||||
// amount of rows
|
// amount of rows
|
||||||
uint16 Hashtable_height(Hashtable* ht);
|
uint16 Hashtable_height(Hashtable* ht);
|
||||||
|
|||||||
@ -1,18 +1,26 @@
|
|||||||
#include "KeyValuePair.h"
|
#include "KeyValuePair.h"
|
||||||
|
|
||||||
Autoarr_define(KVPair)
|
kerepTypeId_define(kerepTypeId_KVPair);
|
||||||
|
kerepTypeId_define(kerepTypeId_KVPairPtr);
|
||||||
|
|
||||||
|
Autoarr_define(KVPair)
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrKVPair);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrKVPairPtr);
|
||||||
|
|
||||||
// proper way to clear a KVP
|
// proper way to clear a KVP
|
||||||
void KVPair_free(KVPair p){
|
void KVPair_free(KVPair p){
|
||||||
free(p.key);
|
free(p.key);
|
||||||
Unitype_free(p.value);
|
Unitype_free(p.value);
|
||||||
}
|
}
|
||||||
|
void __KVPair_free(void* p){ KVPair_free(*(KVPair*)p); }
|
||||||
|
|
||||||
// func for KVP array clearing
|
// func for KVP array clearing
|
||||||
void Autoarr_free_KVPair(Autoarr_KVPair* ar){
|
void __Autoarr_free_KVPair_(Autoarr_KVPair* ar, bool freePtr){
|
||||||
Autoarr_foreach(ar,k,KVPair_free(k));
|
Autoarr_foreach(ar,k,KVPair_free(k));
|
||||||
Autoarr_free(ar);
|
__Autoarr_free_KVPair(ar, freePtr);
|
||||||
|
}
|
||||||
|
void ____Autoarr_free_KVPair_(void* ar){
|
||||||
|
__Autoarr_free_KVPair_((Autoarr_KVPair*)ar, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void printkvp(KVPair p){
|
void printkvp(KVPair p){
|
||||||
|
|||||||
@ -11,14 +11,20 @@ typedef struct KVPair{
|
|||||||
char* key;
|
char* key;
|
||||||
Unitype value;
|
Unitype value;
|
||||||
} KVPair;
|
} KVPair;
|
||||||
|
kerepTypeId_declare(kerepTypeId_KVPair);
|
||||||
|
kerepTypeId_declare(kerepTypeId_KVPairPtr);
|
||||||
|
|
||||||
Autoarr_declare(KVPair)
|
Autoarr_declare(KVPair)
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrKVPair);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrKVPairPtr);
|
||||||
|
|
||||||
// proper way to clear a KVP
|
// proper way to clear a KVP
|
||||||
void KVPair_free(KVPair p);
|
void KVPair_free(KVPair p);
|
||||||
|
void __KVPair_free(void* p);
|
||||||
|
|
||||||
// func to clear KVP array
|
// func to clear KVP array
|
||||||
void Autoarr_free_KVPair(Autoarr_KVPair* ar);
|
void __Autoarr_free_KVPair_(Autoarr_KVPair* ar, bool freePtr);
|
||||||
|
void ____Autoarr_free_KVPair_(void* ar);
|
||||||
|
|
||||||
void printkvp(KVPair p);
|
void printkvp(KVPair p);
|
||||||
|
|
||||||
|
|||||||
@ -1,14 +1,18 @@
|
|||||||
#include "SearchTree.h"
|
#include "SearchTree.h"
|
||||||
|
|
||||||
|
kerepTypeId_define(kerepTypeId_STNode);
|
||||||
|
kerepTypeId_define(kerepTypeId_STNodePtr);
|
||||||
|
|
||||||
STNode* STNode_create(){
|
STNode* STNode_create(){
|
||||||
STNode* node=malloc(sizeof(STNode));
|
STNode* node=malloc(sizeof(STNode));
|
||||||
node->branches=NULL;
|
node->branches=NULL;
|
||||||
node->value.type=Null;
|
node->value.typeId=kerepTypeId_Null;
|
||||||
node->value.UInt64=0;
|
node->value.UInt64=0;
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
void STNode_free(STNode* node){
|
void __STNode_free(void* _node){
|
||||||
|
STNode* node=_node;
|
||||||
if (!node) throw(ERR_NULLPTR);
|
if (!node) throw(ERR_NULLPTR);
|
||||||
if(node->branches){
|
if(node->branches){
|
||||||
for(uint8 n32 = 0;n32<8;n32++){
|
for(uint8 n32 = 0;n32<8;n32++){
|
||||||
@ -32,6 +36,9 @@ void STNode_free(STNode* node){
|
|||||||
}
|
}
|
||||||
if(node->value.VoidPtr)
|
if(node->value.VoidPtr)
|
||||||
Unitype_free(node->value);
|
Unitype_free(node->value);
|
||||||
|
}
|
||||||
|
void STNode_free(STNode* node){
|
||||||
|
__STNode_free(node);
|
||||||
free(node);
|
free(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -11,9 +11,12 @@ typedef struct SearchTreeNode{
|
|||||||
struct SearchTreeNode**** branches; // *STNode[8][8][4]
|
struct SearchTreeNode**** branches; // *STNode[8][8][4]
|
||||||
Unitype value;
|
Unitype value;
|
||||||
} STNode;
|
} STNode;
|
||||||
|
kerepTypeId_declare(kerepTypeId_STNode);
|
||||||
|
kerepTypeId_declare(kerepTypeId_STNodePtr);
|
||||||
|
|
||||||
STNode* STNode_create();
|
STNode* STNode_create();
|
||||||
void STNode_free(STNode* node);
|
void STNode_free(STNode* node);
|
||||||
|
void __STNode_free(void* node);
|
||||||
|
|
||||||
void ST_push(STNode* node, char* key, Unitype value);
|
void ST_push(STNode* node, char* key, Unitype value);
|
||||||
void ST_pushString(STNode* node, string key, Unitype value);
|
void ST_pushString(STNode* node, string key, Unitype value);
|
||||||
|
|||||||
@ -1,6 +1,11 @@
|
|||||||
#include "StringBuilder.h"
|
#include "StringBuilder.h"
|
||||||
|
|
||||||
Autoarr_define(string)
|
Autoarr_define(string)
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrString);
|
||||||
|
kerepTypeId_define(kerepTypeId_AutoarrStringPtr);
|
||||||
|
|
||||||
|
kerepTypeId_define(kerepTypeId_StringBuilder);
|
||||||
|
kerepTypeId_define(kerepTypeId_StringBuilderPtr);
|
||||||
|
|
||||||
#define BL_C 32
|
#define BL_C 32
|
||||||
#define BL_L 1024
|
#define BL_L 1024
|
||||||
@ -17,7 +22,7 @@ void complete_buf(StringBuilder* b){
|
|||||||
str.ptr[i++]=c;
|
str.ptr[i++]=c;
|
||||||
}));
|
}));
|
||||||
Autoarr_add(b->compl_bufs,str);
|
Autoarr_add(b->compl_bufs,str);
|
||||||
Autoarr_free(b->curr_buf);
|
Autoarr_free(b->curr_buf, true);
|
||||||
b->curr_buf=Autoarr_create(int8,BL_C,BL_L);
|
b->curr_buf=Autoarr_create(int8,BL_C,BL_L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,9 +39,13 @@ StringBuilder* StringBuilder_create(){
|
|||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void __StringBuilder_free(void* _b){
|
||||||
|
StringBuilder* b=_b;
|
||||||
|
if(b->compl_bufs) Autoarr_free(b->compl_bufs, true);
|
||||||
|
Autoarr_free(b->curr_buf, true);
|
||||||
|
}
|
||||||
void StringBuilder_free(StringBuilder* b){
|
void StringBuilder_free(StringBuilder* b){
|
||||||
if(b->compl_bufs) Autoarr_free(b->compl_bufs);
|
__StringBuilder_free(b);
|
||||||
Autoarr_free(b->curr_buf);
|
|
||||||
free(b);
|
free(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -8,14 +8,19 @@ extern "C" {
|
|||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
|
||||||
Autoarr_declare(string)
|
Autoarr_declare(string)
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrString);
|
||||||
|
kerepTypeId_declare(kerepTypeId_AutoarrStringPtr);
|
||||||
|
|
||||||
typedef struct StringBuilder{
|
typedef struct StringBuilder{
|
||||||
Autoarr(string)* compl_bufs;
|
Autoarr(string)* compl_bufs;
|
||||||
Autoarr(int8)* curr_buf;
|
Autoarr(int8)* curr_buf;
|
||||||
} StringBuilder;
|
} StringBuilder;
|
||||||
|
kerepTypeId_declare(kerepTypeId_StringBuilder);
|
||||||
|
kerepTypeId_declare(kerepTypeId_StringBuilderPtr);
|
||||||
|
|
||||||
StringBuilder* StringBuilder_create(void);
|
StringBuilder* StringBuilder_create(void);
|
||||||
void StringBuilder_free(StringBuilder* b);
|
void StringBuilder_free(StringBuilder* b);
|
||||||
|
void __StringBuilder_free(void* b);
|
||||||
// Joins all strings from compl_bufs.
|
// Joins all strings from compl_bufs.
|
||||||
// Returns zero-terminated string.
|
// Returns zero-terminated string.
|
||||||
// No need to call string_extract()!
|
// No need to call string_extract()!
|
||||||
|
|||||||
@ -5,10 +5,12 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "std.h"
|
#include "std.h"
|
||||||
#include "types.h"
|
|
||||||
#include "errors.h"
|
#include "errors.h"
|
||||||
#include "cptr.h"
|
#include "cptr.h"
|
||||||
#include "optime.h"
|
#include "optime.h"
|
||||||
|
#include "types.h"
|
||||||
|
#include "unitype.h"
|
||||||
|
#include "init.h"
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,7 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "std.h"
|
#include "std.h"
|
||||||
#include "types.h"
|
#include "unitype.h"
|
||||||
|
|
||||||
typedef enum ErrorId {
|
typedef enum ErrorId {
|
||||||
SUCCESS, // not an error
|
SUCCESS, // not an error
|
||||||
|
|||||||
97
src/base/init.c
Normal file
97
src/base/init.c
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
#include "base.h"
|
||||||
|
#include "../Autoarr/Autoarr.h"
|
||||||
|
#include "../SearchTree/SearchTree.h"
|
||||||
|
#include "../Hashtable/Hashtable.h"
|
||||||
|
#include "../String/StringBuilder.h"
|
||||||
|
|
||||||
|
void kerepTypeDescriptors_initKerepTypes(){
|
||||||
|
// null
|
||||||
|
kerepType_register(NULL, kerepTypeId_Null, NULL);
|
||||||
|
// base types
|
||||||
|
kerepType_register(char, kerepTypeId_Char, NULL);
|
||||||
|
kerepType_register(bool, kerepTypeId_Bool, NULL);
|
||||||
|
kerepType_register(float32, kerepTypeId_Float32, NULL);
|
||||||
|
kerepType_register(float64, kerepTypeId_Float64, NULL);
|
||||||
|
kerepType_register(int8, kerepTypeId_Int8, NULL);
|
||||||
|
kerepType_register(uint8, kerepTypeId_UInt8, NULL);
|
||||||
|
kerepType_register(int16, kerepTypeId_Int16, NULL);
|
||||||
|
kerepType_register(uint16, kerepTypeId_UInt16, NULL);
|
||||||
|
kerepType_register(int32, kerepTypeId_Int32, NULL);
|
||||||
|
kerepType_register(uint32, kerepTypeId_UInt32, NULL);
|
||||||
|
kerepType_register(int64, kerepTypeId_Int64, NULL);
|
||||||
|
kerepType_register(uint64, kerepTypeId_UInt64, NULL);
|
||||||
|
// base type pointers
|
||||||
|
kerepType_register(char*, kerepTypeId_CharPtr, NULL);
|
||||||
|
kerepType_register(bool*, kerepTypeId_BoolPtr, NULL);
|
||||||
|
kerepType_register(float32*, kerepTypeId_Float32Ptr, NULL);
|
||||||
|
kerepType_register(float64*, kerepTypeId_Float64Ptr, NULL);
|
||||||
|
kerepType_register(int8*, kerepTypeId_Int8Ptr, NULL);
|
||||||
|
kerepType_register(uint8*, kerepTypeId_UInt8Ptr, NULL);
|
||||||
|
kerepType_register(int16*, kerepTypeId_Int16Ptr, NULL);
|
||||||
|
kerepType_register(uint16*, kerepTypeId_UInt16Ptr, NULL);
|
||||||
|
kerepType_register(int32*, kerepTypeId_Int32Ptr, NULL);
|
||||||
|
kerepType_register(uint32*, kerepTypeId_UInt32Ptr, NULL);
|
||||||
|
kerepType_register(int64*, kerepTypeId_Int64Ptr, NULL);
|
||||||
|
kerepType_register(uint64*, kerepTypeId_UInt64Ptr, NULL);
|
||||||
|
|
||||||
|
// base type autoarrs
|
||||||
|
kerepType_register(Autoarr_char, kerepTypeId_AutoarrChar, ____Autoarr_free_char);
|
||||||
|
kerepType_register(Autoarr_bool, kerepTypeId_AutoarrBool, ____Autoarr_free_bool);
|
||||||
|
kerepType_register(Autoarr_float32, kerepTypeId_AutoarrFloat32, ____Autoarr_free_float32);
|
||||||
|
kerepType_register(Autoarr_float64, kerepTypeId_AutoarrFloat64, ____Autoarr_free_float64);
|
||||||
|
kerepType_register(Autoarr_int8, kerepTypeId_AutoarrInt8, ____Autoarr_free_int8);
|
||||||
|
kerepType_register(Autoarr_uint8, kerepTypeId_AutoarrUInt8, ____Autoarr_free_uint8);
|
||||||
|
kerepType_register(Autoarr_int16, kerepTypeId_AutoarrInt16, ____Autoarr_free_int16);
|
||||||
|
kerepType_register(Autoarr_uint16, kerepTypeId_AutoarrUInt16, ____Autoarr_free_uint16);
|
||||||
|
kerepType_register(Autoarr_int32, kerepTypeId_AutoarrInt32, ____Autoarr_free_int32);
|
||||||
|
kerepType_register(Autoarr_uint32, kerepTypeId_AutoarrUInt32, ____Autoarr_free_uint32);
|
||||||
|
kerepType_register(Autoarr_int64, kerepTypeId_AutoarrInt64, ____Autoarr_free_int64);
|
||||||
|
kerepType_register(Autoarr_uint64, kerepTypeId_AutoarrUInt64, ____Autoarr_free_uint64);
|
||||||
|
// base type autoarr pointers
|
||||||
|
kerepType_register(Autoarr_char*, kerepTypeId_AutoarrCharPtr, ____Autoarr_free_char);
|
||||||
|
kerepType_register(Autoarr_bool*, kerepTypeId_AutoarrBoolPtr, ____Autoarr_free_bool);
|
||||||
|
kerepType_register(Autoarr_float32*, kerepTypeId_AutoarrFloat32Ptr, ____Autoarr_free_float32);
|
||||||
|
kerepType_register(Autoarr_float64*, kerepTypeId_AutoarrFloat64Ptr, ____Autoarr_free_float64);
|
||||||
|
kerepType_register(Autoarr_int8*, kerepTypeId_AutoarrInt8Ptr, ____Autoarr_free_int8);
|
||||||
|
kerepType_register(Autoarr_uint8*, kerepTypeId_AutoarrUInt8Ptr, ____Autoarr_free_uint8);
|
||||||
|
kerepType_register(Autoarr_int16*, kerepTypeId_AutoarrInt16Ptr, ____Autoarr_free_int16);
|
||||||
|
kerepType_register(Autoarr_uint16*, kerepTypeId_AutoarrUInt16Ptr, ____Autoarr_free_uint16);
|
||||||
|
kerepType_register(Autoarr_int32*, kerepTypeId_AutoarrInt32Ptr, ____Autoarr_free_int32);
|
||||||
|
kerepType_register(Autoarr_uint32*, kerepTypeId_AutoarrUInt32Ptr, ____Autoarr_free_uint32);
|
||||||
|
kerepType_register(Autoarr_int64*, kerepTypeId_AutoarrInt64Ptr, ____Autoarr_free_int64);
|
||||||
|
kerepType_register(Autoarr_uint64*, kerepTypeId_AutoarrUInt64Ptr, ____Autoarr_free_uint64);
|
||||||
|
|
||||||
|
// Unitype
|
||||||
|
kerepType_register(Unitype, kerepTypeId_Unitype, __UnitypePtr_free);
|
||||||
|
kerepType_register(Unitype*, kerepTypeId_UnitypePtr, __UnitypePtr_free);
|
||||||
|
kerepType_register(Autoarr_Unitype, kerepTypeId_AutoarrUnitype, ____Autoarr_free_Unitype_);
|
||||||
|
kerepType_register(Autoarr_Unitype*, kerepTypeId_AutoarrUnitypePtr, ____Autoarr_free_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
|
||||||
|
kerepType_register(STNode, kerepTypeId_STNode, __STNode_free);
|
||||||
|
kerepType_register(STNode*, kerepTypeId_STNodePtr, __STNode_free);
|
||||||
|
|
||||||
|
// KeyValuePair
|
||||||
|
kerepType_register(KVPair, kerepTypeId_KVPair, __KVPair_free);
|
||||||
|
kerepType_register(KVPair*, kerepTypeId_KVPairPtr, __KVPair_free);
|
||||||
|
kerepType_register(Autoarr_KVPair, kerepTypeId_AutoarrKVPair, ____Autoarr_free_KVPair_);
|
||||||
|
kerepType_register(Autoarr_KVPair*, kerepTypeId_AutoarrKVPairPtr, ____Autoarr_free_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
|
||||||
|
kerepType_register(Hashtable, kerepTypeId_Hashtable, __Hashtable_free);
|
||||||
|
kerepType_register(Hashtable*, kerepTypeId_HashtablePtr, __Hashtable_free);
|
||||||
|
|
||||||
|
// StringBuilder
|
||||||
|
kerepType_register(Autoarr_string, kerepTypeId_AutoarrString, ____Autoarr_free_string);
|
||||||
|
kerepType_register(Autoarr_string*, kerepTypeId_AutoarrStringPtr, ____Autoarr_free_string);
|
||||||
|
kerepType_register(StringBuilder, kerepTypeId_StringBuilder, __StringBuilder_free);
|
||||||
|
kerepType_register(StringBuilder*, kerepTypeId_StringBuilderPtr, __StringBuilder_free);
|
||||||
|
}
|
||||||
12
src/base/init.h
Normal file
12
src/base/init.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// call this between kerepTypeDescriptors_beginInit() and kerepTypeDescriptors_endInit()
|
||||||
|
void kerepTypeDescriptors_initKerepTypes();
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -12,10 +12,19 @@ extern "C" {
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <setjmp.h>
|
#include <setjmp.h>
|
||||||
|
|
||||||
|
typedef int8_t int8;
|
||||||
|
typedef uint8_t uint8;
|
||||||
|
typedef int16_t int16;
|
||||||
|
typedef uint16_t uint16;
|
||||||
|
typedef int32_t int32;
|
||||||
|
typedef uint32_t uint32;
|
||||||
|
typedef int64_t int64;
|
||||||
|
typedef uint64_t uint64;
|
||||||
|
typedef float float32;
|
||||||
|
typedef double float64;
|
||||||
|
|
||||||
#define dbg(N) printf("\e[95m%d\n",N)
|
#define dbg(N) printf("\e[95m%d\n",N)
|
||||||
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#pragma comment(lib, "mincore_downlevel.lib") // Support OS older than SDK
|
#pragma comment(lib, "mincore_downlevel.lib") // Support OS older than SDK
|
||||||
#define _CRT_SECURE_NO_WARNINGS 1
|
#define _CRT_SECURE_NO_WARNINGS 1
|
||||||
@ -29,13 +38,12 @@ extern "C" {
|
|||||||
#define CALL
|
#define CALL
|
||||||
#endif
|
#endif
|
||||||
#ifndef typeof
|
#ifndef typeof
|
||||||
#define typeof __typeof__
|
#define typeof(X) __typeof__(X)
|
||||||
#endif
|
#endif
|
||||||
#else
|
#else
|
||||||
#pragma GCC error "unknown compiler"
|
#pragma GCC error "unknown compiler"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#define IFWIN(YES, NO) YES
|
#define IFWIN(YES, NO) YES
|
||||||
#define IFMSC(YES, NO) YES
|
#define IFMSC(YES, NO) YES
|
||||||
@ -49,6 +57,9 @@ extern "C" {
|
|||||||
#pragma GCC error "unknown compiler"
|
#pragma GCC error "unknown compiler"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef sprintf_s
|
||||||
|
#define sprintf_s(BUF, BUFSIZE, FORMAT, ...) sprintf(BUF, FORMAT, ## __VA_ARGS__)
|
||||||
|
#endif
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
209
src/base/types.c
209
src/base/types.c
@ -1,154 +1,71 @@
|
|||||||
#include "types.h"
|
|
||||||
#include "errors.h"
|
|
||||||
#include "../Autoarr/Autoarr.h"
|
#include "../Autoarr/Autoarr.h"
|
||||||
#include "../Hashtable/Hashtable.h"
|
#include "unitype.h"
|
||||||
#include "../SearchTree/SearchTree.h"
|
|
||||||
|
|
||||||
const char* my_type_name(my_type t){
|
Autoarr_declare(kerepTypeDescriptor)
|
||||||
switch (t) {
|
Autoarr_define(kerepTypeDescriptor)
|
||||||
case Null: return "Null";
|
|
||||||
case Float64: return "Float64";
|
kerepTypeId_define(kerepTypeId_Null);
|
||||||
case Float32: return "Float32";
|
|
||||||
case Bool: return "Bool";
|
kerepTypeId_define(kerepTypeId_Char);
|
||||||
case Char: return "Char";
|
kerepTypeId_define(kerepTypeId_Bool);
|
||||||
case Int8: return "Int8";
|
kerepTypeId_define(kerepTypeId_Float32);
|
||||||
case UInt8: return "UInt8";
|
kerepTypeId_define(kerepTypeId_Float64);
|
||||||
case Int16: return "Int16";
|
kerepTypeId_define(kerepTypeId_Int8);
|
||||||
case UInt16: return "UInt16";
|
kerepTypeId_define(kerepTypeId_UInt8);
|
||||||
case Int32: return "Int32";
|
kerepTypeId_define(kerepTypeId_Int16);
|
||||||
case UInt32: return "UInt32";
|
kerepTypeId_define(kerepTypeId_UInt16);
|
||||||
case Int64: return "Int64";
|
kerepTypeId_define(kerepTypeId_Int32);
|
||||||
case UInt64: return "UInt64";
|
kerepTypeId_define(kerepTypeId_UInt32);
|
||||||
case Int8Ptr: return "Int8Ptr";
|
kerepTypeId_define(kerepTypeId_Int64);
|
||||||
case UInt8Ptr: return "UInt8Ptr";
|
kerepTypeId_define(kerepTypeId_UInt64);
|
||||||
case Int16Ptr: return "Int16Ptr";
|
|
||||||
case UInt16Ptr: return "UInt16Ptr";
|
kerepTypeId_define(kerepTypeId_CharPtr);
|
||||||
case Int32Ptr: return "Int32Ptr";
|
kerepTypeId_define(kerepTypeId_BoolPtr);
|
||||||
case UInt32Ptr: return "UInt32Ptr";
|
kerepTypeId_define(kerepTypeId_Float32Ptr);
|
||||||
case Int64Ptr: return "Int64Ptr";
|
kerepTypeId_define(kerepTypeId_Float64Ptr);
|
||||||
case UInt64Ptr: return "UInt64Ptr";
|
kerepTypeId_define(kerepTypeId_Int8Ptr);
|
||||||
case CharPtr: return "CharPtr";
|
kerepTypeId_define(kerepTypeId_UInt8Ptr);
|
||||||
case STNodePtr: return "STNodePtr";
|
kerepTypeId_define(kerepTypeId_Int16Ptr);
|
||||||
case HashtablePtr: return "HashtablePtr";
|
kerepTypeId_define(kerepTypeId_UInt16Ptr);
|
||||||
case UniversalType: return "Unitype";
|
kerepTypeId_define(kerepTypeId_Int32Ptr);
|
||||||
case AutoarrInt8Ptr: return "AutoarrInt8Ptr";
|
kerepTypeId_define(kerepTypeId_UInt32Ptr);
|
||||||
case AutoarrUInt8Ptr: return "AutoarrUInt8Ptr";
|
kerepTypeId_define(kerepTypeId_Int64Ptr);
|
||||||
case AutoarrInt16Ptr: return "AutoarrInt16Ptr";
|
kerepTypeId_define(kerepTypeId_UInt64Ptr);
|
||||||
case AutoarrUInt16Ptr: return "AutoarrUInt16Ptr";
|
|
||||||
case AutoarrInt32Ptr: return "AutoarrInt32Ptr";
|
// type descriptors are stored here during initialization
|
||||||
case AutoarrUInt32Ptr: return "AutoarrUInt32Ptr";
|
Autoarr(kerepTypeDescriptor)* __kerepTypeDescriptors=NULL;
|
||||||
case AutoarrInt64Ptr: return "AutoarrInt64Ptr";
|
// here type descriptors are stored when initialization is complited
|
||||||
case AutoarrUInt64Ptr: return "AutoarrUInt64Ptr";
|
kerepTypeDescriptor* typeDescriptors=NULL;
|
||||||
case AutoarrUnitypePtr: return "AutoarrUnitypePtr";
|
kerepTypeId kerepTypeId_last=-1;
|
||||||
case AutoarrKVPairPtr: return "AutoarrKVPairPtr";
|
|
||||||
default: throw(ERR_WRONGTYPE);
|
typedef enum{
|
||||||
}
|
NotInitialized, Initializing, Initialized
|
||||||
|
} kerepTypeDescriptorsState;
|
||||||
|
kerepTypeDescriptorsState initState=NotInitialized;
|
||||||
|
|
||||||
|
void kerepTypeDescriptors_beginInit(){
|
||||||
|
printf("\e[94mtype descriptors initializing...\n");
|
||||||
|
__kerepTypeDescriptors=Autoarr_create(kerepTypeDescriptor, 256, 256);
|
||||||
|
if(__kerepTypeDescriptors==NULL) throw(ERR_NULLPTR);
|
||||||
}
|
}
|
||||||
|
|
||||||
// frees VoidPtr value or does nothing if type isn't pointer
|
void kerepTypeDescriptors_endInit(){
|
||||||
void Unitype_free(Unitype u){
|
typeDescriptors=Autoarr_toArray(__kerepTypeDescriptors);
|
||||||
switch (u.type) {
|
Autoarr_free(__kerepTypeDescriptors,true);
|
||||||
case Null:
|
if(typeDescriptors==NULL) throw(ERR_NULLPTR);
|
||||||
case Float32:
|
printf("\e[92minitialized %u type descriptors\n", kerepTypeId_last);
|
||||||
case Float64:
|
|
||||||
case Char:
|
|
||||||
case Bool:
|
|
||||||
case Int8:
|
|
||||||
case UInt8:
|
|
||||||
case Int16:
|
|
||||||
case UInt16:
|
|
||||||
case Int32:
|
|
||||||
case UInt32:
|
|
||||||
case Int64:
|
|
||||||
case UInt64:
|
|
||||||
break;
|
|
||||||
case Int8Ptr:
|
|
||||||
case UInt8Ptr:
|
|
||||||
case Int16Ptr:
|
|
||||||
case UInt16Ptr:
|
|
||||||
case Int32Ptr:
|
|
||||||
case UInt32Ptr:
|
|
||||||
case Int64Ptr:
|
|
||||||
case UInt64Ptr:
|
|
||||||
case CharPtr:
|
|
||||||
free(u.VoidPtr);
|
|
||||||
break;
|
|
||||||
case HashtablePtr:
|
|
||||||
Hashtable_free(u.VoidPtr);
|
|
||||||
break;
|
|
||||||
case STNodePtr:
|
|
||||||
STNode_free(u.VoidPtr);
|
|
||||||
break;
|
|
||||||
case AutoarrInt8Ptr:
|
|
||||||
__Autoarr_free_int8(u.VoidPtr);
|
|
||||||
break;
|
|
||||||
case AutoarrUInt8Ptr:
|
|
||||||
__Autoarr_free_uint8(u.VoidPtr);
|
|
||||||
break;
|
|
||||||
case AutoarrInt16Ptr:
|
|
||||||
__Autoarr_free_int16(u.VoidPtr);
|
|
||||||
break;
|
|
||||||
case AutoarrUInt16Ptr:
|
|
||||||
__Autoarr_free_uint16(u.VoidPtr);
|
|
||||||
break;
|
|
||||||
case AutoarrInt32Ptr:
|
|
||||||
__Autoarr_free_int32(u.VoidPtr);
|
|
||||||
break;
|
|
||||||
case AutoarrUInt32Ptr:
|
|
||||||
__Autoarr_free_uint32(u.VoidPtr);
|
|
||||||
break;
|
|
||||||
case AutoarrInt64Ptr:
|
|
||||||
__Autoarr_free_int64(u.VoidPtr);
|
|
||||||
break;
|
|
||||||
case AutoarrUInt64Ptr:
|
|
||||||
__Autoarr_free_uint64(u.VoidPtr);
|
|
||||||
break;
|
|
||||||
case AutoarrUnitypePtr:
|
|
||||||
Autoarr_free_Unitype(u.VoidPtr);
|
|
||||||
break;
|
|
||||||
case AutoarrKVPairPtr:
|
|
||||||
Autoarr_free_KVPair(u.VoidPtr);
|
|
||||||
break;
|
|
||||||
default: throw(ERR_WRONGTYPE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define BUFSIZE 64
|
void __kerepType_register(char* name, int16 size, void (*free_members)(void*)){
|
||||||
char* sprintuni(Unitype v){
|
kerepTypeDescriptor typeDesc={
|
||||||
char* buf=malloc(BUFSIZE);
|
.name=name,
|
||||||
IFMSC(
|
.size=size,
|
||||||
switch (v.type) {
|
.free_members=free_members,
|
||||||
case Null: sprintf_s(buf, BUFSIZE, "{Null}");break;
|
.id=++kerepTypeId_last
|
||||||
case Float64: sprintf_s(buf, BUFSIZE, "{%s : %lf}", my_type_name(v.type),v.Float64);break;
|
};
|
||||||
case Bool:
|
Autoarr_add(__kerepTypeDescriptors, typeDesc);
|
||||||
case UInt64: sprintf_s(buf, BUFSIZE, "{%s : %lu}", my_type_name(v.type),v.UInt64);break;
|
|
||||||
case Int64: sprintf_s(buf, BUFSIZE, "{%s : %ld}", my_type_name(v.type),v.Int64);break;
|
|
||||||
case CharPtr: ;
|
|
||||||
size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2;
|
|
||||||
buf=realloc(buf, newBUFSIZE);
|
|
||||||
sprintf_s(buf, newBUFSIZE, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr);
|
|
||||||
break;
|
|
||||||
default: sprintf_s(buf, BUFSIZE, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break;
|
|
||||||
},
|
|
||||||
switch (v.type) {
|
|
||||||
case Null: sprintf(buf, "{Null}"); break;
|
|
||||||
case Float64: sprintf(buf, "{%s : %lf}", my_type_name(v.type),v.Float64); break;
|
|
||||||
case Bool:
|
|
||||||
case UInt64: sprintf(buf, "{%s : " IFWIN("%llu", "%lu") "}", my_type_name(v.type),v.UInt64); break;
|
|
||||||
case Int64: sprintf(buf, "{%s : " IFWIN("%lld", "%ld") "}", my_type_name(v.type),v.Int64); break;
|
|
||||||
case CharPtr: ;
|
|
||||||
size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2;
|
|
||||||
buf=realloc(buf, newBUFSIZE);
|
|
||||||
sprintf(buf, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr);
|
|
||||||
break;
|
|
||||||
default: sprintf(buf, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
return buf;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void printuni(Unitype v){
|
kerepTypeDescriptor kerepTypeDescriptor_get(kerepTypeId id){
|
||||||
char* s=sprintuni(v);
|
return typeDescriptors[id];
|
||||||
fputs(s, stdout);
|
}
|
||||||
free(s);
|
|
||||||
}
|
|
||||||
|
|||||||
@ -6,52 +6,58 @@ extern "C" {
|
|||||||
|
|
||||||
#include "std.h"
|
#include "std.h"
|
||||||
|
|
||||||
typedef int8_t int8;
|
typedef uint16 kerepTypeId;
|
||||||
typedef uint8_t uint8;
|
|
||||||
typedef int16_t int16;
|
|
||||||
typedef uint16_t uint16;
|
|
||||||
typedef int32_t int32;
|
|
||||||
typedef uint32_t uint32;
|
|
||||||
typedef int64_t int64;
|
|
||||||
typedef uint64_t uint64;
|
|
||||||
typedef float float32;
|
|
||||||
typedef double float64;
|
|
||||||
typedef enum __attribute__((__packed__)) my_type {
|
|
||||||
Null, Float32, Float64, Char, Bool,
|
|
||||||
UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64,
|
|
||||||
UInt8Ptr, Int8Ptr, UInt16Ptr, Int16Ptr, UInt32Ptr, Int32Ptr, UInt64Ptr, Int64Ptr,
|
|
||||||
CharPtr, STNodePtr, HashtablePtr,
|
|
||||||
UniversalType,
|
|
||||||
AutoarrInt8Ptr, AutoarrUInt8Ptr, AutoarrInt16Ptr, AutoarrUInt16Ptr,
|
|
||||||
AutoarrInt32Ptr, AutoarrUInt32Ptr, AutoarrInt64Ptr, AutoarrUInt64Ptr,
|
|
||||||
AutoarrUnitypePtr, AutoarrKVPairPtr, knSocketPtr
|
|
||||||
} my_type;
|
|
||||||
#define my_type_last knSocketPtr
|
|
||||||
|
|
||||||
const char* my_type_name(my_type t);
|
typedef struct kerepTypeDescriptor{
|
||||||
|
void (*free_members)(void*); // NULL or function which frees all struct members
|
||||||
|
char* name;
|
||||||
|
kerepTypeId id;
|
||||||
|
uint16 size;
|
||||||
|
} kerepTypeDescriptor;
|
||||||
|
|
||||||
typedef struct Unitype{
|
#define kerepTypeId_declare(ID_VAR_NAME)\
|
||||||
union {
|
extern kerepTypeId ID_VAR_NAME
|
||||||
int64 Int64;
|
#define kerepTypeId_define(ID_VAR_NAME)\
|
||||||
uint64 UInt64;
|
kerepTypeId ID_VAR_NAME=-1
|
||||||
double Float64;
|
|
||||||
bool Bool;
|
|
||||||
void* VoidPtr;
|
|
||||||
};
|
|
||||||
my_type type;
|
|
||||||
} Unitype;
|
|
||||||
|
|
||||||
static const Unitype UniNull={.VoidPtr=NULL,.type=Null};
|
extern kerepTypeId kerepTypeId_last;
|
||||||
static const Unitype UniTrue={.Bool=true,.type=Bool};
|
void __kerepType_register(char* name, int16 size, void (*free_members)(void*));
|
||||||
static const Unitype UniFalse={.Bool=false,.type=Bool};
|
|
||||||
|
|
||||||
#define Uni(TYPE,VAL) (Unitype){.type=TYPE,.TYPE=VAL}
|
#define kerepType_register(TYPE, ID_VAR_NAME, FREE_MEMBERS_FUNC)\
|
||||||
#define UniPtr(TYPE,VAL) (Unitype){.type=TYPE,.VoidPtr=VAL}
|
__kerepType_register(#ID_VAR_NAME, sizeof(TYPE), FREE_MEMBERS_FUNC);\
|
||||||
|
ID_VAR_NAME=kerepTypeId_last;
|
||||||
|
|
||||||
// frees VoidPtr value or does nothing if type isn't pointer
|
void kerepTypeDescriptors_beginInit();
|
||||||
void Unitype_free(Unitype u);
|
void kerepTypeDescriptors_endInit();
|
||||||
void printuni(Unitype v);
|
kerepTypeDescriptor kerepTypeDescriptor_get(kerepTypeId id);
|
||||||
char* sprintuni(Unitype v);
|
|
||||||
|
kerepTypeId_declare(kerepTypeId_Null);
|
||||||
|
|
||||||
|
kerepTypeId_declare(kerepTypeId_Char);
|
||||||
|
kerepTypeId_declare(kerepTypeId_Bool);
|
||||||
|
kerepTypeId_declare(kerepTypeId_Float32);
|
||||||
|
kerepTypeId_declare(kerepTypeId_Float64);
|
||||||
|
kerepTypeId_declare(kerepTypeId_Int8);
|
||||||
|
kerepTypeId_declare(kerepTypeId_UInt8);
|
||||||
|
kerepTypeId_declare(kerepTypeId_Int16);
|
||||||
|
kerepTypeId_declare(kerepTypeId_UInt16);
|
||||||
|
kerepTypeId_declare(kerepTypeId_Int32);
|
||||||
|
kerepTypeId_declare(kerepTypeId_UInt32);
|
||||||
|
kerepTypeId_declare(kerepTypeId_Int64);
|
||||||
|
kerepTypeId_declare(kerepTypeId_UInt64);
|
||||||
|
|
||||||
|
kerepTypeId_declare(kerepTypeId_CharPtr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_BoolPtr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_Float32Ptr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_Float64Ptr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_Int8Ptr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_UInt8Ptr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_Int16Ptr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_UInt16Ptr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_Int32Ptr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_UInt32Ptr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_Int64Ptr);
|
||||||
|
kerepTypeId_declare(kerepTypeId_UInt64Ptr);
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
40
src/base/unitype.c
Normal file
40
src/base/unitype.c
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#include "base.h"
|
||||||
|
|
||||||
|
kerepTypeId_define(kerepTypeId_Unitype);
|
||||||
|
kerepTypeId_define(kerepTypeId_UnitypePtr);
|
||||||
|
|
||||||
|
void Unitype_free(Unitype u){
|
||||||
|
kerepTypeDescriptor type=kerepTypeDescriptor_get(u.typeId);
|
||||||
|
if(type.free_members)
|
||||||
|
type.free_members(u.VoidPtr);
|
||||||
|
if(u.allocatedInHeap)
|
||||||
|
free(u.VoidPtr);
|
||||||
|
}
|
||||||
|
void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); }
|
||||||
|
|
||||||
|
#define BUFSIZE 64
|
||||||
|
char* sprintuni(Unitype v){
|
||||||
|
char* buf=malloc(BUFSIZE);
|
||||||
|
kerepTypeDescriptor type=kerepTypeDescriptor_get(v.typeId);
|
||||||
|
if(v.typeId==kerepTypeId_Null)
|
||||||
|
sprintf_s(buf, BUFSIZE, "{Null}");
|
||||||
|
else if(v.typeId==kerepTypeId_Float64)
|
||||||
|
sprintf_s(buf, BUFSIZE, "{%s : %lf}", type.name,v.Float64);
|
||||||
|
else if(v.typeId==kerepTypeId_Bool || v.typeId==kerepTypeId_UInt64)
|
||||||
|
sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%llu", "%lu") "}", type.name,v.UInt64);
|
||||||
|
else if(v.typeId==kerepTypeId_Int64)
|
||||||
|
sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%lld", "%ld") "}", type.name,v.Int64);
|
||||||
|
else if(v.typeId==kerepTypeId_CharPtr){
|
||||||
|
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);
|
||||||
|
fputs(s, stdout);
|
||||||
|
free(s);
|
||||||
|
}
|
||||||
50
src/base/unitype.h
Normal file
50
src/base/unitype.h
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
typedef struct Unitype{
|
||||||
|
union {
|
||||||
|
int64 Int64;
|
||||||
|
uint64 UInt64;
|
||||||
|
double Float64;
|
||||||
|
bool Bool;
|
||||||
|
void* VoidPtr;
|
||||||
|
char Bytes[8];
|
||||||
|
};
|
||||||
|
kerepTypeId typeId;
|
||||||
|
bool allocatedInHeap; // should Unitype_free call free() to VoidPtr*
|
||||||
|
} Unitype;
|
||||||
|
kerepTypeId_declare(kerepTypeId_Unitype);
|
||||||
|
kerepTypeId_declare(kerepTypeId_UnitypePtr);
|
||||||
|
|
||||||
|
|
||||||
|
#define Uni(TYPE, VAL) (Unitype){\
|
||||||
|
.TYPE=VAL, .typeId=kerepTypeId_##TYPE, .allocatedInHeap=false}
|
||||||
|
|
||||||
|
#define UniInt64(VAL) Uni(Int64, VAL)
|
||||||
|
#define UniUInt64(VAL) Uni(UInt64, VAL)
|
||||||
|
#define UniFloat64(VAL) Uni(Float64, VAL)
|
||||||
|
#define UniBool(VAL) Uni(Bool, VAL)
|
||||||
|
|
||||||
|
#define UniPtrStack(ID_VAR_NAME, VAL) (Unitype){\
|
||||||
|
.VoidPtr=VAL, .typeId=ID_VAR_NAME, .allocatedInHeap=false}
|
||||||
|
#define UniPtrHeap(ID_VAR_NAME, VAL) (Unitype){\
|
||||||
|
.VoidPtr=VAL, .typeId=ID_VAR_NAME, .allocatedInHeap=true}
|
||||||
|
|
||||||
|
#define UniNull UniPtrStack(kerepTypeId_Null, NULL)
|
||||||
|
#define UniTrue UniBool(true)
|
||||||
|
#define UniFalse UniBool(false)
|
||||||
|
|
||||||
|
// frees VoidPtr value or does nothing if type isn't pointer
|
||||||
|
void Unitype_free(Unitype u);
|
||||||
|
void __UnitypePtr_free(void* u);
|
||||||
|
void printuni(Unitype v);
|
||||||
|
char* sprintuni(Unitype v);
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -13,7 +13,10 @@ void test_all(){
|
|||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
setlocale(LC_ALL, "en-US.Unicode");
|
setlocale(LC_ALL, "en-US.Unicode");
|
||||||
printf("\e[92mkerep tests are starting!\e[97m\n");
|
kerepTypeDescriptors_beginInit();
|
||||||
|
kerepTypeDescriptors_initKerepTypes();
|
||||||
|
kerepTypeDescriptors_endInit();
|
||||||
|
printf("\e[97mkerep tests are starting!\n");
|
||||||
optime("test_all",1,test_all());
|
optime("test_all",1,test_all());
|
||||||
printf("\e[0m\n");
|
printf("\e[0m\n");
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@ -47,7 +47,7 @@ void test_autoarr(){
|
|||||||
resetar(ar);
|
resetar(ar);
|
||||||
printf("\e[92mautoarr values reset\n");
|
printf("\e[92mautoarr values reset\n");
|
||||||
printallval(ar);
|
printallval(ar);
|
||||||
Autoarr_free(ar);
|
Autoarr_free(ar, true);
|
||||||
printf("\e[92mautoarr deleted\n");
|
printf("\e[92mautoarr deleted\n");
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,7 @@ void print_dtsod(Hashtable* dtsod){
|
|||||||
printf("\e[92m");
|
printf("\e[92m");
|
||||||
Hashtable_foreach(dtsod, p,({
|
Hashtable_foreach(dtsod, p,({
|
||||||
printkvp(p);
|
printkvp(p);
|
||||||
if(p.value.type==HashtablePtr){
|
if(p.value.typeId==kerepTypeId_HashtablePtr){
|
||||||
printf(": {\n");
|
printf(": {\n");
|
||||||
Hashtable* sub=p.value.VoidPtr;
|
Hashtable* sub=p.value.VoidPtr;
|
||||||
Hashtable_foreach(sub, _p,({
|
Hashtable_foreach(sub, _p,({
|
||||||
|
|||||||
@ -33,7 +33,7 @@ char data[]="iojihiojopijiugbjmoihftytryfdrh";
|
|||||||
}\
|
}\
|
||||||
printf("\e[93m%u \e[94mcollisions detected in %u hashes\n", collisions, COLLISION_TESTS);\
|
printf("\e[93m%u \e[94mcollisions detected in %u hashes\n", collisions, COLLISION_TESTS);\
|
||||||
}));\
|
}));\
|
||||||
Autoarr_free(hashes);\
|
Autoarr_free(hashes, true);\
|
||||||
printf("\e[96m--------------------------------------\n");\
|
printf("\e[96m--------------------------------------\n");\
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,7 @@ EXPORT void CALL test_marshalling(char* text, KVPair** kptr){
|
|||||||
KVPair* k=malloc(sizeof(KVPair));
|
KVPair* k=malloc(sizeof(KVPair));
|
||||||
k->key="message";
|
k->key="message";
|
||||||
char* tc=cptr_copy(text);
|
char* tc=cptr_copy(text);
|
||||||
Unitype u={.VoidPtr=tc, .type=CharPtr};
|
Unitype u=UniPtrHeap(kerepTypeId_CharPtr,tc);
|
||||||
k->value=u;
|
k->value=u;
|
||||||
*kptr=k;
|
*kptr=k;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,8 +6,9 @@ void printstnode(STNode* node){
|
|||||||
IFWIN("%llu", "%lu")
|
IFWIN("%llu", "%lu")
|
||||||
"\n address: %p\n value: ",sizeof(STNode),node);
|
"\n address: %p\n value: ",sizeof(STNode),node);
|
||||||
printuni(node->value);
|
printuni(node->value);
|
||||||
|
printf("\n");
|
||||||
// prints pointers to all existing branches
|
// prints pointers to all existing branches
|
||||||
/* printf("\n branches: %p\n", node->branches);
|
/* printf(" branches: %p\n", node->branches);
|
||||||
if(node->branches) for(uint8 i=0;i<8;i++){
|
if(node->branches) for(uint8 i=0;i<8;i++){
|
||||||
printf(" \e[90m[%u]=%p\n",i,node->branches[i]);
|
printf(" \e[90m[%u]=%p\n",i,node->branches[i]);
|
||||||
if(node->branches[i])
|
if(node->branches[i])
|
||||||
@ -27,27 +28,27 @@ void test_searchtree(){
|
|||||||
STNode* node=STNode_create();
|
STNode* node=STNode_create();
|
||||||
printf("\e[92mnode created\n");
|
printf("\e[92mnode created\n");
|
||||||
printf("push:\e[94m\n ");
|
printf("push:\e[94m\n ");
|
||||||
Unitype u={.type=Int64,.Int64=-3};
|
Unitype u=UniInt64(-3);
|
||||||
printuni(u);
|
printuni(u);
|
||||||
ST_push(node,"type", u);
|
ST_push(node,"type", u);
|
||||||
printf(" -> type\n ");
|
printf(" -> type\n ");
|
||||||
u=(Unitype){.type=Int64,.Int64=25};
|
u=UniInt64(25);
|
||||||
printuni(u);
|
printuni(u);
|
||||||
ST_push(node,"time", u);
|
ST_push(node,"time", u);
|
||||||
printf(" -> time\n ");
|
printf(" -> time\n ");
|
||||||
u=(Unitype){.type=Float64,.Float64=-542.00600};
|
u=UniFloat64(-542.00600);
|
||||||
printuni(u);
|
printuni(u);
|
||||||
ST_push(node,"author_id", u);
|
ST_push(node,"author_id", u);
|
||||||
printf(" -> author_id\n ");
|
printf(" -> author_id\n ");
|
||||||
u=(Unitype){.type=Int64,.Int64=-31255};
|
u=UniInt64(-31255);
|
||||||
printuni(u);
|
printuni(u);
|
||||||
ST_push(node,"channel_id", u);
|
ST_push(node,"channel_id", u);
|
||||||
printf(" -> channel_id\n ");
|
printf(" -> channel_id\n ");
|
||||||
u=(Unitype){.type=Float64,.Float64=32.2004};
|
u=UniPtrHeap(kerepTypeId_CharPtr, cptr_copy("32.2004"));
|
||||||
printuni(u);
|
printuni(u);
|
||||||
ST_push(node,"message_id", u);
|
ST_push(node,"message_id", u);
|
||||||
printf(" -> message_id\n ");
|
printf(" -> message_id\n ");
|
||||||
u=(Unitype){.type=CharPtr,.VoidPtr=cptr_copy("some text UwU")};
|
u=UniPtrStack(kerepTypeId_CharPtr,"some text UwU");
|
||||||
printuni(u);
|
printuni(u);
|
||||||
ST_push(node,"text", u);
|
ST_push(node,"text", u);
|
||||||
printf(" -> text\n");
|
printf(" -> text\n");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user