kerepType -> kt
This commit is contained in:
@@ -8,9 +8,7 @@ extern "C" {
|
||||
#include "errors.h"
|
||||
#include "cptr.h"
|
||||
#include "optime.h"
|
||||
#include "type_system/types.h"
|
||||
#include "type_system/unitype.h"
|
||||
#include "init.h"
|
||||
#include "type_system/type_system.h"
|
||||
#include "kprint/kprint.h"
|
||||
|
||||
#if __cplusplus
|
||||
|
||||
103
src/base/init.c
103
src/base/init.c
@@ -1,103 +0,0 @@
|
||||
#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, NULL);
|
||||
// base types
|
||||
kerepType_register(char, kerepTypeId_Char, NULL, NULL);
|
||||
kerepType_register(bool, kerepTypeId_Bool, NULL, NULL);
|
||||
kerepType_register(float32, kerepTypeId_Float32, NULL, NULL);
|
||||
kerepType_register(float64, kerepTypeId_Float64, NULL, NULL);
|
||||
kerepType_register(int8, kerepTypeId_Int8, NULL, NULL);
|
||||
kerepType_register(uint8, kerepTypeId_UInt8, NULL, NULL);
|
||||
kerepType_register(int16, kerepTypeId_Int16, NULL, NULL);
|
||||
kerepType_register(uint16, kerepTypeId_UInt16, NULL, NULL);
|
||||
kerepType_register(int32, kerepTypeId_Int32, NULL, NULL);
|
||||
kerepType_register(uint32, kerepTypeId_UInt32, NULL, NULL);
|
||||
kerepType_register(int64, kerepTypeId_Int64, NULL, NULL);
|
||||
kerepType_register(uint64, kerepTypeId_UInt64, NULL, NULL);
|
||||
// base type pointers
|
||||
kerepType_register(char*, kerepTypeId_CharPtr, NULL, NULL);
|
||||
kerepType_register(bool*, kerepTypeId_BoolPtr, NULL, NULL);
|
||||
kerepType_register(float32*, kerepTypeId_Float32Ptr, NULL, NULL);
|
||||
kerepType_register(float64*, kerepTypeId_Float64Ptr, NULL, NULL);
|
||||
kerepType_register(int8*, kerepTypeId_Int8Ptr, NULL, NULL);
|
||||
kerepType_register(uint8*, kerepTypeId_UInt8Ptr, NULL, NULL);
|
||||
kerepType_register(int16*, kerepTypeId_Int16Ptr, NULL, NULL);
|
||||
kerepType_register(uint16*, kerepTypeId_UInt16Ptr, NULL, NULL);
|
||||
kerepType_register(int32*, kerepTypeId_Int32Ptr, NULL, NULL);
|
||||
kerepType_register(uint32*, kerepTypeId_UInt32Ptr, NULL, NULL);
|
||||
kerepType_register(int64*, kerepTypeId_Int64Ptr, NULL, NULL);
|
||||
kerepType_register(uint64*, kerepTypeId_UInt64Ptr, NULL, NULL);
|
||||
// kerepTypeDescriptor
|
||||
kerepType_register(kerepTypeDescriptor, kerepTypeId_kerepTypeDescriptor, NULL, NULL);
|
||||
kerepType_register(kerepTypeDescriptor*, kerepTypeId_kerepTypeDescriptorPtr, NULL, NULL);
|
||||
|
||||
// base type autoarrs
|
||||
kerepType_register(Autoarr_char, kerepTypeId_AutoarrChar, ____Autoarr_free_char, NULL);
|
||||
kerepType_register(Autoarr_bool, kerepTypeId_AutoarrBool, ____Autoarr_free_bool, NULL);
|
||||
kerepType_register(Autoarr_float32, kerepTypeId_AutoarrFloat32, ____Autoarr_free_float32, NULL);
|
||||
kerepType_register(Autoarr_float64, kerepTypeId_AutoarrFloat64, ____Autoarr_free_float64, NULL);
|
||||
kerepType_register(Autoarr_int8, kerepTypeId_AutoarrInt8, ____Autoarr_free_int8, NULL);
|
||||
kerepType_register(Autoarr_uint8, kerepTypeId_AutoarrUInt8, ____Autoarr_free_uint8, NULL);
|
||||
kerepType_register(Autoarr_int16, kerepTypeId_AutoarrInt16, ____Autoarr_free_int16, NULL);
|
||||
kerepType_register(Autoarr_uint16, kerepTypeId_AutoarrUInt16, ____Autoarr_free_uint16, NULL);
|
||||
kerepType_register(Autoarr_int32, kerepTypeId_AutoarrInt32, ____Autoarr_free_int32, NULL);
|
||||
kerepType_register(Autoarr_uint32, kerepTypeId_AutoarrUInt32, ____Autoarr_free_uint32, NULL);
|
||||
kerepType_register(Autoarr_int64, kerepTypeId_AutoarrInt64, ____Autoarr_free_int64, NULL);
|
||||
kerepType_register(Autoarr_uint64, kerepTypeId_AutoarrUInt64, ____Autoarr_free_uint64, NULL);
|
||||
// base type autoarr pointers
|
||||
kerepType_register(Autoarr_char*, kerepTypeId_AutoarrCharPtr, ____Autoarr_free_char, NULL);
|
||||
kerepType_register(Autoarr_bool*, kerepTypeId_AutoarrBoolPtr, ____Autoarr_free_bool, NULL);
|
||||
kerepType_register(Autoarr_float32*, kerepTypeId_AutoarrFloat32Ptr, ____Autoarr_free_float32, NULL);
|
||||
kerepType_register(Autoarr_float64*, kerepTypeId_AutoarrFloat64Ptr, ____Autoarr_free_float64, NULL);
|
||||
kerepType_register(Autoarr_int8*, kerepTypeId_AutoarrInt8Ptr, ____Autoarr_free_int8, NULL);
|
||||
kerepType_register(Autoarr_uint8*, kerepTypeId_AutoarrUInt8Ptr, ____Autoarr_free_uint8, NULL);
|
||||
kerepType_register(Autoarr_int16*, kerepTypeId_AutoarrInt16Ptr, ____Autoarr_free_int16, NULL);
|
||||
kerepType_register(Autoarr_uint16*, kerepTypeId_AutoarrUInt16Ptr, ____Autoarr_free_uint16, NULL);
|
||||
kerepType_register(Autoarr_int32*, kerepTypeId_AutoarrInt32Ptr, ____Autoarr_free_int32, NULL);
|
||||
kerepType_register(Autoarr_uint32*, kerepTypeId_AutoarrUInt32Ptr, ____Autoarr_free_uint32, NULL);
|
||||
kerepType_register(Autoarr_int64*, kerepTypeId_AutoarrInt64Ptr, ____Autoarr_free_int64, NULL);
|
||||
kerepType_register(Autoarr_uint64*, kerepTypeId_AutoarrUInt64Ptr, ____Autoarr_free_uint64, NULL);
|
||||
|
||||
// Unitype
|
||||
kerepType_register(Unitype, kerepTypeId_Unitype, __UnitypePtr_free, NULL);
|
||||
kerepType_register(Unitype*, kerepTypeId_UnitypePtr, __UnitypePtr_free, NULL);
|
||||
kerepType_register(Autoarr_Unitype, kerepTypeId_AutoarrUnitype, ____Autoarr_free_Unitype_, NULL);
|
||||
kerepType_register(Autoarr_Unitype*, kerepTypeId_AutoarrUnitypePtr, ____Autoarr_free_Unitype_, NULL);
|
||||
// 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, NULL);
|
||||
kerepType_register(STNode*, kerepTypeId_STNodePtr, __STNode_free, NULL);
|
||||
|
||||
// KeyValuePair
|
||||
kerepType_register(KVPair, kerepTypeId_KVPair, __KVPair_free, NULL);
|
||||
kerepType_register(KVPair*, kerepTypeId_KVPairPtr, __KVPair_free, NULL);
|
||||
kerepType_register(Autoarr_KVPair, kerepTypeId_AutoarrKVPair, ____Autoarr_free_KVPair_, NULL);
|
||||
kerepType_register(Autoarr_KVPair*, kerepTypeId_AutoarrKVPairPtr, ____Autoarr_free_KVPair_, NULL);
|
||||
// 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, NULL);
|
||||
kerepType_register(Hashtable*, kerepTypeId_HashtablePtr, __Hashtable_free, NULL);
|
||||
|
||||
// string
|
||||
kerepType_register(string, kerepTypeId_string, NULL, NULL);
|
||||
kerepType_register(string*, kerepTypeId_stringPtr, NULL, NULL);
|
||||
kerepType_register(string, kerepTypeId_AutoarrString, ____Autoarr_free_string, NULL);
|
||||
kerepType_register(string*, kerepTypeId_AutoarrStringPtr, ____Autoarr_free_string, NULL);
|
||||
// StringBuilder
|
||||
kerepType_register(StringBuilder, kerepTypeId_StringBuilder, __StringBuilder_free, NULL);
|
||||
kerepType_register(StringBuilder*, kerepTypeId_StringBuilderPtr, __StringBuilder_free, NULL);
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// call this between kerepTypeDescriptors_beginInit() and kerepTypeDescriptors_endInit()
|
||||
void kerepTypeDescriptors_initKerepTypes();
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -10,11 +10,11 @@ I don't really like printf function (and its variants), so i made safer and more
|
||||
## how to use it:
|
||||
+ **format construction:**
|
||||
```
|
||||
kprint_format fmt= kprint_fgColor | kprint_fbgColor | kprint_fdataFmt | flags | kerepTypeId;
|
||||
kprint_format fmt= kprint_fgColor | kprint_fbgColor | kprint_fdataFmt | flags | ktId;
|
||||
```
|
||||
[more about `kprint_format`](kprint_format.md)
|
||||
+ fgColor and bgColor can be set to change console output color
|
||||
+ you should set dataFormat for `int`/`uint`/`float`/`char*` arguments and kerepTypeId for other types
|
||||
+ you should set dataFormat for `int`/`uint`/`float`/`char*` arguments and ktId for other types
|
||||
+ flags can be set to modify TypeDescriptor.toString() behavior
|
||||
+ don't forget to set TypeDescriptor.toString when registering type, or kprint will crash
|
||||
|
||||
@@ -28,6 +28,6 @@ I don't really like printf function (and its variants), so i made safer and more
|
||||
should be sent as pointers
|
||||
```
|
||||
Maybe m=MaybeNull;
|
||||
kprint(kprint_fgBlue | kprint_fmtString, "Maybe: ", kprint_fgGreen | kerepTypeId_MaybePtr, &m);
|
||||
kprint(kprint_fgBlue | kprint_fmtString, "Maybe: ", kprint_fgGreen | ktId_MaybePtr, &m);
|
||||
```
|
||||
output: <span style="color:blue">Maybe:</span> <span style="color:lightgreen">{value={0, kerepTypeId_Null}}</span>
|
||||
output: <span style="color:blue">Maybe:</span> <span style="color:lightgreen">{value={0, ktId_Null}}</span>
|
||||
@@ -7,20 +7,20 @@ Maybe __next_toString(kprint_format format, void* object){
|
||||
case kprint_fmtInt:
|
||||
case kprint_fmtHex:
|
||||
case kprint_fmtBin:
|
||||
format.typeId=kerepTypeId_Int64; break;
|
||||
format.typeId=ktId_Int64; break;
|
||||
case kprint_fmtUInt:
|
||||
format.typeId=kerepTypeId_UInt64; break;
|
||||
format.typeId=ktId_UInt64; break;
|
||||
case kprint_fmtFloat:
|
||||
format.typeId=kerepTypeId_Float64; break;
|
||||
format.typeId=ktId_Float64; break;
|
||||
case kprint_fmtChar:
|
||||
format.typeId=kerepTypeId_Char; break;
|
||||
format.typeId=ktId_Char; break;
|
||||
case kprint_fmtString:
|
||||
format.typeId=kerepTypeId_CharPtr; break;
|
||||
format.typeId=ktId_CharPtr; break;
|
||||
default:
|
||||
safethrow("typeId is not set, can't autodetect type",;);
|
||||
}
|
||||
try(kerepTypeDescriptor_get(format.typeId),mtd,;);
|
||||
kerepTypeDescriptor typeDesc=*(kerepTypeDescriptor*)mtd.value.VoidPtr;
|
||||
try(ktDescriptor_get(format.typeId),mtd,;);
|
||||
ktDescriptor typeDesc=*(ktDescriptor*)mtd.value.VoidPtr;
|
||||
if(!typeDesc.toString)
|
||||
safethrow("type descriptor doesnt have toString() func",;);
|
||||
try(typeDesc.toString(object, &format), mStr,;);
|
||||
@@ -34,7 +34,7 @@ Maybe __ksprint(uint8 n, kprint_format* formats, void** objects){
|
||||
StringBuilder_append_cptr(strb, mStr.value.VoidPtr);
|
||||
}
|
||||
char* rezult=StringBuilder_build(strb).ptr;
|
||||
return SUCCESS(UniHeap(kerepTypeId_CharPtr, rezult));
|
||||
return SUCCESS(UniHeap(ktId_CharPtr, rezult));
|
||||
}
|
||||
|
||||
Maybe __kfprint(FILE* file, uint8 n, kprint_format* formats, void** objects){
|
||||
@@ -68,9 +68,9 @@ void kprint_setColor(kprint_format f){
|
||||
|
||||
|
||||
|
||||
Maybe ksprint_ar(uint32 count, kprint_format format, kerepTypeId typeId, void* array){
|
||||
try(kerepTypeDescriptor_get(format.typeId),mtd,;);
|
||||
kerepTypeDescriptor typeDesc=*(kerepTypeDescriptor*)mtd.value.VoidPtr;
|
||||
Maybe ksprint_ar(uint32 count, kprint_format format, ktId typeId, void* array){
|
||||
try(ktDescriptor_get(format.typeId),mtd,;);
|
||||
ktDescriptor typeDesc=*(ktDescriptor*)mtd.value.VoidPtr;
|
||||
if(!typeDesc.toString)
|
||||
safethrow("type descriptor doesnt have toString() func",;);
|
||||
StringBuilder* strb=StringBuilder_create();
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../type_system/kerepTypeId.h"
|
||||
#include "../type_system/ktId.h"
|
||||
|
||||
typedef enum kprint_dataFormat{
|
||||
// 00000000 00000000 00000000 00000000
|
||||
@@ -39,7 +39,7 @@ typedef struct {
|
||||
unsigned char fgColor : 4;
|
||||
unsigned char bgColor : 4;
|
||||
unsigned char dataFmt : 4;
|
||||
kerepTypeId typeId;
|
||||
ktId typeId;
|
||||
} kprint_format;
|
||||
|
||||
#if __cplusplus
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
```
|
||||
00000000 00000000 00000000 00000000
|
||||
fgColorSet┘│││└┼┴┘ └┼┴┘└┴┴┤ kerepTypeId
|
||||
fgColorSet┘│││└┼┴┘ └┼┴┘└┴┴┤ ktId
|
||||
bgColorSet─┘││ │ bgColor └data format
|
||||
prefix┬────┘│ └fgColor
|
||||
postfix └uppercase
|
||||
|
||||
103
src/base/type_system/init.c
Normal file
103
src/base/type_system/init.c
Normal file
@@ -0,0 +1,103 @@
|
||||
#include "../base.h"
|
||||
#include "../../Autoarr/Autoarr.h"
|
||||
#include "../../SearchTree/SearchTree.h"
|
||||
#include "../../Hashtable/Hashtable.h"
|
||||
#include "../../String/StringBuilder.h"
|
||||
|
||||
void ktDescriptors_initKerepTypes(){
|
||||
// null
|
||||
kt_register(NULL, ktId_Null, NULL, NULL);
|
||||
// base types
|
||||
kt_register(char, ktId_Char, NULL, NULL);
|
||||
kt_register(bool, ktId_Bool, NULL, NULL);
|
||||
kt_register(float32, ktId_Float32, NULL, NULL);
|
||||
kt_register(float64, ktId_Float64, NULL, NULL);
|
||||
kt_register(int8, ktId_Int8, NULL, NULL);
|
||||
kt_register(uint8, ktId_UInt8, NULL, NULL);
|
||||
kt_register(int16, ktId_Int16, NULL, NULL);
|
||||
kt_register(uint16, ktId_UInt16, NULL, NULL);
|
||||
kt_register(int32, ktId_Int32, NULL, NULL);
|
||||
kt_register(uint32, ktId_UInt32, NULL, NULL);
|
||||
kt_register(int64, ktId_Int64, NULL, NULL);
|
||||
kt_register(uint64, ktId_UInt64, NULL, NULL);
|
||||
// base type pointers
|
||||
kt_register(char*, ktId_CharPtr, NULL, NULL);
|
||||
kt_register(bool*, ktId_BoolPtr, NULL, NULL);
|
||||
kt_register(float32*, ktId_Float32Ptr, NULL, NULL);
|
||||
kt_register(float64*, ktId_Float64Ptr, NULL, NULL);
|
||||
kt_register(int8*, ktId_Int8Ptr, NULL, NULL);
|
||||
kt_register(uint8*, ktId_UInt8Ptr, NULL, NULL);
|
||||
kt_register(int16*, ktId_Int16Ptr, NULL, NULL);
|
||||
kt_register(uint16*, ktId_UInt16Ptr, NULL, NULL);
|
||||
kt_register(int32*, ktId_Int32Ptr, NULL, NULL);
|
||||
kt_register(uint32*, ktId_UInt32Ptr, NULL, NULL);
|
||||
kt_register(int64*, ktId_Int64Ptr, NULL, NULL);
|
||||
kt_register(uint64*, ktId_UInt64Ptr, NULL, NULL);
|
||||
// ktDescriptor
|
||||
kt_register(ktDescriptor, ktId_ktDescriptor, NULL, NULL);
|
||||
kt_register(ktDescriptor*, ktId_ktDescriptorPtr, NULL, NULL);
|
||||
|
||||
// base type autoarrs
|
||||
kt_register(Autoarr_char, ktId_AutoarrChar, ____Autoarr_free_char, NULL);
|
||||
kt_register(Autoarr_bool, ktId_AutoarrBool, ____Autoarr_free_bool, NULL);
|
||||
kt_register(Autoarr_float32, ktId_AutoarrFloat32, ____Autoarr_free_float32, NULL);
|
||||
kt_register(Autoarr_float64, ktId_AutoarrFloat64, ____Autoarr_free_float64, NULL);
|
||||
kt_register(Autoarr_int8, ktId_AutoarrInt8, ____Autoarr_free_int8, NULL);
|
||||
kt_register(Autoarr_uint8, ktId_AutoarrUInt8, ____Autoarr_free_uint8, NULL);
|
||||
kt_register(Autoarr_int16, ktId_AutoarrInt16, ____Autoarr_free_int16, NULL);
|
||||
kt_register(Autoarr_uint16, ktId_AutoarrUInt16, ____Autoarr_free_uint16, NULL);
|
||||
kt_register(Autoarr_int32, ktId_AutoarrInt32, ____Autoarr_free_int32, NULL);
|
||||
kt_register(Autoarr_uint32, ktId_AutoarrUInt32, ____Autoarr_free_uint32, NULL);
|
||||
kt_register(Autoarr_int64, ktId_AutoarrInt64, ____Autoarr_free_int64, NULL);
|
||||
kt_register(Autoarr_uint64, ktId_AutoarrUInt64, ____Autoarr_free_uint64, NULL);
|
||||
// base type autoarr pointers
|
||||
kt_register(Autoarr_char*, ktId_AutoarrCharPtr, ____Autoarr_free_char, NULL);
|
||||
kt_register(Autoarr_bool*, ktId_AutoarrBoolPtr, ____Autoarr_free_bool, NULL);
|
||||
kt_register(Autoarr_float32*, ktId_AutoarrFloat32Ptr, ____Autoarr_free_float32, NULL);
|
||||
kt_register(Autoarr_float64*, ktId_AutoarrFloat64Ptr, ____Autoarr_free_float64, NULL);
|
||||
kt_register(Autoarr_int8*, ktId_AutoarrInt8Ptr, ____Autoarr_free_int8, NULL);
|
||||
kt_register(Autoarr_uint8*, ktId_AutoarrUInt8Ptr, ____Autoarr_free_uint8, NULL);
|
||||
kt_register(Autoarr_int16*, ktId_AutoarrInt16Ptr, ____Autoarr_free_int16, NULL);
|
||||
kt_register(Autoarr_uint16*, ktId_AutoarrUInt16Ptr, ____Autoarr_free_uint16, NULL);
|
||||
kt_register(Autoarr_int32*, ktId_AutoarrInt32Ptr, ____Autoarr_free_int32, NULL);
|
||||
kt_register(Autoarr_uint32*, ktId_AutoarrUInt32Ptr, ____Autoarr_free_uint32, NULL);
|
||||
kt_register(Autoarr_int64*, ktId_AutoarrInt64Ptr, ____Autoarr_free_int64, NULL);
|
||||
kt_register(Autoarr_uint64*, ktId_AutoarrUInt64Ptr, ____Autoarr_free_uint64, NULL);
|
||||
|
||||
// Unitype
|
||||
kt_register(Unitype, ktId_Unitype, __UnitypePtr_free, NULL);
|
||||
kt_register(Unitype*, ktId_UnitypePtr, __UnitypePtr_free, NULL);
|
||||
kt_register(Autoarr_Unitype, ktId_AutoarrUnitype, ____Autoarr_free_Unitype_, NULL);
|
||||
kt_register(Autoarr_Unitype*, ktId_AutoarrUnitypePtr, ____Autoarr_free_Unitype_, NULL);
|
||||
// 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, ktId_STNode, __STNode_free, NULL);
|
||||
kt_register(STNode*, ktId_STNodePtr, __STNode_free, NULL);
|
||||
|
||||
// KeyValuePair
|
||||
kt_register(KVPair, ktId_KVPair, __KVPair_free, NULL);
|
||||
kt_register(KVPair*, ktId_KVPairPtr, __KVPair_free, NULL);
|
||||
kt_register(Autoarr_KVPair, ktId_AutoarrKVPair, ____Autoarr_free_KVPair_, NULL);
|
||||
kt_register(Autoarr_KVPair*, ktId_AutoarrKVPairPtr, ____Autoarr_free_KVPair_, NULL);
|
||||
// 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, ktId_Hashtable, __Hashtable_free, NULL);
|
||||
kt_register(Hashtable*, ktId_HashtablePtr, __Hashtable_free, NULL);
|
||||
|
||||
// string
|
||||
kt_register(string, ktId_string, NULL, NULL);
|
||||
kt_register(string*, ktId_stringPtr, NULL, NULL);
|
||||
kt_register(string, ktId_AutoarrString, ____Autoarr_free_string, NULL);
|
||||
kt_register(string*, ktId_AutoarrStringPtr, ____Autoarr_free_string, NULL);
|
||||
// StringBuilder
|
||||
kt_register(StringBuilder, ktId_StringBuilder, __StringBuilder_free, NULL);
|
||||
kt_register(StringBuilder*, ktId_StringBuilderPtr, __StringBuilder_free, NULL);
|
||||
}
|
||||
12
src/base/type_system/init.h
Normal file
12
src/base/type_system/init.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// call this between ktDescriptors_beginInit() and ktDescriptors_endInit()
|
||||
void ktDescriptors_initKerepTypes();
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,17 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../std.h"
|
||||
typedef uint16 kerepTypeId;
|
||||
|
||||
#define kerepTypeId_declare(ID_VAR_NAME)\
|
||||
extern kerepTypeId ID_VAR_NAME
|
||||
#define kerepTypeId_define(ID_VAR_NAME)\
|
||||
kerepTypeId ID_VAR_NAME=-1
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -6,16 +6,16 @@ extern "C" {
|
||||
|
||||
#include "../std.h"
|
||||
#include "../errors.h"
|
||||
#include "kerepTypeId.h"
|
||||
#include "ktId.h"
|
||||
|
||||
typedef struct kerepTypeDescriptor{
|
||||
typedef struct ktDescriptor{
|
||||
char* name;
|
||||
kerepTypeId id;
|
||||
ktId id;
|
||||
uint16 size;
|
||||
void (*freeMembers)(void*); // NULL or function which frees all struct members
|
||||
///@return Maybe<char*>
|
||||
Maybe (*toString)(void*, int32); // NULL or function which generates string representaion of object
|
||||
} kerepTypeDescriptor;
|
||||
} ktDescriptor;
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
17
src/base/type_system/ktId.h
Normal file
17
src/base/type_system/ktId.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../std.h"
|
||||
typedef uint16 ktId;
|
||||
|
||||
#define ktId_declare(ID_VAR_NAME)\
|
||||
extern ktId ID_VAR_NAME
|
||||
#define ktId_define(ID_VAR_NAME)\
|
||||
ktId ID_VAR_NAME=-1
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
75
src/base/type_system/kt_functions.c
Normal file
75
src/base/type_system/kt_functions.c
Normal file
@@ -0,0 +1,75 @@
|
||||
#include "../../Autoarr/Autoarr.h"
|
||||
|
||||
Autoarr_declare(ktDescriptor)
|
||||
Autoarr_define(ktDescriptor)
|
||||
|
||||
ktId_define(ktId_Null);
|
||||
|
||||
ktId_define(ktId_Char);
|
||||
ktId_define(ktId_Bool);
|
||||
ktId_define(ktId_Float32);
|
||||
ktId_define(ktId_Float64);
|
||||
ktId_define(ktId_Int8);
|
||||
ktId_define(ktId_UInt8);
|
||||
ktId_define(ktId_Int16);
|
||||
ktId_define(ktId_UInt16);
|
||||
ktId_define(ktId_Int32);
|
||||
ktId_define(ktId_UInt32);
|
||||
ktId_define(ktId_Int64);
|
||||
ktId_define(ktId_UInt64);
|
||||
|
||||
ktId_define(ktId_CharPtr);
|
||||
ktId_define(ktId_BoolPtr);
|
||||
ktId_define(ktId_Float32Ptr);
|
||||
ktId_define(ktId_Float64Ptr);
|
||||
ktId_define(ktId_Int8Ptr);
|
||||
ktId_define(ktId_UInt8Ptr);
|
||||
ktId_define(ktId_Int16Ptr);
|
||||
ktId_define(ktId_UInt16Ptr);
|
||||
ktId_define(ktId_Int32Ptr);
|
||||
ktId_define(ktId_UInt32Ptr);
|
||||
ktId_define(ktId_Int64Ptr);
|
||||
ktId_define(ktId_UInt64Ptr);
|
||||
|
||||
ktId_define(ktId_ktDescriptor);
|
||||
ktId_define(ktId_ktDescriptorPtr);
|
||||
|
||||
// type descriptors are stored here during initialization
|
||||
Autoarr(ktDescriptor)* __ktDescriptors=NULL;
|
||||
// here type descriptors are stored when initialization is complited
|
||||
ktDescriptor* typeDescriptors=NULL;
|
||||
ktId ktId_last=-1;
|
||||
|
||||
typedef enum{
|
||||
NotInitialized, Initializing, Initialized
|
||||
} ktDescriptorsState;
|
||||
ktDescriptorsState initState=NotInitialized;
|
||||
|
||||
void ktDescriptors_beginInit(){
|
||||
printf("\e[94mtype descriptors initializing...\n");
|
||||
__ktDescriptors=Autoarr_create(ktDescriptor, 256, 256);
|
||||
if(__ktDescriptors==NULL) throw(ERR_NULLPTR);
|
||||
}
|
||||
|
||||
void ktDescriptors_endInit(){
|
||||
typeDescriptors=Autoarr_toArray(__ktDescriptors);
|
||||
Autoarr_free(__ktDescriptors,true);
|
||||
if(typeDescriptors==NULL) throw(ERR_NULLPTR);
|
||||
printf("\e[92minitialized %u type descriptors\n", ktId_last);
|
||||
}
|
||||
|
||||
void __kt_register(char* name, int16 size, void (*freeMembers)(void*), Maybe (*toString)(void*, int32)){
|
||||
ktDescriptor typeDesc={
|
||||
.name=name,
|
||||
.size=size,
|
||||
.id=++ktId_last,
|
||||
.freeMembers=freeMembers,
|
||||
.toString=toString
|
||||
};
|
||||
Autoarr_add(__ktDescriptors, typeDesc);
|
||||
}
|
||||
|
||||
Maybe ktDescriptor_get(ktId id){
|
||||
if(id>ktId_last) safethrow("invalid type id",;);
|
||||
return SUCCESS(UniStack(ktId_ktDescriptorPtr, &typeDescriptors[id]));
|
||||
}
|
||||
59
src/base/type_system/kt_functions.h
Normal file
59
src/base/type_system/kt_functions.h
Normal file
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../std.h"
|
||||
#include "ktId.h"
|
||||
#include "ktDescriptor.h"
|
||||
|
||||
|
||||
extern ktId ktId_last;
|
||||
void __kt_register(char* name, int16 size, void (*freeMembers)(void*), Maybe (*toString)(void*, int32));
|
||||
|
||||
#define kt_register(TYPE, ID_VAR_NAME, FREE_MEMBERS_FUNC, TO_STRING_FUNC)\
|
||||
__kt_register(#ID_VAR_NAME, sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\
|
||||
ID_VAR_NAME=ktId_last;
|
||||
|
||||
void ktDescriptors_beginInit();
|
||||
void ktDescriptors_endInit();
|
||||
/// @param id id of registered type
|
||||
/// @return Maybe<ktDescriptor*>
|
||||
Maybe ktDescriptor_get(ktId id);
|
||||
|
||||
|
||||
ktId_declare(ktId_Null);
|
||||
|
||||
ktId_declare(ktId_Char);
|
||||
ktId_declare(ktId_Bool);
|
||||
ktId_declare(ktId_Float32);
|
||||
ktId_declare(ktId_Float64);
|
||||
ktId_declare(ktId_Int8);
|
||||
ktId_declare(ktId_UInt8);
|
||||
ktId_declare(ktId_Int16);
|
||||
ktId_declare(ktId_UInt16);
|
||||
ktId_declare(ktId_Int32);
|
||||
ktId_declare(ktId_UInt32);
|
||||
ktId_declare(ktId_Int64);
|
||||
ktId_declare(ktId_UInt64);
|
||||
|
||||
ktId_declare(ktId_CharPtr);
|
||||
ktId_declare(ktId_BoolPtr);
|
||||
ktId_declare(ktId_Float32Ptr);
|
||||
ktId_declare(ktId_Float64Ptr);
|
||||
ktId_declare(ktId_Int8Ptr);
|
||||
ktId_declare(ktId_UInt8Ptr);
|
||||
ktId_declare(ktId_Int16Ptr);
|
||||
ktId_declare(ktId_UInt16Ptr);
|
||||
ktId_declare(ktId_Int32Ptr);
|
||||
ktId_declare(ktId_UInt32Ptr);
|
||||
ktId_declare(ktId_Int64Ptr);
|
||||
ktId_declare(ktId_UInt64Ptr);
|
||||
|
||||
ktId_declare(ktId_ktDescriptor);
|
||||
ktId_declare(ktId_ktDescriptorPtr);
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
4
src/base/type_system/type_system.h
Normal file
4
src/base/type_system/type_system.h
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
#include "init.h"
|
||||
#include "kt_functions.h"
|
||||
#include "unitype.h"
|
||||
@@ -1,76 +0,0 @@
|
||||
#include "../../Autoarr/Autoarr.h"
|
||||
#include "types.h"
|
||||
|
||||
Autoarr_declare(kerepTypeDescriptor)
|
||||
Autoarr_define(kerepTypeDescriptor)
|
||||
|
||||
kerepTypeId_define(kerepTypeId_Null);
|
||||
|
||||
kerepTypeId_define(kerepTypeId_Char);
|
||||
kerepTypeId_define(kerepTypeId_Bool);
|
||||
kerepTypeId_define(kerepTypeId_Float32);
|
||||
kerepTypeId_define(kerepTypeId_Float64);
|
||||
kerepTypeId_define(kerepTypeId_Int8);
|
||||
kerepTypeId_define(kerepTypeId_UInt8);
|
||||
kerepTypeId_define(kerepTypeId_Int16);
|
||||
kerepTypeId_define(kerepTypeId_UInt16);
|
||||
kerepTypeId_define(kerepTypeId_Int32);
|
||||
kerepTypeId_define(kerepTypeId_UInt32);
|
||||
kerepTypeId_define(kerepTypeId_Int64);
|
||||
kerepTypeId_define(kerepTypeId_UInt64);
|
||||
|
||||
kerepTypeId_define(kerepTypeId_CharPtr);
|
||||
kerepTypeId_define(kerepTypeId_BoolPtr);
|
||||
kerepTypeId_define(kerepTypeId_Float32Ptr);
|
||||
kerepTypeId_define(kerepTypeId_Float64Ptr);
|
||||
kerepTypeId_define(kerepTypeId_Int8Ptr);
|
||||
kerepTypeId_define(kerepTypeId_UInt8Ptr);
|
||||
kerepTypeId_define(kerepTypeId_Int16Ptr);
|
||||
kerepTypeId_define(kerepTypeId_UInt16Ptr);
|
||||
kerepTypeId_define(kerepTypeId_Int32Ptr);
|
||||
kerepTypeId_define(kerepTypeId_UInt32Ptr);
|
||||
kerepTypeId_define(kerepTypeId_Int64Ptr);
|
||||
kerepTypeId_define(kerepTypeId_UInt64Ptr);
|
||||
|
||||
kerepTypeId_define(kerepTypeId_kerepTypeDescriptor);
|
||||
kerepTypeId_define(kerepTypeId_kerepTypeDescriptorPtr);
|
||||
|
||||
// type descriptors are stored here during initialization
|
||||
Autoarr(kerepTypeDescriptor)* __kerepTypeDescriptors=NULL;
|
||||
// here type descriptors are stored when initialization is complited
|
||||
kerepTypeDescriptor* typeDescriptors=NULL;
|
||||
kerepTypeId kerepTypeId_last=-1;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void kerepTypeDescriptors_endInit(){
|
||||
typeDescriptors=Autoarr_toArray(__kerepTypeDescriptors);
|
||||
Autoarr_free(__kerepTypeDescriptors,true);
|
||||
if(typeDescriptors==NULL) throw(ERR_NULLPTR);
|
||||
printf("\e[92minitialized %u type descriptors\n", kerepTypeId_last);
|
||||
}
|
||||
|
||||
void __kerepType_register(char* name, int16 size, void (*freeMembers)(void*), Maybe (*toString)(void*, int32)){
|
||||
kerepTypeDescriptor typeDesc={
|
||||
.name=name,
|
||||
.size=size,
|
||||
.id=++kerepTypeId_last,
|
||||
.freeMembers=freeMembers,
|
||||
.toString=toString
|
||||
};
|
||||
Autoarr_add(__kerepTypeDescriptors, typeDesc);
|
||||
}
|
||||
|
||||
Maybe kerepTypeDescriptor_get(kerepTypeId id){
|
||||
if(id>kerepTypeId_last) safethrow("invalid type id",;);
|
||||
return SUCCESS(UniStack(kerepTypeId_kerepTypeDescriptorPtr, &typeDescriptors[id]));
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../std.h"
|
||||
#include "kerepTypeId.h"
|
||||
#include "kerepTypeDescriptor.h"
|
||||
|
||||
|
||||
extern kerepTypeId kerepTypeId_last;
|
||||
void __kerepType_register(char* name, int16 size, void (*freeMembers)(void*), Maybe (*toString)(void*, int32));
|
||||
|
||||
#define kerepType_register(TYPE, ID_VAR_NAME, FREE_MEMBERS_FUNC, TO_STRING_FUNC)\
|
||||
__kerepType_register(#ID_VAR_NAME, sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\
|
||||
ID_VAR_NAME=kerepTypeId_last;
|
||||
|
||||
void kerepTypeDescriptors_beginInit();
|
||||
void kerepTypeDescriptors_endInit();
|
||||
/// @param id id of registered type
|
||||
/// @return Maybe<kerepTypeDescriptor*>
|
||||
Maybe kerepTypeDescriptor_get(kerepTypeId id);
|
||||
|
||||
|
||||
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);
|
||||
|
||||
kerepTypeId_declare(kerepTypeId_kerepTypeDescriptor);
|
||||
kerepTypeId_declare(kerepTypeId_kerepTypeDescriptorPtr);
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,11 +1,11 @@
|
||||
#include "../base.h"
|
||||
|
||||
kerepTypeId_define(kerepTypeId_Unitype);
|
||||
kerepTypeId_define(kerepTypeId_UnitypePtr);
|
||||
ktId_define(ktId_Unitype);
|
||||
ktId_define(ktId_UnitypePtr);
|
||||
|
||||
void Unitype_free(Unitype u){
|
||||
tryLast(kerepTypeDescriptor_get(u.typeId), mType);
|
||||
kerepTypeDescriptor type=*(kerepTypeDescriptor*)mType.value.VoidPtr;
|
||||
tryLast(ktDescriptor_get(u.typeId), mType);
|
||||
ktDescriptor type=*(ktDescriptor*)mType.value.VoidPtr;
|
||||
if(type.freeMembers)
|
||||
type.freeMembers(u.VoidPtr);
|
||||
if(u.allocatedInHeap)
|
||||
@@ -16,17 +16,17 @@ void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); }
|
||||
#define BUFSIZE 64
|
||||
char* sprintuni(Unitype v){
|
||||
char* buf=malloc(BUFSIZE);
|
||||
tryLast(kerepTypeDescriptor_get(v.typeId), mType);
|
||||
kerepTypeDescriptor type=*(kerepTypeDescriptor*)mType.value.VoidPtr;
|
||||
if(v.typeId==kerepTypeId_Null)
|
||||
tryLast(ktDescriptor_get(v.typeId), mType);
|
||||
ktDescriptor type=*(ktDescriptor*)mType.value.VoidPtr;
|
||||
if(v.typeId==ktId_Null)
|
||||
sprintf_s(buf, BUFSIZE, "{Null}");
|
||||
else if(v.typeId==kerepTypeId_Float64)
|
||||
else if(v.typeId==ktId_Float64)
|
||||
sprintf_s(buf, BUFSIZE, "{%s : %lf}", type.name,v.Float64);
|
||||
else if(v.typeId==kerepTypeId_Bool || v.typeId==kerepTypeId_UInt64)
|
||||
else if(v.typeId==ktId_Bool || v.typeId==ktId_UInt64)
|
||||
sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%llu", "%lu") "}", type.name,v.UInt64);
|
||||
else if(v.typeId==kerepTypeId_Int64)
|
||||
else if(v.typeId==ktId_Int64)
|
||||
sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%lld", "%ld") "}", type.name,v.Int64);
|
||||
else if(v.typeId==kerepTypeId_CharPtr){
|
||||
else if(v.typeId==ktId_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);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "kerepTypeId.h"
|
||||
#include "ktId.h"
|
||||
|
||||
typedef struct Unitype{
|
||||
union {
|
||||
@@ -15,15 +15,15 @@ typedef struct Unitype{
|
||||
void* VoidPtr;
|
||||
char Bytes[8];
|
||||
};
|
||||
kerepTypeId typeId;
|
||||
ktId typeId;
|
||||
bool allocatedInHeap; // should Unitype_free call free() to VoidPtr*
|
||||
} Unitype;
|
||||
kerepTypeId_declare(kerepTypeId_Unitype);
|
||||
kerepTypeId_declare(kerepTypeId_UnitypePtr);
|
||||
ktId_declare(ktId_Unitype);
|
||||
ktId_declare(ktId_UnitypePtr);
|
||||
|
||||
|
||||
#define __UniDef(TYPE, VAL) (Unitype){\
|
||||
.TYPE=VAL, .typeId=kerepTypeId_##TYPE, .allocatedInHeap=false}
|
||||
.TYPE=VAL, .typeId=ktId_##TYPE, .allocatedInHeap=false}
|
||||
|
||||
#define UniInt64(VAL) __UniDef(Int64, VAL)
|
||||
#define UniUInt64(VAL) __UniDef(UInt64, VAL)
|
||||
@@ -35,7 +35,7 @@ kerepTypeId_declare(kerepTypeId_UnitypePtr);
|
||||
#define UniHeap(ID_VAR_NAME, VAL) (Unitype){\
|
||||
.VoidPtr=VAL, .typeId=ID_VAR_NAME, .allocatedInHeap=true}
|
||||
|
||||
#define UniNull UniStack(kerepTypeId_Null, NULL)
|
||||
#define UniNull UniStack(ktId_Null, NULL)
|
||||
#define UniTrue UniBool(true)
|
||||
#define UniFalse UniBool(false)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user