registred all types

This commit is contained in:
2022-08-25 01:37:14 +06:00
parent 2e378d1458
commit d62405ccff
24 changed files with 201 additions and 213 deletions

View File

@@ -5,7 +5,7 @@ extern "C" {
#endif
#include "std.h"
#include "types.h"
#include "unitype.h"
typedef enum ErrorId {
SUCCESS, // not an error

View File

@@ -1,66 +1,95 @@
#include "base.h"
#include "../Autoarr/Autoarr.h"
#include "../SearchTree/SearchTree.h"
#include "../Hashtable/Hashtable.h"
#include "../String/StringBuilder.h"
void kerepInit(){
void kerepTypeDescriptors_initKerepTypes(){
// null
kerepType_register(NULL, Null, NULL);
kerepType_register(char, Char, NULL);
kerepType_register(bool, Bool, NULL);
// base types
kerepType_register(char, Char, NULL);
kerepType_register(bool, Bool, NULL);
kerepType_register(float32, Float32, NULL);
kerepType_register(float64, Float64, NULL);
kerepType_register(int8, Int8, NULL);
kerepType_register(uint8, UInt8, NULL);
kerepType_register(int16, Int16, NULL);
kerepType_register(uint16, UInt16, NULL);
kerepType_register(int32, Int32, NULL);
kerepType_register(uint32, UInt32, NULL);
kerepType_register(int64, Int64, NULL);
kerepType_register(uint64, UInt64, NULL);
kerepType_register(char*, CharPtr, NULL);
kerepType_register(bool*, BoolPtr, NULL);
kerepType_register(int8, Int8, NULL);
kerepType_register(uint8, UInt8, NULL);
kerepType_register(int16, Int16, NULL);
kerepType_register(uint16, UInt16, NULL);
kerepType_register(int32, Int32, NULL);
kerepType_register(uint32, UInt32, NULL);
kerepType_register(int64, Int64, NULL);
kerepType_register(uint64, UInt64, NULL);
// base type pointers
kerepType_register(char*, CharPtr, NULL);
kerepType_register(bool*, BoolPtr, NULL);
kerepType_register(float32*, Float32Ptr, NULL);
kerepType_register(float64*, Float64Ptr, NULL);
kerepType_register(int8*, Int8Ptr, NULL);
kerepType_register(uint8*, UInt8Ptr, NULL);
kerepType_register(int16*, Int16Ptr, NULL);
kerepType_register(uint16*, UInt16Ptr, NULL);
kerepType_register(int32*, Int32Ptr, NULL);
kerepType_register(uint32*, UInt32Ptr, NULL);
kerepType_register(int64*, Int64Ptr, NULL);
kerepType_register(uint64*, UInt64Ptr, NULL);
kerepType_register(int8*, Int8Ptr, NULL);
kerepType_register(uint8*, UInt8Ptr, NULL);
kerepType_register(int16*, Int16Ptr, NULL);
kerepType_register(uint16*, UInt16Ptr, NULL);
kerepType_register(int32*, Int32Ptr, NULL);
kerepType_register(uint32*, UInt32Ptr, NULL);
kerepType_register(int64*, Int64Ptr, NULL);
kerepType_register(uint64*, UInt64Ptr, NULL);
// base type autoarrs
kerepType_register(Autoarr_char, AutoarrChar, ____Autoarr_free_char);
kerepType_register(Autoarr_bool, AutoarrBool, ____Autoarr_free_bool);
kerepType_register(Autoarr_float32, AutoarrFloat32, ____Autoarr_free_float32);
kerepType_register(Autoarr_float64, AutoarrFloat64, ____Autoarr_free_float64);
kerepType_register(Autoarr_int8, AutoarrInt8, ____Autoarr_free_int8);
kerepType_register(Autoarr_uint8, AutoarrUInt8, ____Autoarr_free_uint8);
kerepType_register(Autoarr_int16, AutoarrInt16, ____Autoarr_free_int16);
kerepType_register(Autoarr_uint16, AutoarrUInt16, ____Autoarr_free_uint16);
kerepType_register(Autoarr_int32, AutoarrInt32, ____Autoarr_free_int32);
kerepType_register(Autoarr_uint32, AutoarrUInt32, ____Autoarr_free_uint32);
kerepType_register(Autoarr_int64, AutoarrInt64, ____Autoarr_free_int64);
kerepType_register(Autoarr_uint64, AutoarrUInt64, ____Autoarr_free_uint64);
// base type autoarr pointers
kerepType_register(Autoarr_char*, AutoarrCharPtr, ____Autoarr_free_char);
kerepType_register(Autoarr_bool*, AutoarrBoolPtr, ____Autoarr_free_bool);
kerepType_register(Autoarr_float32*, AutoarrFloat32Ptr, ____Autoarr_free_float32);
kerepType_register(Autoarr_float64*, AutoarrFloat64Ptr, ____Autoarr_free_float64);
kerepType_register(Autoarr_int8*, AutoarrInt8Ptr, ____Autoarr_free_int8);
kerepType_register(Autoarr_uint8*, AutoarrUInt8Ptr, ____Autoarr_free_uint8);
kerepType_register(Autoarr_int16*, AutoarrInt16Ptr, ____Autoarr_free_int16);
kerepType_register(Autoarr_uint16*, AutoarrUInt16Ptr, ____Autoarr_free_uint16);
kerepType_register(Autoarr_int32*, AutoarrInt32Ptr, ____Autoarr_free_int32);
kerepType_register(Autoarr_uint32*, AutoarrUInt32Ptr, ____Autoarr_free_uint32);
kerepType_register(Autoarr_int64*, AutoarrInt64Ptr, ____Autoarr_free_int64);
kerepType_register(Autoarr_uint64*, AutoarrUInt64Ptr, ____Autoarr_free_uint64);
// Unitype
kerepType_register(Unitype, Unitype, __UnitypePtr_free);
kerepType_register(Unitype*, UnitypePtr, __UnitypePtr_free);
kerepType_register(Autoarr_Unitype, AutoarrUnitype, ____Autoarr_free_Unitype_);
kerepType_register(Autoarr_Unitype*, 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);
kerepType_register(, AutoarrChar, Autoarr_free);
kerepType_register(, AutoarrBool, Autoarr_free);
kerepType_register(, AutoarrFloat32, Autoarr_free);
kerepType_register(, AutoarrFloat64, Autoarr_free);
kerepType_register(, AutoarrInt8, Autoarr_free);
kerepType_register(, AutoarrUInt8, Autoarr_free);
kerepType_register(, AutoarrInt16, Autoarr_free);
kerepType_register(, AutoarrUInt16, Autoarr_free);
kerepType_register(, AutoarrInt32, Autoarr_free);
kerepType_register(, AutoarrUInt32, Autoarr_free);
kerepType_register(, AutoarrInt64, Autoarr_free);
kerepType_register(, AutoarrUInt64, Autoarr_free);
// SearchTreeNode
kerepType_register(STNode, STNode, __STNode_free);
kerepType_register(STNode*, STNodePtr, __STNode_free);
kerepType_register(, AutoarrCharPtr, Autoarr_free);
kerepType_register(, AutoarrBoolPtr, Autoarr_free);
kerepType_register(, AutoarrFloat32Ptr, Autoarr_free);
kerepType_register(, AutoarrFloat64Ptr, Autoarr_free);
kerepType_register(, AutoarrInt8Ptr, Autoarr_free);
kerepType_register(, AutoarrUInt8Ptr, Autoarr_free);
kerepType_register(, AutoarrInt16Ptr, Autoarr_free);
kerepType_register(, AutoarrUInt16Ptr, Autoarr_free);
kerepType_register(, AutoarrInt32Ptr, Autoarr_free);
kerepType_register(, AutoarrUInt32Ptr, Autoarr_free);
kerepType_register(, AutoarrInt64Ptr, Autoarr_free);
kerepType_register(, AutoarrUInt64Ptr, Autoarr_free);
// KeyValuePair
kerepType_register(KVPair, KVPair, __KVPair_free);
kerepType_register(KVPair*, KVPairPtr, __KVPair_free);
kerepType_register(Autoarr_KVPair, AutoarrKVPair, ____Autoarr_free_KVPair_);
kerepType_register(Autoarr_KVPair*, 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);
kerepType_register(, STNode, STNode_free);
kerepType_register(, STNodePtr, STNode_free);
// Hashtable
kerepType_register(Hashtable, Hashtable, __Hashtable_free);
kerepType_register(Hashtable*, HashtablePtr, __Hashtable_free);
kerepType_register(, Hashtable, Hashtable_free);
kerepType_register(, HashtablePtr, Hashtable_free);
// StringBuilder
kerepType_register(StringBuilder, StringBuilder, __StringBuilder_free);
kerepType_register(StringBuilder*, StringBuilderPtr, __StringBuilder_free);
}

View File

@@ -4,8 +4,8 @@
extern "C" {
#endif
// call this between kerepTypesInitBegin() and kerepTypesInitEnd()
void kerepInit();
// call this between kerepTypeDescriptors_beginInit() and kerepTypeDescriptors_endInit()
void kerepTypeDescriptors_initKerepTypes();
#if __cplusplus
}

View File

@@ -44,7 +44,6 @@ typedef double float64;
#pragma GCC error "unknown compiler"
#endif
#ifdef _MSC_VER
#define IFWIN(YES, NO) YES
#define IFMSC(YES, NO) YES
@@ -58,6 +57,9 @@ typedef double float64;
#pragma GCC error "unknown compiler"
#endif
#ifndef sprintf_s
#define sprintf_s(BUF, BUFSIZE, FORMAT, ...) sprintf(BUF, FORMAT, ## __VA_ARGS__)
#endif
#if __cplusplus
}

View File

@@ -22,7 +22,7 @@ void kerepTypeDescriptors_beginInit(){
void kerepTypeDescriptors_endInit(){
typeDescriptors=Autoarr_toArray(__kerepTypeDescriptors);
Autoarr_free(__kerepTypeDescriptors);
Autoarr_free(__kerepTypeDescriptors,true);
if(typeDescriptors==NULL) throw(ERR_NULLPTR);
}
void __kerepType_register(char* name, int16 size, void (*free_members)(void*)){
@@ -35,6 +35,6 @@ void __kerepType_register(char* name, int16 size, void (*free_members)(void*)){
Autoarr_add(__kerepTypeDescriptors, typeDesc);
}
kerepTypeDescriptor typeDescriptor_get(kerepTypeId id){
kerepTypeDescriptor kerepTypeDescriptor_get(kerepTypeId id){
return typeDescriptors[id];
}

View File

@@ -1,113 +0,0 @@
#include "types.h"
#include "errors.h"
#include "../Autoarr/Autoarr.h"
#include "../Hashtable/Hashtable.h"
#include "../SearchTree/SearchTree.h"
// frees VoidPtr value or does nothing if type isn't pointer
void Unitype_free(Unitype u){
switch (u.type) {
case Null:
case Float32:
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
char* sprintuni(Unitype v){
char* buf=malloc(BUFSIZE);
IFMSC(
switch (v.type) {
case Null: sprintf_s(buf, BUFSIZE, "{Null}");break;
case Float64: sprintf_s(buf, BUFSIZE, "{%s : %lf}", my_type_name(v.type),v.Float64);break;
case Bool:
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){
char* s=sprintuni(v);
fputs(s, stdout);
free(s);
}

View File

@@ -7,6 +7,7 @@ extern "C" {
#include "std.h"
typedef uint16 kerepTypeId;
typedef struct kerepTypeDescriptor{
void (*free_members)(void*); // NULL or function which frees all struct members
char* name;
@@ -26,7 +27,7 @@ void __kerepType_register(char* name, int16 size, void (*free_members)(void*));
void kerepTypeDescriptors_beginInit();
void kerepTypeDescriptors_endInit();
kerepTypeDescriptor typeDescriptor_get(kerepTypeId id);
kerepTypeDescriptor kerepTypeDescriptor_get(kerepTypeId id);
kerepType_declare(Null);

View File

@@ -1,10 +1,37 @@
#include "unitype.h"
#include "base.h"
void Unitype_free(Unitype u){
kerepTypeDescriptor type=typeDescriptor_get(u.typeId);
kerepTypeDescriptor type=kerepTypeDescriptor_get(u.typeId);
if(type.free_members)
type.free_members(u.VoidPtr);
if(u.allocatedInHeap)
free(u.VoidPtr);
}
void __UnitypePtr_free(Unitype* u) { Unitype_free(&u); }
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);
}

View File

@@ -23,7 +23,7 @@ kerepType_declare(UnitypePtr);
#define __Uni(TYPE,VAL) (Unitype){\
.TYPE_NAME=VAL, .type=kerepTypeId_##TYPE, .allocatedInHeap=false}
.TYPE_NAME=VAL, .typeId=kerepTypeId_##TYPE, .allocatedInHeap=false}
#define UniInt64(VAL) __Uni(Int64, VAL)
#define UniUInt64(VAL) __Uni(UInt64, VAL)
@@ -41,7 +41,7 @@ kerepType_declare(UnitypePtr);
// frees VoidPtr value or does nothing if type isn't pointer
void Unitype_free(Unitype u);
void __UnitypePtr_free(Unitype* u);
void __UnitypePtr_free(void* u);
void printuni(Unitype v);
char* sprintuni(Unitype v);