breaking changes in type system

This commit is contained in:
Timerix22 2023-02-13 00:34:32 +06:00
parent 305854e721
commit 95fec8d166
48 changed files with 704 additions and 608 deletions

View File

@ -12,6 +12,7 @@ Array_define(i32)
Array_define(u32) Array_define(u32)
Array_define(i64) Array_define(i64)
Array_define(u64) Array_define(u64)
Array_define(Pointer)
Array_define(Unitype) Array_define(Unitype)

View File

@ -19,6 +19,7 @@ Array_declare(i32)
Array_declare(u32) Array_declare(u32)
Array_declare(i64) Array_declare(i64)
Array_declare(u64) Array_declare(u64)
Array_declare(Pointer)
Array_declare(Unitype) Array_declare(Unitype)

View File

@ -6,34 +6,32 @@ extern "C" {
#include "../base/base.h" #include "../base/base.h"
#define Array_declare(type)\ #define Array_declare(type) \
typedef struct Array_##type{\ STRUCT(Array_##type, \
type* values;\ type* values; \
u32 length;\ u32 length; \
bool allocatedOnHeap;\ bool allocatedOnHeap; \
} Array_##type;\ ) \
\ \
ktid_declare(Array_##type);\ static inline Array_##type Array_##type##_allocValues(u32 length){ \
return (Array_##type) { \
.values=(type*)malloc(sizeof(type)*length), \
.length=length, \
.allocatedOnHeap=true \
}; \
} \
\ \
static inline Array_##type Array_##type##_allocValues(u32 length){\ static inline Array_##type Array_##type##_fromBuffer(type* buffer, u32 bufferLength, bool allocatedOnHeap){ \
return (Array_##type) {\ return (Array_##type) { \
.values=(type*)malloc(sizeof(type)*length),\ .values=buffer, \
.length=length,\ .length=bufferLength, \
.allocatedOnHeap=true\ .allocatedOnHeap=allocatedOnHeap \
};\ }; \
}\ } \
\ \
static inline Array_##type Array_##type##_fromBuffer(type* buffer, u32 bufferLength, bool allocatedOnHeap){\ static inline void Array_##type##_free(Array_##type* array){ \
return (Array_##type) {\ if(array->allocatedOnHeap) \
.values=buffer,\ free(array->values); \
.length=bufferLength,\
.allocatedOnHeap=allocatedOnHeap\
};\
}\
\
static inline void Array_##type##_free(Array_##type* array){\
if(array->allocatedOnHeap)\
free(array->values);\
} }
#if __cplusplus #if __cplusplus

View File

@ -6,8 +6,8 @@ extern "C" {
#include "../base/base.h" #include "../base/base.h"
#define Array_define(type)\ #define Array_define(type) \
ktid_define(Array_##type); kt_define(Array_##type, (freeMembers_t)Array_##type##_free, NULL);
#if __cplusplus #if __cplusplus
} }

View File

@ -12,3 +12,4 @@ Autoarr_define(u32)
Autoarr_define(i32) Autoarr_define(i32)
Autoarr_define(u64) Autoarr_define(u64)
Autoarr_define(i64) Autoarr_define(i64)
Autoarr_define(Pointer)

View File

@ -20,7 +20,7 @@ Autoarr_declare(i32)
Autoarr_declare(u32) Autoarr_declare(u32)
Autoarr_declare(i64) Autoarr_declare(i64)
Autoarr_declare(u64) Autoarr_declare(u64)
Autoarr_declare(Pointer)
#if __cplusplus #if __cplusplus
} }

View File

@ -13,19 +13,19 @@ Autoarr_declare(Unitype)
void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr); void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr);
void ____Autoarr_free_Unitype_(void* ar); 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) { \
typeof(**ar->values) elem;\ typeof(**ar->values) elem; \
for(u32 blockI=0;blockI<ar->blocks_count-1;blockI++)\ for(u32 blockI=0;blockI<ar->blocks_count-1;blockI++) \
for(u32 elemI=0;elemI<ar->max_block_length;elemI++){\ for(u32 elemI=0;elemI<ar->max_block_length;elemI++){ \
elem=ar->values[blockI][elemI];\ elem=ar->values[blockI][elemI]; \
(codeblock);\ (codeblock); \
}\ } \
for(u32 elemI=0;elemI<ar->block_length;elemI++){\ for(u32 elemI=0;elemI<ar->block_length;elemI++){ \
elem=ar->values[ar->blocks_count-1][elemI];\ elem=ar->values[ar->blocks_count-1][elemI]; \
(codeblock);\ (codeblock); \
}\ } \
}\ } \
}) })
#if __cplusplus #if __cplusplus

View File

@ -6,64 +6,62 @@ extern "C" {
#include "../base/base.h" #include "../base/base.h"
#define Autoarr_declare(type)\ #define Autoarr_declare(type) \
\ \
struct Autoarr_##type;\ struct Autoarr_##type; \
\ \
typedef struct {\ STRUCT(__functions_list_t_##type, \
void (*add)(struct Autoarr_##type* ar, type element);\ void (*add)(struct Autoarr_##type* ar, type element); \
type (*get)(struct Autoarr_##type* ar, u32 index);\ type (*get)(struct Autoarr_##type* ar, u32 index); \
type* (*getptr)(struct Autoarr_##type* ar, u32 index);\ type* (*getptr)(struct Autoarr_##type* ar, u32 index); \
void (*set)(struct Autoarr_##type* ar, u32 index, type element);\ void (*set)(struct Autoarr_##type* ar, u32 index, type element); \
void (*freear)(struct Autoarr_##type* ar, bool freePtr);\ void (*freear)(struct Autoarr_##type* ar, bool freePtr); \
type* (*toArray)(struct Autoarr_##type* ar);\ type* (*toArray)(struct Autoarr_##type* ar); \
} __functions_list_t_##type;\ ) \
\ \
typedef struct Autoarr_##type{\ STRUCT(Autoarr_##type, \
u16 blocks_count;\ u16 blocks_count; \
u16 max_blocks_count;\ u16 max_blocks_count; \
u16 block_length;\ u16 block_length; \
u16 max_block_length;\ u16 max_block_length; \
type** values;\ type** values; \
__functions_list_t_##type* functions;\ __functions_list_t_##type* functions; \
} Autoarr_##type;\ ) \
\ \
ktid_declare(Autoarr_##type);\ Autoarr_##type* __Autoarr_create_##type(u16 max_blocks_count, u16 max_block_length); \
\ void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr); \
Autoarr_##type* __Autoarr_create_##type(u16 max_blocks_count, u16 max_block_length);\
void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr);\
void ____Autoarr_free_##type(void* ar); void ____Autoarr_free_##type(void* ar);
#define Autoarr(type) Autoarr_##type #define Autoarr(type) Autoarr_##type
#define Autoarr_create(type, max_blocks_count, max_block_length)\ #define Autoarr_create(type, max_blocks_count, max_block_length) \
__Autoarr_create_##type(max_blocks_count, max_block_length) __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) \
autoarr->functions->get(autoarr,index) autoarr->functions->get(autoarr,index)
#define Autoarr_getptr(autoarr, index)\ #define Autoarr_getptr(autoarr, index) \
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, freePtr)\ #define Autoarr_free(autoarr, freePtr) \
autoarr->functions->freear(autoarr, freePtr) autoarr->functions->freear(autoarr, freePtr)
#define Autoarr_toArray(autoarr)\ #define Autoarr_toArray(autoarr) \
autoarr->functions->toArray(autoarr) autoarr->functions->toArray(autoarr)
#define Autoarr_length(autoarr) \ #define Autoarr_length(autoarr) \
(u32)(!autoarr->blocks_count ? 0 : \ (u32)(!autoarr->blocks_count ? 0 : \
autoarr->max_block_length*(autoarr->blocks_count-1)+autoarr->block_length) autoarr->max_block_length*(autoarr->blocks_count-1)+autoarr->block_length)
#define Autoarr_max_length(autoarr)\ #define Autoarr_max_length(autoarr) \
(u32)(autoarr->max_block_length*autoarr->max_blocks_count) (u32)(autoarr->max_block_length*autoarr->max_blocks_count)
#define Autoarr_pop(AR){\ #define Autoarr_pop(AR){ \
if(AR->block_length==1){\ if(AR->block_length==1){ \
AR->blocks_count--;\ AR->blocks_count--; \
AR->block_length=AR->max_block_length;\ AR->block_length=AR->max_block_length; \
free(AR->values[AR->blocks_count]);\ free(AR->values[AR->blocks_count]); \
}\ } \
else AR->block_length--;\ else AR->block_length--; \
} }
#if __cplusplus #if __cplusplus

View File

@ -6,79 +6,79 @@ extern "C" {
#include "../base/base.h" #include "../base/base.h"
#define Autoarr_define(type)\ #define Autoarr_define(type) \
\ \
ktid_define(Autoarr_##type);\ kt_define(Autoarr_##type, ____Autoarr_free_##type, NULL); \
\ \
void __Autoarr_add_##type(Autoarr_##type* ar, type element){\ void __Autoarr_add_##type(Autoarr_##type* ar, type element){ \
if(!ar->values){\ if(!ar->values){ \
ar->values=malloc(ar->max_blocks_count*sizeof(type*));\ ar->values=malloc(ar->max_blocks_count*sizeof(type*)); \
goto create_block;\ goto create_block; \
}\ } \
if(ar->block_length==ar->max_block_length){\ if(ar->block_length==ar->max_block_length){ \
if (ar->blocks_count>=ar->max_blocks_count) throw(ERR_MAXLENGTH);\ if (ar->blocks_count>=ar->max_blocks_count) throw(ERR_MAXLENGTH); \
ar->block_length=0;\ ar->block_length=0; \
create_block:\ create_block: \
ar->values[ar->blocks_count]=malloc(ar->max_block_length*sizeof(type));\ ar->values[ar->blocks_count]=malloc(ar->max_block_length*sizeof(type)); \
ar->blocks_count++;\ ar->blocks_count++; \
}\ } \
ar->values[ar->blocks_count-1][ar->block_length]=element;\ ar->values[ar->blocks_count-1][ar->block_length]=element; \
ar->block_length++;\ ar->block_length++; \
}\ } \
\ \
type __Autoarr_get_##type(Autoarr_##type* ar, u32 index){\ type __Autoarr_get_##type(Autoarr_##type* ar, u32 index){ \
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX);\ if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \
return ar->values[index/ar->max_block_length][index%ar->max_block_length];\ return ar->values[index/ar->max_block_length][index%ar->max_block_length]; \
}\ } \
\ \
type* __Autoarr_getptr_##type(Autoarr_##type* ar, u32 index){\ type* __Autoarr_getptr_##type(Autoarr_##type* ar, u32 index){ \
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX);\ if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \
return ar->values[index/ar->max_block_length]+(index%ar->max_block_length);\ return ar->values[index/ar->max_block_length]+(index%ar->max_block_length); \
}\ } \
\ \
void __Autoarr_set_##type(Autoarr_##type* ar, u32 index, type element){\ void __Autoarr_set_##type(Autoarr_##type* ar, u32 index, type element){ \
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX);\ if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \
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, bool freePtr){\ void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr){ \
for(u16 i=0; i<ar->blocks_count;i++)\ for(u16 i=0; i<ar->blocks_count;i++) \
free(ar->values[i]); \ free(ar->values[i]); \
free(ar->values);\ free(ar->values); \
if(freePtr) free(ar);\ if(freePtr) free(ar); \
}\ } \
void ____Autoarr_free_##type(void* ar){\ void ____Autoarr_free_##type(void* ar){ \
__Autoarr_free_##type((Autoarr_##type*)ar, false);\ __Autoarr_free_##type((Autoarr_##type*)ar, false); \
}\ } \
\ \
type* __Autoarr_toArray_##type(Autoarr_##type* ar){\ type* __Autoarr_toArray_##type(Autoarr_##type* ar){ \
u32 length=Autoarr_length(ar);\ u32 length=Autoarr_length(ar); \
type* array=malloc(length * sizeof(type));\ type* array=malloc(length * sizeof(type)); \
for(u32 i=0; i<length; i++)\ for(u32 i=0; i<length; i++) \
array[i]=__Autoarr_get_##type(ar, i);\ array[i]=__Autoarr_get_##type(ar, i); \
return array;\ return array; \
}\ } \
\ \
__functions_list_t_##type __functions_list_##type={\ __functions_list_t_##type __functions_list_##type={ \
&__Autoarr_add_##type,\ &__Autoarr_add_##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_toArray_##type \
};\ }; \
\ \
Autoarr_##type* __Autoarr_create_##type(u16 max_blocks_count, u16 max_block_length){\ Autoarr_##type* __Autoarr_create_##type(u16 max_blocks_count, u16 max_block_length){ \
Autoarr_##type* ar=malloc(sizeof(Autoarr_##type));\ Autoarr_##type* ar=malloc(sizeof(Autoarr_##type)); \
*ar=(Autoarr_##type){\ *ar=(Autoarr_##type){ \
.max_blocks_count=max_blocks_count,\ .max_blocks_count=max_blocks_count, \
.blocks_count=0,\ .blocks_count=0, \
.max_block_length=max_block_length,\ .max_block_length=max_block_length, \
.block_length=0,\ .block_length=0, \
.values=NULL,\ .values=NULL, \
.functions=&__functions_list_##type\ .functions=&__functions_list_##type \
};\ }; \
return ar;\ return ar; \
} }
#if __cplusplus #if __cplusplus

View File

@ -154,10 +154,10 @@ 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(list, true)) try(ReadValue((&readingList)), m_val, Autoarr_free(list, true))
Autoarr_add(list,val.value); Autoarr_add(list,m_val.value);
if (!readingList){ if (!readingList){
if(val.value.typeId==ktid_Null) if(Unitype_isUniNull(m_val.value))
Autoarr_pop(list); Autoarr_pop(list);
break; break;
} }
@ -275,7 +275,7 @@ Maybe __ReadValue(DeserializeSharedData* shared, bool* readingList){
case ';': case ';':
case ',': case ',':
if(valueStr.length!=0){ if(valueStr.length!=0){
if(value.typeId!=ktid_Null) if(!Unitype_isUniNull(value))
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;

View File

@ -2,10 +2,10 @@
#include "../String/StringBuilder.h" #include "../String/StringBuilder.h"
typedef struct SerializeSharedData{ STRUCT(SerializeSharedData,
StringBuilder* sh_builder; StringBuilder* sh_builder;
u8 sh_tabs; u8 sh_tabs;
} SerializeSharedData; )
#define b shared->sh_builder #define b shared->sh_builder
#define tabs shared->sh_tabs #define tabs shared->sh_tabs
@ -46,7 +46,7 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){
else if(u.typeId==ktid_name(bool)){ else if(u.typeId==ktid_name(bool)){
StringBuilder_append_cptr(b, u.Bool ? "true" : "false"); StringBuilder_append_cptr(b, u.Bool ? "true" : "false");
} }
else if(u.typeId==ktid_Null){ else if(Unitype_isUniNull(u)){
safethrow("Null isn't supported in DtsodV24",;); safethrow("Null isn't supported in DtsodV24",;);
} }
else if(u.typeId==ktid_ptrName(Autoarr_Unitype)){ else if(u.typeId==ktid_ptrName(Autoarr_Unitype)){

View File

@ -2,7 +2,7 @@
#include "../String/StringBuilder.h" #include "../String/StringBuilder.h"
#include "io_includes.h" #include "io_includes.h"
ktid_define(File); kt_define(File, (freeMembers_t)file_close, NULL)
bool file_exists(const char* path){ bool file_exists(const char* path){
if(path[0]=='.'){ if(path[0]=='.'){
@ -61,10 +61,10 @@ Maybe file_close(File* file){
return MaybeNull; return MaybeNull;
} }
#define ioWriteCheck()\ #define ioWriteCheck() \
if(rezult==EOF)\ if(rezult==EOF) \
safethrow(ERR_IO_EOF,;);\ safethrow(ERR_IO_EOF,;); \
if(rezult!=0)\ if(rezult!=0) \
safethrow(ERR_IO,;); safethrow(ERR_IO,;);
Maybe file_writeChar(File* file, char byte){ Maybe file_writeChar(File* file, char byte){

View File

@ -9,14 +9,14 @@ extern "C" {
#include "../String/string.h" #include "../String/string.h"
typedef FILE File; typedef FILE File;
ktid_declare(File); kt_declare(File);
bool file_exists(const char* path); bool file_exists(const char* path);
///@return Maybe<void> ///@return Maybe<void>
Maybe file_delete(const char* path, bool recursive); Maybe file_delete(const char* path, bool recursive);
PACK_ENUM(FileOpenMode, PACKED_ENUM(FileOpenMode,
// open a file for reading // open a file for reading
FileOpenMode_Read=1, FileOpenMode_Read=1,
// (re)create a file for writing // (re)create a file for writing

View File

@ -1,6 +1,6 @@
#include "Hashtable.h" #include "Hashtable.h"
ktid_define(Hashtable); kt_define(Hashtable, __Hashtable_free, NULL);
// amount of rows // amount of rows
static const u16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521}; static const u16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521};
@ -95,7 +95,7 @@ Unitype Hashtable_get(Hashtable* ht, char* key){
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){ 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.typeId!=ktid_Null; return Unitype_isUniNull(u);
} }
void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u){ void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u){

View File

@ -4,14 +4,14 @@
extern "C" { extern "C" {
#endif #endif
#include "../base/base.h"
#include "../HashFunctions/hash.h" #include "../HashFunctions/hash.h"
#include "KeyValuePair.h" #include "KeyValuePair.h"
typedef struct Hashtable{ STRUCT(Hashtable,
u8 hein; // height=HT_HEIGHTS[hein] u8 hein; // height=HT_HEIGHTS[hein]
Autoarr(KVPair)** rows; // Autoarr[height] Autoarr(KVPair)** rows; // Autoarr[height]
} Hashtable; )
ktid_declare(Hashtable);
Hashtable* Hashtable_create(); Hashtable* Hashtable_create();
void Hashtable_free(Hashtable* ht); void Hashtable_free(Hashtable* ht);
@ -33,12 +33,12 @@ Unitype* Hashtable_getptr(Hashtable* ht, char* key);
Unitype Hashtable_get(Hashtable* ht, char* key); 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);
#define Hashtable_foreach(HT, EL, codeblock)({\ #define Hashtable_foreach(HT, EL, codeblock)({ \
u16 hmax=Hashtable_height(HT);\ u16 hmax=Hashtable_height(HT); \
for(u16 h=0; h<hmax; h++){\ for(u16 h=0; h<hmax; h++){ \
Autoarr(KVPair)* AR=HT->rows[h];\ Autoarr(KVPair)* AR=HT->rows[h]; \
Autoarr_foreach(AR, EL, codeblock);\ Autoarr_foreach(AR, EL, codeblock); \
}\ } \
}) })
#if __cplusplus #if __cplusplus

View File

@ -1,6 +1,6 @@
#include "KeyValuePair.h" #include "KeyValuePair.h"
ktid_define(KVPair); kt_define(KVPair, __KVPair_free, NULL);
Autoarr_define(KVPair) Autoarr_define(KVPair)

View File

@ -7,11 +7,10 @@ extern "C" {
#include "../base/base.h" #include "../base/base.h"
#include "../Autoarr/Autoarr.h" #include "../Autoarr/Autoarr.h"
typedef struct KVPair{ STRUCT(KVPair,
char* key; char* key;
Unitype value; Unitype value;
} KVPair; )
ktid_declare(KVPair);
Autoarr_declare(KVPair) Autoarr_declare(KVPair)

View File

@ -1,12 +1,11 @@
#include "SearchTree.h" #include "SearchTree.h"
ktid_define(STNode); kt_define(STNode, __STNode_free, NULL);
STNode* STNode_create(){ STNode* STNode_create(){
STNode* node=malloc(sizeof(STNode)); STNode* node=malloc(sizeof(STNode));
node->branches=NULL; node->branches=NULL;
node->value.typeId=ktid_Null; node->value=UniNull;
node->value.UInt64=0;
return node; return node;
} }

View File

@ -7,11 +7,10 @@ extern "C" {
#include "../base/base.h" #include "../base/base.h"
#include "../String/string.h" #include "../String/string.h"
typedef struct SearchTreeNode{ STRUCT(STNode,
struct SearchTreeNode**** branches; // *STNode[8][8][4] struct STNode**** branches; // *STNode[8][8][4]
Unitype value; Unitype value;
} STNode; )
ktid_declare(STNode);
STNode* STNode_create(); STNode* STNode_create();
void STNode_free(STNode* node); void STNode_free(STNode* node);

View File

@ -1,8 +1,6 @@
#include "StringBuilder.h" #include "StringBuilder.h"
Autoarr_define(string) kt_define(StringBuilder, __StringBuilder_free, NULL);
ktid_define(StringBuilder);
#define BL_C 32 #define BL_C 32
#define BL_L 1024 #define BL_L 1024

View File

@ -7,13 +7,10 @@ extern "C" {
#include "../Autoarr/Autoarr.h" #include "../Autoarr/Autoarr.h"
#include "string.h" #include "string.h"
Autoarr_declare(string) STRUCT(StringBuilder,
typedef struct StringBuilder{
Autoarr(string)* compl_bufs; Autoarr(string)* compl_bufs;
Autoarr(i8)* curr_buf; Autoarr(i8)* curr_buf;
} StringBuilder; )
ktid_declare(StringBuilder);
StringBuilder* StringBuilder_create(void); StringBuilder* StringBuilder_create(void);
void StringBuilder_free(StringBuilder* b); void StringBuilder_free(StringBuilder* b);

View File

@ -1,7 +1,8 @@
#include "string.h" #include "string.h"
ktid_define(string); kt_define(string, NULL, NULL);
Array_define(string); Array_define(string)
Autoarr_define(string)
// copies str content to new char pointer value (adding '\0' at the end) // copies str content to new char pointer value (adding '\0' at the end)
char* string_extract(string str){ char* string_extract(string str){

View File

@ -6,15 +6,17 @@ extern "C" {
#include "../base/base.h" #include "../base/base.h"
#include "../Array/Array.h" #include "../Array/Array.h"
#include "../Autoarr/Autoarr.h"
// my fixed length string struct // my fixed length string struct
// doesn't store '\0' at the end // doesn't store '\0' at the end
typedef struct string{ STRUCT(string,
char* ptr; // char pointer char* ptr; // char pointer
u64 length; // amount of chars in ptr value u64 length; // amount of chars in ptr value
} string; )
ktid_declare(string);
Array_declare(string); Array_declare(string)
Autoarr_declare(string)
static const string stringNull={NULL,0}; static const string stringNull={NULL,0};

View File

@ -5,12 +5,13 @@ extern "C" {
#endif #endif
#include "std.h" #include "std.h"
#include "type_system/typedef_macros.h"
PACK_ENUM(Endian, PACKED_ENUM(Endian,
UnknownEndian=0, UnknownEndian=0,
LittleEndian=1, LittleEndian=1,
BigEndian=2 BigEndian=2
); )
Endian getEndian(); Endian getEndian();

View File

@ -5,9 +5,9 @@ extern "C" {
#endif #endif
#include "std.h" #include "std.h"
#include "type_system/unitype.h" #include "type_system/type_system.h"
PACK_ENUM(ErrorId, PACKED_ENUM(ErrorId,
SUCCESS, // not an error SUCCESS, // not an error
ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX,
ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR,
@ -20,10 +20,10 @@ char* errname(ErrorId err);
char* __genErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname); char* __genErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname);
char* __extendErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname); char* __extendErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname);
typedef struct Maybe{ STRUCT(Maybe,
Unitype value; Unitype value;
char* errmsg; char* errmsg;
} Maybe; )
// return it if func doesn't return anything // return it if func doesn't return anything
// .value .errmsg // .value .errmsg
@ -42,47 +42,47 @@ void printMaybe(Maybe e);
char* __doNothing(char* a); char* __doNothing(char* a);
char* __unknownErr( ); char* __unknownErr( );
#define __stringify_err(E) _Generic(\ #define __stringify_err(E) _Generic( \
(E),\ (E), \
char*: __doNothing,\ char*: __doNothing, \
int: errname,\ int: errname, \
default: __unknownErr\ default: __unknownErr \
)(E) )(E)
#if __cplusplus #if __cplusplus
#define throw_id(E) __EXIT(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__))) #define throw_id(E) __EXIT(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__)))
#define throw_msg(E) __EXIT(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__))) #define throw_msg(E) __EXIT(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__)))
#define safethrow_id(E, FREEMEM) { FREEMEM;\ #define safethrow_id(E, FREEMEM) { FREEMEM; \
__RETURN_EXCEPTION(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__)));\ __RETURN_EXCEPTION(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__))); \
} }
#define safethrow_msg(E, FREEMEM) { FREEMEM;\ #define safethrow_msg(E, FREEMEM) { FREEMEM; \
__RETURN_EXCEPTION(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__)));\ __RETURN_EXCEPTION(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__))); \
} }
#define try_cpp(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){\ #define try_cpp(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){ \
freeMem;\ freeMem; \
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__); \
return _rezult;\ return _rezult; \
} }
#else #else
#define throw(E) __EXIT(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__))) #define throw(E) __EXIT(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__)))
#define safethrow(E, FREEMEM) { FREEMEM;\ #define safethrow(E, FREEMEM) { FREEMEM; \
__RETURN_EXCEPTION(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__)));\ __RETURN_EXCEPTION(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__))); \
} }
#define try(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){\ #define try(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){ \
freeMem;\ freeMem; \
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__); \
return _rezult;\ return _rezult; \
} }
#endif #endif
#define tryLast(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){\ #define tryLast(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){ \
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__); \
__EXIT(_rezult.errmsg);\ __EXIT(_rezult.errmsg); \
} }
#if __cplusplus #if __cplusplus

View File

@ -2,40 +2,40 @@
#include "std.h" #include "std.h"
#define __optime_print(opname, t)\ #define __optime_print(opname, t) \
char tnames[3][3]={"s\0","ms","us"};\ char tnames[3][3]={"s\0","ms","us"}; \
i32 tni=0;\ i32 tni=0; \
if(t>1000000){\ if(t>1000000){ \
t/=1000000;\ t/=1000000; \
tni=0;\ tni=0; \
} else if(t>1000){\ } else if(t>1000){ \
t/=1000;\ t/=1000; \
tni=1;\ tni=1; \
} else tni=2;\ } else tni=2; \
kprintf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%f \e[93m%s\n",\ kprintf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%f \e[93m%s\n", \
opname, t, tnames[tni]); opname, t, tnames[tni]);
#ifdef CLOCK_REALTIME #ifdef CLOCK_REALTIME
/// executes codeblock and prints execution time /// executes codeblock and prints execution time
/// u64 op_i is counter of the internal loop /// u64 op_i is counter of the internal loop
/// uses non-standard high-precision clock /// uses non-standard high-precision clock
#define optime(opname,repeats,codeblock) ({\ #define optime(opname,repeats,codeblock) ({ \
struct timespec start, stop;\ struct timespec start, stop; \
clock_gettime(CLOCK_REALTIME, &start);\ clock_gettime(CLOCK_REALTIME, &start); \
for(u64 op_i=0;op_i<(u64)repeats;op_i++)\ for(u64 op_i=0;op_i<(u64)repeats;op_i++) \
(codeblock);\ (codeblock); \
clock_gettime(CLOCK_REALTIME, &stop);\ clock_gettime(CLOCK_REALTIME, &stop); \
f64 t=(f64)(stop.tv_sec-start.tv_sec)*1000000+(f64)(stop.tv_nsec-start.tv_nsec)/1000;\ f64 t=(f64)(stop.tv_sec-start.tv_sec)*1000000+(f64)(stop.tv_nsec-start.tv_nsec)/1000; \
__optime_print(opname,t)\ __optime_print(opname,t) \
}) })
#else #else
/// uses standard low precision clock /// uses standard low precision clock
#define optime(opname,repeats,codeblock) ({\ #define optime(opname,repeats,codeblock) ({ \
clock_t start=clock();\ clock_t start=clock(); \
for(u64 op_i=0;op_i<(u64)repeats;op_i++)\ for(u64 op_i=0;op_i<(u64)repeats;op_i++) \
(codeblock);\ (codeblock); \
clock_t stop=clock();\ clock_t stop=clock(); \
f64 t=(f64)(stop-start)/CLOCKS_PER_SEC*1000000;\ f64 t=(f64)(stop-start)/CLOCKS_PER_SEC*1000000; \
__optime_print(opname,t)\ __optime_print(opname,t) \
}) })
#endif #endif

View File

@ -22,6 +22,7 @@ typedef int64_t i64;
typedef uint64_t u64; typedef uint64_t u64;
typedef float f32; typedef float f32;
typedef double f64; typedef double f64;
typedef void* Pointer;
// Usually bool from stdbool.h is defined as macro, // Usually bool from stdbool.h is defined as macro,
// so in other macros like ktid_##TYPE it will be replaced by _Bool. // so in other macros like ktid_##TYPE it will be replaced by _Bool.
@ -72,27 +73,27 @@ typedef u8 bool;
#endif #endif
#define __count_args(\ #define __count_args( \
a0, a1, a2, a3, a4, a5, a6, a7 ,\ a0, a1, a2, a3, a4, a5, a6, a7 , \
a8, a9, a10,a11,a12,a13,a14,a15,\ a8, a9, a10,a11,a12,a13,a14,a15, \
a16,a17,a18,a19,a20,a21,a22,a23,\ a16,a17,a18,a19,a20,a21,a22,a23, \
a24,a25,a26,a27,a28,a29,a30,a31,\ a24,a25,a26,a27,a28,a29,a30,a31, \
a32,a33,a34,a35,a36,a37,a38,a39,\ a32,a33,a34,a35,a36,a37,a38,a39, \
a40,a41,a42,a43,a44,a45,a46,a47,\ a40,a41,a42,a43,a44,a45,a46,a47, \
a48,a49,a50,a51,a52,a53,a54,a55,\ a48,a49,a50,a51,a52,a53,a54,a55, \
a56,a57,a58,a59,a60,a61,a62,a63,\ a56,a57,a58,a59,a60,a61,a62,a63, \
a64,...) a64 a64,...) a64
// Macro for counting variadic arguments (max 64) // Macro for counting variadic arguments (max 64)
// (see usage in kprint.h) // (see usage in kprint.h)
#define count_args(ARGS...) __count_args(\ #define count_args(ARGS...) __count_args( \
ARGS,\ ARGS, \
64,63,62,61,60,59,58,57,\ 64,63,62,61,60,59,58,57, \
56,55,54,53,52,51,50,49,\ 56,55,54,53,52,51,50,49, \
48,47,46,45,44,43,42,41,\ 48,47,46,45,44,43,42,41, \
40,39,38,37,36,35,34,33,\ 40,39,38,37,36,35,34,33, \
32,31,30,29,28,27,26,25,\ 32,31,30,29,28,27,26,25, \
24,23,22,21,20,19,18,17,\ 24,23,22,21,20,19,18,17, \
16,15,14,13,12,11,10,9,\ 16,15,14,13,12,11,10,9, \
8, 7, 6, 5, 4, 3, 2, 1, 0) 8, 7, 6, 5, 4, 3, 2, 1, 0)
/* /*
@ -113,16 +114,12 @@ You can even embed it into macro in header (see kprint.h)
#define PRAGMA_WARNING_POP _PRAGMA(GCC diagnostic pop) #define PRAGMA_WARNING_POP _PRAGMA(GCC diagnostic pop)
#define W_INT_CONVERSION "-Wint-conversion" #define W_INT_CONVERSION "-Wint-conversion"
#endif #endif
#define WARNING_DISABLE(WARNING, CODE)\ #define WARNING_DISABLE(WARNING, CODE) \
PRAGMA_WARNING_PUSH\ PRAGMA_WARNING_PUSH \
PRAGMA_WARNING_DISABLE(WARNING)\ PRAGMA_WARNING_DISABLE(WARNING) \
CODE;\ CODE; \
PRAGMA_WARNING_POP PRAGMA_WARNING_POP
#define PACK_ENUM(ENUM_NAME, ENUM_MEMBERS...) typedef enum ENUM_NAME {\
ENUM_MEMBERS\
} __attribute__((__packed__)) ENUM_NAME;
#if __cplusplus #if __cplusplus
} }
#endif #endif

View File

@ -4,17 +4,21 @@ For using some kerep capabilities, such as generic structs, unitype, and kprint,
## type id ## type id
Every registered type has its own id (`ktid`), which should be declared in header file and defined in source file. Every registered type has its own `ktDescriptor` and `ktid` is an index of the descriptor in descriptors array.
Example: Descriptor should be declared in header file.
Following macro declares `typedef struct` and `ktDescriptor`
```c ```c
//someStruct.h //someStruct.h
typedef struct { } someStruct; STRUCT(someStruct,
ktid_declare(someStruct); i32 i; i32 j; i32 k;
);
``` ```
then you need to define descriptor in a source file
```c ```c
//someStruct.c //someStruct.c
ktid_define(someStruct); kt_define(someStruct);
``` ```
and register it.
## type descriptors ## type descriptors

View File

@ -2,18 +2,20 @@
#include "../base.h" #include "../base.h"
#include "../../kprint/kprint_format.h" #include "../../kprint/kprint_format.h"
// accepts char* (ptr to char) and char** (ptr to string)
char* __toString_char(void* c, u32 fmt) { char* __toString_char(void* c, u32 fmt) {
//*c=char // *c=char*
if(kp_fmt_dataFormat(fmt)==kp_s){
return cptr_copy(*(char**)c); // to avoid segmentation fault on free() when *c allocalet on stack
}
// *c=char
if(kp_fmt_dataFormat(fmt)==kp_c){ if(kp_fmt_dataFormat(fmt)==kp_c){
char* cc=malloc(2); char* cc=malloc(2);
cc[0]=*(char*)c; cc[0]=*(char*)c;
cc[1]=0; cc[1]=0;
return cc; return cc;
} }
// *c=cstring
else if(kp_fmt_dataFormat(fmt)==kp_s){
return cptr_copy(*(char**)c);
}
else throw(ERR_FORMAT); else throw(ERR_FORMAT);
} }
@ -61,23 +63,23 @@ char* toString_u64(u64 n, bool withPostfix, bool uppercase){
return cptr_copy((char*)str+i); return cptr_copy((char*)str+i);
} }
#define _toString_float_impl(bufsize, maxPrecision) {\ #define _toString_float_impl(bufsize, maxPrecision) { \
char str[bufsize];\ char str[bufsize]; \
if(precision>maxPrecision)\ if(precision>maxPrecision) \
throw("too big precision");\ throw("too big precision"); \
if(precision==0)\ if(precision==0) \
precision=toString_float_default_precision;\ precision=toString_float_default_precision; \
i32 cn=IFMSC(\ i32 cn=IFMSC( \
sprintf_s(str, bufsize, "%.*f", precision, n),\ sprintf_s(str, bufsize, "%.*f", precision, n), \
sprintf(str, "%.*f", precision, n)\ sprintf(str, "%.*f", precision, n) \
);\ ); \
/* remove trailing zeroes except .0*/\ /* remove trailing zeroes except .0*/ \
while(str[cn-1]=='0' && str[cn-2]!='.')\ while(str[cn-1]=='0' && str[cn-2]!='.') \
cn--;\ cn--; \
if(withPostfix)\ if(withPostfix) \
str[cn++]= uppercase ? 'F' : 'f';\ str[cn++]= uppercase ? 'F' : 'f'; \
str[cn]='\0';\ str[cn]='\0'; \
return cptr_copy(str);\ return cptr_copy(str); \
} }
char* toString_f32(f32 n, u8 precision, bool withPostfix, bool uppercase) char* toString_f32(f32 n, u8 precision, bool withPostfix, bool uppercase)
@ -86,15 +88,15 @@ char* toString_f32(f32 n, u8 precision, bool withPostfix, bool uppercase)
char* toString_f64(f64 n, u8 precision, bool withPostfix, bool uppercase) char* toString_f64(f64 n, u8 precision, bool withPostfix, bool uppercase)
_toString_float_impl(512, toString_f64_max_precision) _toString_float_impl(512, toString_f64_max_precision)
#define byte_to_bits(byte) {\ #define byte_to_bits(byte) { \
str[cn++]='0' + (u8)((byte>>7)&1); /* 8th bit */\ str[cn++]='0' + (u8)((byte>>7)&1); /* 8th bit */ \
str[cn++]='0' + (u8)((byte>>6)&1); /* 7th bit */\ str[cn++]='0' + (u8)((byte>>6)&1); /* 7th bit */ \
str[cn++]='0' + (u8)((byte>>5)&1); /* 6th bit */\ str[cn++]='0' + (u8)((byte>>5)&1); /* 6th bit */ \
str[cn++]='0' + (u8)((byte>>4)&1); /* 5th bit */\ str[cn++]='0' + (u8)((byte>>4)&1); /* 5th bit */ \
str[cn++]='0' + (u8)((byte>>3)&1); /* 4th bit */\ str[cn++]='0' + (u8)((byte>>3)&1); /* 4th bit */ \
str[cn++]='0' + (u8)((byte>>2)&1); /* 3th bit */\ str[cn++]='0' + (u8)((byte>>2)&1); /* 3th bit */ \
str[cn++]='0' + (u8)((byte>>1)&1); /* 2th bit */\ str[cn++]='0' + (u8)((byte>>1)&1); /* 2th bit */ \
str[cn++]='0' + (u8)((byte>>0)&1); /* 1th bit */\ str[cn++]='0' + (u8)((byte>>0)&1); /* 1th bit */ \
} }
char* toString_bin(void* _bytes, u32 size, bool inverse, bool withPrefix){ char* toString_bin(void* _bytes, u32 size, bool inverse, bool withPrefix){
@ -163,60 +165,72 @@ char* toString_hex(void* _bytes, u32 size, bool inverse, bool withPrefix, bool u
} }
#define __toString_i32_def(BITS) char* __toString_i##BITS(void* _n, u32 f){\ #define __toString_i32_def(BITS) char* __toString_i##BITS(void* _n, u32 f){ \
switch(kp_fmt_dataFormat(f)){\ switch(kp_fmt_dataFormat(f)){ \
case kp_i: ;\ case kp_i: ; \
i##BITS n=*(i##BITS*)_n;\ i##BITS n=*(i##BITS*)_n; \
return toString_i64(n);\ return toString_i64(n); \
case kp_b:\ case kp_b: \
return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f));\ return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f)); \
case kp_h:\ case kp_h: \
return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f));\ return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f)); \
default:\ default: \
kprintf("\n%u\n", kp_fmt_dataFormat(f));\ kprintf("\n%u\n", kp_fmt_dataFormat(f)); \
throw(ERR_FORMAT);\ throw(ERR_FORMAT); \
return NULL;\ return NULL; \
}\ } \
} }
__toString_i32_def(8) __toString_i32_def(8)
__toString_i32_def(16) __toString_i32_def(16)
__toString_i32_def(32) __toString_i32_def(32)
__toString_i32_def(64) __toString_i32_def(64)
#define __toString_u_def(BITS) char* __toString_u##BITS(void* _n, u32 f){\ #define __toString_u_def(BITS) char* __toString_u##BITS(void* _n, u32 f){ \
switch(kp_fmt_dataFormat(f)){\ switch(kp_fmt_dataFormat(f)){ \
case kp_u: ;\ case kp_u: ; \
u##BITS n=*(u##BITS*)_n;\ u##BITS n=*(u##BITS*)_n; \
return toString_u64(n, kp_fmt_withPostfix(f), kp_fmt_isUpper(f));\ return toString_u64(n, kp_fmt_withPostfix(f), kp_fmt_isUpper(f)); \
case kp_b:\ case kp_b: \
return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f));\ return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f)); \
case kp_h:\ case kp_h: \
return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f));\ return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f)); \
default:\ default: \
kprintf("\n%u\n", kp_fmt_dataFormat(f));\ kprintf("\n%u\n", kp_fmt_dataFormat(f)); \
throw(ERR_FORMAT);\ throw(ERR_FORMAT); \
return NULL;\ return NULL; \
}\ } \
} }
__toString_u_def(8) __toString_u_def(8)
__toString_u_def(16) __toString_u_def(16)
__toString_u_def(32) __toString_u_def(32)
__toString_u_def(64) // __toString_u_def(64)
char* __toString_u64(void* _n, u32 f){
switch(kp_fmt_dataFormat(f)){
case kp_u: ;
u64 n=*(u64*)_n;
return toString_u64(n, kp_fmt_withPostfix(f), kp_fmt_isUpper(f));
case kp_b:
return toString_bin(_n, 64/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f));
case kp_h:
return toString_hex(_n, 64/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f));
default:
kprintf("\n%u\n", kp_fmt_dataFormat(f)); throw(ERR_FORMAT); return NULL; }
}
#define __toString_float_def(BITS) char* __toString_f##BITS(void* _n, u32 f){\ #define __toString_float_def(BITS) char* __toString_f##BITS(void* _n, u32 f){ \
switch(kp_fmt_dataFormat(f)){\ switch(kp_fmt_dataFormat(f)){ \
case kp_f: ;\ case kp_f: ; \
f##BITS n=*(f##BITS*)_n;\ f##BITS n=*(f##BITS*)_n; \
return toString_f64(n, toString_float_default_precision, kp_fmt_withPostfix(f), kp_fmt_isUpper(f));\ return toString_f64(n, toString_float_default_precision, kp_fmt_withPostfix(f), kp_fmt_isUpper(f)); \
case kp_b:\ case kp_b: \
return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f));\ return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f)); \
case kp_h:\ case kp_h: \
return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f));\ return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f)); \
default:\ default: \
kprintf("\n%u\n", kp_fmt_dataFormat(f));\ kprintf("\n%u\n", kp_fmt_dataFormat(f)); \
throw(ERR_FORMAT);\ throw(ERR_FORMAT); \
return NULL;\ return NULL; \
}\ } \
} }
__toString_float_def(32) __toString_float_def(32)

View File

@ -6,8 +6,8 @@ extern "C" {
#include "../errors.h" #include "../errors.h"
// char and cstring // accepts char* (ptr to char) and char** (ptr to string)
// has different output for fmtChar and fmtString // uses format kp_s and kp_c to determine what type is <c> argument
char* __toString_char(void* c, u32 fmt); char* __toString_char(void* c, u32 fmt);
// bool // bool

View File

@ -4,85 +4,92 @@
#include "../../SearchTree/SearchTree.h" #include "../../SearchTree/SearchTree.h"
#include "../../Hashtable/Hashtable.h" #include "../../Hashtable/Hashtable.h"
#include "../../String/StringBuilder.h" #include "../../String/StringBuilder.h"
#include "../../Filesystem/filesystem.h"
#include "base_toString.h" #include "base_toString.h"
void ktDescriptors_initKerepTypes(){ void ktDescriptors_initKerepTypes(){
// null
__kt_register("Null", sizeof(NULL), NULL, NULL);
ktid_Null=ktid_last;
// base types // base types
kt_register(char, NULL, __toString_char); kt_register(Pointer);
kt_register(bool, NULL, __toString_bool); if(ktid_Pointer!=0) // this can break UnitypeNull
kt_register(f32, NULL, __toString_f32); throw("ktid_Pointer!=0, you must init kerep types before any other types");
kt_register(f64, NULL, __toString_f64);
kt_register(i8, NULL, __toString_i8); kt_register(char);
kt_register(u8, NULL, __toString_u8); kt_register(bool);
kt_register(i16, NULL, __toString_i16); kt_register(f32);
kt_register(u16, NULL, __toString_u16); kt_register(f64);
kt_register(i32, NULL, __toString_i32); kt_register(i8);
kt_register(u32, NULL, __toString_u32); kt_register(u8);
kt_register(i64, NULL, __toString_i64); kt_register(i16);
kt_register(u64, NULL, __toString_u64); kt_register(u16);
kt_register(i32);
kt_register(u32);
kt_register(i64);
kt_register(u64);
// ktDescriptor // ktDescriptor
kt_register(ktDescriptor, NULL, NULL); kt_register(ktDescriptor);
// base type arrays // base type arrays
kt_register(Array_char, (freeMembers_t)Array_char_free, NULL); kt_register(Array_char);
kt_register(Array_bool, (freeMembers_t)Array_bool_free, NULL); kt_register(Array_bool);
kt_register(Array_f32, (freeMembers_t)Array_f32_free, NULL); kt_register(Array_f32);
kt_register(Array_f64, (freeMembers_t)Array_f64_free, NULL); kt_register(Array_f64);
kt_register(Array_i8, (freeMembers_t)Array_i8_free, NULL); kt_register(Array_i8);
kt_register(Array_u8, (freeMembers_t)Array_u8_free, NULL); kt_register(Array_u8);
kt_register(Array_i16, (freeMembers_t)Array_i16_free, NULL); kt_register(Array_i16);
kt_register(Array_u16, (freeMembers_t)Array_u16_free, NULL); kt_register(Array_u16);
kt_register(Array_i32, (freeMembers_t)Array_i32_free, NULL); kt_register(Array_i32);
kt_register(Array_u32, (freeMembers_t)Array_u32_free, NULL); kt_register(Array_u32);
kt_register(Array_i64, (freeMembers_t)Array_i64_free, NULL); kt_register(Array_i64);
kt_register(Array_u64, (freeMembers_t)Array_u64_free, NULL); kt_register(Array_u64);
kt_register(Array_Pointer);
// base type autoarrs // base type autoarrs
kt_register(Autoarr_char, ____Autoarr_free_char, NULL); kt_register(Autoarr_char);
kt_register(Autoarr_bool, ____Autoarr_free_bool, NULL); kt_register(Autoarr_bool);
kt_register(Autoarr_f32, ____Autoarr_free_f32, NULL); kt_register(Autoarr_f32);
kt_register(Autoarr_f64, ____Autoarr_free_f64, NULL); kt_register(Autoarr_f64);
kt_register(Autoarr_i8, ____Autoarr_free_i8, NULL); kt_register(Autoarr_i8);
kt_register(Autoarr_u8, ____Autoarr_free_u8, NULL); kt_register(Autoarr_u8);
kt_register(Autoarr_i16, ____Autoarr_free_i16, NULL); kt_register(Autoarr_i16);
kt_register(Autoarr_u16, ____Autoarr_free_u16, NULL); kt_register(Autoarr_u16);
kt_register(Autoarr_i32, ____Autoarr_free_i32, NULL); kt_register(Autoarr_i32);
kt_register(Autoarr_u32, ____Autoarr_free_u32, NULL); kt_register(Autoarr_u32);
kt_register(Autoarr_i64, ____Autoarr_free_i64, NULL); kt_register(Autoarr_i64);
kt_register(Autoarr_u64, ____Autoarr_free_u64, NULL); kt_register(Autoarr_u64);
kt_register(Autoarr_Pointer);
// Unitype // Unitype
kt_register(Unitype, __UnitypePtr_free, NULL); kt_register(Unitype);
kt_register(Array_Unitype, __Array_Unitype_free_, NULL); kt_register(Array_Unitype);
kt_register(Autoarr_Unitype, ____Autoarr_free_Unitype_, NULL); kt_register(Autoarr_Unitype);
// replacing autogenerated freear() function to custom // replacing autogenerated freear() function to custom
Autoarr_Unitype* _uar=Autoarr_create(Unitype, 1, 1); Autoarr_Unitype* _uar=Autoarr_create(Unitype, 1, 1);
_uar->functions->freear=__Autoarr_free_Unitype_; _uar->functions->freear=__Autoarr_free_Unitype_;
Autoarr_free(_uar, true); Autoarr_free(_uar, true);
// SearchTreeNode // STNode
kt_register(STNode, __STNode_free, NULL); kt_register(STNode);
// KeyValuePair // KeyValuePair
kt_register(KVPair, __KVPair_free, NULL); kt_register(KVPair);
kt_register(Autoarr_KVPair, ____Autoarr_free_KVPair_, NULL); kt_register(Autoarr_KVPair);
// replacing autogenerated freear() function to custom // replacing autogenerated freear() function to custom
Autoarr_KVPair* _kvpar=Autoarr_create(KVPair, 1, 1); Autoarr_KVPair* _kvpar=Autoarr_create(KVPair, 1, 1);
_kvpar->functions->freear=__Autoarr_free_KVPair_; _kvpar->functions->freear=__Autoarr_free_KVPair_;
Autoarr_free(_kvpar, true); Autoarr_free(_kvpar, true);
// Hashtable // Hashtable
kt_register(Hashtable, __Hashtable_free, NULL); kt_register(Hashtable);
// string // string
kt_register(string, NULL, NULL); kt_register(string);
kt_register(Array_string, (freeMembers_t)Array_string_free, NULL); kt_register(Array_string);
kt_register(Autoarr_string, ____Autoarr_free_string, NULL); kt_register(Autoarr_string);
// StringBuilder // StringBuilder
kt_register(StringBuilder, __StringBuilder_free, NULL); kt_register(StringBuilder);
//File
kt_register(File);
} }

View File

@ -6,16 +6,40 @@ extern "C" {
#include "../std.h" #include "../std.h"
#include "ktid.h" #include "ktid.h"
#include "typedef_macros.h"
#define kt_declare(TYPE)\
ktid_declare(TYPE);\
extern ktDescriptor ktDescriptor_##TYPE; \
extern ktDescriptor ktDescriptor_##TYPE##_Ptr;
#define kt_define(TYPE, FREE_FUNC, TOSTRING_FUNC)\
ktid_define(TYPE); \
ktDescriptor ktDescriptor_##TYPE={ \
.name=#TYPE, \
.id=ktid_undefined, \
.size=sizeof(TYPE), \
.freeMembers=FREE_FUNC, \
.toString=TOSTRING_FUNC \
}; \
ktDescriptor ktDescriptor_##TYPE##_Ptr={\
.name=#TYPE "_Ptr", \
.id=ktid_undefined, \
.size=sizeof(TYPE), \
.freeMembers=FREE_FUNC, \
.toString=TOSTRING_FUNC \
};
typedef void (*freeMembers_t)(void*); typedef void (*freeMembers_t)(void*);
typedef char* (*toString_t)(void* obj, u32 fmt); typedef char* (*toString_t)(void* obj, u32 fmt);
typedef struct ktDescriptor{
STRUCT(ktDescriptor,
char* name; char* name;
ktid id; ktid id;
u16 size; u16 size;
freeMembers_t freeMembers; // NULL or function which frees all struct members freeMembers_t freeMembers; // NULL or function which frees all struct members
toString_t toString; // NULL or function which generates string representaion of object toString_t toString; // NULL or function which generates string representaion of object
} ktDescriptor; )
#if __cplusplus #if __cplusplus
} }

View File

@ -1,62 +1,57 @@
#include "../../Autoarr/Autoarr.h" #include "../../Autoarr/Autoarr.h"
#include "type_system.h"
#include "base_toString.h"
Autoarr_declare(ktDescriptor) kt_define(Pointer, free, __toString_u64);
Autoarr_define(ktDescriptor) kt_define(char,NULL, __toString_char);
kt_define(bool,NULL, __toString_bool);
kt_define(f32, NULL, __toString_f32);
kt_define(f64, NULL, __toString_f64);
kt_define(i8, NULL, __toString_i8);
kt_define(u8, NULL, __toString_u8);
kt_define(i16, NULL, __toString_i16);
kt_define(u16, NULL, __toString_u16);
kt_define(i32, NULL, __toString_i32);
kt_define(u32, NULL, __toString_u32);
kt_define(i64, NULL, __toString_i64);
kt_define(u64, NULL, __toString_u64);
ktid ktid_Null=-1; kt_define(ktDescriptor, NULL, NULL);
ktid_define(char); typedef ktDescriptor* ktDescriptor_Ptr;
ktid_define(bool);
ktid_define(f32);
ktid_define(f64);
ktid_define(i8);
ktid_define(u8);
ktid_define(i16);
ktid_define(u16);
ktid_define(i32);
ktid_define(u32);
ktid_define(i64);
ktid_define(u64);
ktid_define(ktDescriptor);
// type descriptors are stored here during initialization // type descriptors are stored here during initialization
Autoarr(ktDescriptor)* __ktDescriptors=NULL; Autoarr(Pointer)* __descriptorPointers=NULL;
// here type descriptors are stored when initialization is complited // here type descriptors are stored when initialization is complited
ktDescriptor* typeDescriptors=NULL; ktDescriptor** typeDescriptors=NULL;
ktid ktid_last=-1; ktid ktid_last=-1;
typedef enum{ ENUM(ktDescriptorsState,
NotInitialized, Initializing, Initialized NotInitialized, Initializing, Initialized
} ktDescriptorsState; )
ktDescriptorsState initState=NotInitialized; ktDescriptorsState initState=NotInitialized;
void ktDescriptors_beginInit(){ void ktDescriptors_beginInit(){
kprintf("\e[94mtype descriptors initializing...\n"); kprintf("\e[94mtype descriptors initializing...\n");
__ktDescriptors=Autoarr_create(ktDescriptor, 256, 256); __descriptorPointers=Autoarr_create(Pointer, 256, 256);
if(__ktDescriptors==NULL) throw(ERR_NULLPTR); if(__descriptorPointers==NULL)
throw(ERR_NULLPTR);
} }
void ktDescriptors_endInit(){ void ktDescriptors_endInit(){
typeDescriptors=Autoarr_toArray(__ktDescriptors); typeDescriptors=(ktDescriptor**)Autoarr_toArray(__descriptorPointers);
Autoarr_free(__ktDescriptors,true); Autoarr_free(__descriptorPointers,true);
if(typeDescriptors==NULL) throw(ERR_NULLPTR); if(typeDescriptors==NULL) throw(ERR_NULLPTR);
kprintf("\e[92minitialized %u type descriptors\n", ktid_last); kprintf("\e[92minitialized %u type descriptors\n", ktid_last);
} }
void __kt_register(char* name, i16 size, void (*freeMembers)(void*), char* (*toString)(void*, u32)){ void __kt_register(ktDescriptor* descriptor){
ktDescriptor typeDesc={ descriptor->id=++ktid_last;
.name=name, Autoarr_add(__descriptorPointers, descriptor);
.size=size,
.id=++ktid_last,
.freeMembers=freeMembers,
.toString=toString
};
Autoarr_add(__ktDescriptors, typeDesc);
} }
ktDescriptor ktDescriptor_get(ktid id){ ktDescriptor* ktDescriptor_get(ktid id){
if(id>ktid_last) { if(id>ktid_last || id==ktid_undefined) {
kprintf("\ntype id: %u\n",id); kprintf("\ntype id: %u\n",id);
throw("invalid type id"); throw("invalid type id");
} }

View File

@ -9,39 +9,38 @@ extern "C" {
#include "ktDescriptor.h" #include "ktDescriptor.h"
extern ktid ktid_last; extern ktid ktid_last;
void __kt_register(char* name, i16 size, void (*freeMembers)(void*), char* (*toString)(void*, u32)); void __kt_register(ktDescriptor* descriptor);
#define kt_register(TYPE, FREE_MEMBERS_FUNC, TO_STRING_FUNC)\ #define kt_register(TYPE) \
__kt_register(#TYPE, sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\ __kt_register(&ktDescriptor_##TYPE); \
ktid_##TYPE=ktid_last;\ ktid_##TYPE=ktid_last; \
__kt_register(#TYPE "*", sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\ __kt_register(&ktDescriptor_##TYPE##_Ptr); \
ktid_##TYPE##_Ptr=ktid_last; ktid_##TYPE##_Ptr=ktid_last;
void ktDescriptors_beginInit(); void ktDescriptors_beginInit();
void ktDescriptors_endInit(); void ktDescriptors_endInit();
/// @param id id of registered type /// @param id id of registered type
ktDescriptor ktDescriptor_get(ktid id); ktDescriptor* ktDescriptor_get(ktid id);
// call it to free heap-allocated ktDescriptors array // call it to free heap-allocated ktDescriptors array
void ktDescriptors_free(); void ktDescriptors_free();
extern ktid ktid_Null; kt_declare(Pointer);
kt_declare(char);
kt_declare(bool);
kt_declare(f32);
kt_declare(f64);
kt_declare(i8);
kt_declare(u8);
kt_declare(i16);
kt_declare(u16);
kt_declare(i32);
kt_declare(u32);
kt_declare(i64);
kt_declare(u64);
ktid_declare(char); kt_declare(ktDescriptor);
ktid_declare(bool);
ktid_declare(f32);
ktid_declare(f64);
ktid_declare(i8);
ktid_declare(u8);
ktid_declare(i16);
ktid_declare(u16);
ktid_declare(i32);
ktid_declare(u32);
ktid_declare(i64);
ktid_declare(u64);
ktid_declare(ktDescriptor);
#if __cplusplus #if __cplusplus
} }

View File

@ -5,17 +5,20 @@ extern "C" {
#endif #endif
#include "../std.h" #include "../std.h"
#include "typedef_macros.h"
typedef u16 ktid; typedef u16 ktid;
static const ktid ktid_undefined=-1;
#define ktid_name(TYPE) ktid_##TYPE #define ktid_name(TYPE) ktid_##TYPE
#define ktid_ptrName(TYPE) ktid_##TYPE##_Ptr #define ktid_ptrName(TYPE) ktid_##TYPE##_Ptr
#define ktid_declare(TYPE)\ #define ktid_declare(TYPE) \
extern ktid ktid_##TYPE;\ extern ktid ktid_##TYPE; \
extern ktid ktid_##TYPE##_Ptr; extern ktid ktid_##TYPE##_Ptr;
#define ktid_define(TYPE)\ #define ktid_define(TYPE) \
ktid ktid_##TYPE=-1;\ ktid ktid_##TYPE=-1; \
ktid ktid_##TYPE##_Ptr=-1; ktid ktid_##TYPE##_Ptr=-1;
#if __cplusplus #if __cplusplus

View File

@ -1,5 +1,8 @@
#pragma once
#include "init.h" #include "init.h"
#include "ktid.h" #include "ktid.h"
#include "ktDescriptor.h" #include "ktDescriptor.h"
#include "kt_functions.h" #include "kt_functions.h"
#include "unitype.h" #include "unitype.h"
#include "typedef_macros.h"

View File

@ -0,0 +1,14 @@
#pragma once
#define ENUM(ENUM_NAME, ENUM_MEMBERS...) typedef enum ENUM_NAME { \
ENUM_MEMBERS \
} ENUM_NAME;
#define PACKED_ENUM(ENUM_NAME, ENUM_MEMBERS...) typedef enum ENUM_NAME { \
ENUM_MEMBERS \
} __attribute__((__packed__)) ENUM_NAME;
#define STRUCT(STRUCT_NAME, STRUCT_MEMBERS...) typedef struct STRUCT_NAME { \
STRUCT_MEMBERS \
} STRUCT_NAME; \
kt_declare(STRUCT_NAME);

View File

@ -1,52 +1,90 @@
#include "../base.h" #include "../base.h"
#include "../../kprint/kprint_format.h"
ktid_define(Unitype);
char* __Unitype_toString(void* _u, u32 fmt){
return Unitype_toString(*(Unitype*)_u, fmt);
}
kt_define(Unitype, __UnitypePtr_free, __Unitype_toString);
void Unitype_free(Unitype u){ void Unitype_free(Unitype u){
ktDescriptor type=ktDescriptor_get(u.typeId); if(u.typeId==ktid_undefined){
if(type.freeMembers) if(u.VoidPtr!=NULL)
type.freeMembers(u.VoidPtr); throw("unitype with undefined typeId has value");
return;
}
ktDescriptor* type=ktDescriptor_get(u.typeId);
if(type->freeMembers)
type->freeMembers(u.VoidPtr);
if(u.allocatedInHeap) if(u.allocatedInHeap)
free(u.VoidPtr); free(u.VoidPtr);
} }
void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); } void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); }
char* toString_Unitype(void* _u, u32 fmt){
Unitype* u=_u; char* Unitype_toString(Unitype u, u32 fmt){
ktDescriptor type=ktDescriptor_get(u->typeId); if(u.typeId==ktid_undefined){
char* valuestr=type.toString(_u, fmt); if(u.VoidPtr!=NULL)
char* rezult=cptr_concat("{ type: ", type.name, throw("unitype with undefined typeId has value");
", allocated on heap: ", (u->allocatedInHeap ? "true" : "false"), return cptr_copy("{ERROR_TYPE}");
}
if(fmt==0) {
if(u.typeId==ktid_name(bool) ||
u.typeId==ktid_name(i8) || u.typeId==ktid_name(i16) ||
u.typeId==ktid_name(i32) || u.typeId==ktid_name(i64)){
// auto format set
fmt=kp_i;
// replaces value with pointer to value to pass into toString_i64(void*, u32)
i64 value=u.Int64;
u.VoidPtr=&value;
}
else if(u.typeId==ktid_name(u8) || u.typeId==ktid_name(u16) ||
u.typeId==ktid_name(u32) || u.typeId==ktid_name(u64)){
fmt=kp_u;
u64 value=u.UInt64;
u.VoidPtr=&value;
}
else if(u.typeId==ktid_name(f32) || u.typeId==ktid_name(f64)){
fmt=kp_f;
f64 value=u.Float64;
u.VoidPtr=&value;
}
else if(u.typeId==ktid_name(char)){
fmt=kp_c;
i64 value=u.Int64;
u.VoidPtr=&value;
}
else if(u.typeId==ktid_ptrName(char)){
char* value=u.VoidPtr;
u.VoidPtr=&value;
fmt=kp_s;
}
else if(u.typeId==ktid_name(Pointer)){
if(u.VoidPtr==NULL)
return cptr_copy("{ UniNull }");
fmt=kp_h;
}
}
ktDescriptor* type=ktDescriptor_get(u.typeId);
char* valuestr;
if(type->toString)
valuestr=type->toString(u.VoidPtr, fmt);
else valuestr="ERR_NO_TOSTRING_FUNC";
char* rezult=cptr_concat("{ type: ", type->name,
", allocated on heap: ", (u.allocatedInHeap ? "true" : "false"),
", value:", valuestr, " }"); ", value:", valuestr, " }");
if(type->toString)
free(valuestr); free(valuestr);
return rezult; return rezult;
} }
#define BUFSIZE 64
char* sprintuni(Unitype v){
char* buf=malloc(BUFSIZE);
ktDescriptor type=ktDescriptor_get(v.typeId);
if(v.typeId==ktid_Null)
sprintf_s(buf, BUFSIZE, "{Null}");
else if(v.typeId==ktid_name(f64))
sprintf_s(buf, BUFSIZE, "{%s : %lf}", type.name,v.Float64);
else if(v.typeId==ktid_name(bool) || v.typeId==ktid_name(u64))
sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%llu", "%lu") "}", type.name,v.UInt64);
else if(v.typeId==ktid_name(i64))
sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%lld", "%ld") "}", type.name,v.Int64);
else if(v.typeId==ktid_ptrName(char)){
size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2;
buf=realloc(buf, newBUFSIZE);
sprintf_s(buf, BUFSIZE, "{%s : \"%s\"}", type.name,(char*)v.VoidPtr);
}
else sprintf_s(buf, BUFSIZE, "{%s : %p}", type.name,v.VoidPtr);
return buf;
}
void printuni(Unitype v){ void printuni(Unitype v){
char* s=sprintuni(v); char* s=Unitype_toString(v,0);
fputs(s, stdout); fputs(s, stdout);
fputc('\n',stdout);
free(s); free(s);
} }

View File

@ -5,8 +5,9 @@ extern "C" {
#endif #endif
#include "ktid.h" #include "ktid.h"
#include "typedef_macros.h"
typedef struct Unitype{ STRUCT(Unitype,
union { union {
i64 Int64; i64 Int64;
u64 UInt64; u64 UInt64;
@ -17,11 +18,10 @@ typedef struct Unitype{
}; };
ktid typeId; ktid typeId;
bool allocatedInHeap; // should Unitype_free call free() to VoidPtr* bool allocatedInHeap; // should Unitype_free call free() to VoidPtr*
} Unitype; )
ktid_declare(Unitype);
#define __UniDef(FIELD, TYPE, VAL) (Unitype){\ #define __UniDef(FIELD, TYPE, VAL) (Unitype){ \
.FIELD=VAL, .typeId=ktid_name(TYPE), .allocatedInHeap=false} .FIELD=VAL, .typeId=ktid_name(TYPE), .allocatedInHeap=false}
#define UniInt64(VAL) __UniDef(Int64, i64, VAL) #define UniInt64(VAL) __UniDef(Int64, i64, VAL)
@ -29,20 +29,23 @@ ktid_declare(Unitype);
#define UniFloat64(VAL) __UniDef(Float64, f64, VAL) #define UniFloat64(VAL) __UniDef(Float64, f64, VAL)
#define UniBool(VAL) __UniDef(Bool, bool, VAL) #define UniBool(VAL) __UniDef(Bool, bool, VAL)
#define UniStackPtr(TYPE, VAL) (Unitype){\ #define UniStackPtr(TYPE, VAL) (Unitype){ \
.VoidPtr=VAL, .typeId=ktid_ptrName(TYPE), .allocatedInHeap=false} .VoidPtr=VAL, .typeId=ktid_ptrName(TYPE), .allocatedInHeap=false}
#define UniHeapPtr(TYPE, VAL) (Unitype){\ #define UniHeapPtr(TYPE, VAL) (Unitype){ \
.VoidPtr=VAL, .typeId=ktid_ptrName(TYPE), .allocatedInHeap=true} .VoidPtr=VAL, .typeId=ktid_ptrName(TYPE), .allocatedInHeap=true}
#define UniNull (Unitype){.Int64=0, .typeId=ktid_Null, .allocatedInHeap=false} // 0==ktid_Pointer
#define UniNull (Unitype){.Int64=0, .typeId=0, .allocatedInHeap=false}
#define UniTrue UniBool(true) #define UniTrue UniBool(true)
#define UniFalse UniBool(false) #define UniFalse UniBool(false)
#define Unitype_isUniNull(UNI) (UNI.typeId==ktid_Pointer && UNI.VoidPtr==NULL)
// frees VoidPtr value or does nothing if type isn't pointer // frees VoidPtr value or does nothing if type isn't pointer
void Unitype_free(Unitype u); void Unitype_free(Unitype u);
void __UnitypePtr_free(void* u); void __UnitypePtr_free(void* u);
char* Unitype_toString(Unitype v, u32 fmt);
void printuni(Unitype v); void printuni(Unitype v);
char* sprintuni(Unitype v);
#if __cplusplus #if __cplusplus
} }

View File

@ -47,4 +47,4 @@ I don't really like printf function (and its variants), so i made safer and more
Maybe m=MaybeNull; Maybe m=MaybeNull;
kprint(kp_fgBlue|kp_s, "Maybe: ", kp_fgGreen|ktid_MaybePtr, &m); kprint(kp_fgBlue|kp_s, "Maybe: ", kp_fgGreen|ktid_MaybePtr, &m);
``` ```
output: <span style="color:blue">Maybe:</span> <span style="color:lightgreen">{value={0, ktid_Null}}</span> output: <span style="color:blue">Maybe:</span> <span style="color:lightgreen">{value: { Pointer, 0x0 }}</span>

View File

@ -15,7 +15,7 @@ ktid __typeFromFormat(kp_fmt f){
case kp_f: case kp_f:
return ktid_name(f64); return ktid_name(f64);
case kp_c: case kp_c:
return ktid_char; return ktid_name(char);
case kp_s: case kp_s:
return ktid_ptrName(char); return ktid_ptrName(char);
default: default:
@ -26,12 +26,12 @@ ktid __typeFromFormat(kp_fmt f){
Maybe __next_toString(kp_fmt f, __kp_value_union* object){ Maybe __next_toString(kp_fmt f, __kp_value_union* object){
// detecting type // detecting type
ktid typeId=__typeFromFormat(f); ktid typeId=__typeFromFormat(f);
if(typeId==-1) if(typeId==ktid_undefined)
safethrow("typeId is not set, can't autodetect type",;); safethrow("typeId is undefined, can't autodetect type",;);
ktDescriptor typeDesc=ktDescriptor_get(typeId); ktDescriptor* type=ktDescriptor_get(typeId);
if(!typeDesc.toString) if(!type->toString)
safethrow("type descriptor doesnt have toString() func",;); safethrow("type descriptor doesnt have toString() func",;);
return SUCCESS(UniHeapPtr(char, typeDesc.toString(object, f))); return SUCCESS(UniHeapPtr(char, type->toString(object, f)));
} }
Maybe check_argsN(u8 n){ Maybe check_argsN(u8 n){
@ -73,7 +73,7 @@ void __kprint(u8 n, kp_fmt* formats, __kp_value_union* objects){
kp_fmt fmt=formats[i]; kp_fmt fmt=formats[i];
kprint_setColor(fmt); kprint_setColor(fmt);
tryLast(__next_toString(fmt, &objects[i]),maybeStr); tryLast(__next_toString(fmt, &objects[i]),maybeStr);
if(fputs(maybeStr.value.VoidPtr, stdout)==EOF)\ if(fputs(maybeStr.value.VoidPtr, stdout)==EOF) \
throw("can't write string to stdout"); throw("can't write string to stdout");
//, Unitype_free(maybeStr.value) //, Unitype_free(maybeStr.value)
Unitype_free(maybeStr.value); Unitype_free(maybeStr.value);
@ -160,14 +160,14 @@ void kprint_setColor(kp_fmt f){
#endif #endif
/* Maybe ksprint_ar(u32 count, kp_fmt format, ktid typeId, void* array){ /* Maybe ksprint_ar(u32 count, kp_fmt format, ktid typeId, void* array){
ktDescriptor typeDesc=ktDescriptor_get(format.typeId); ktDescriptor* type=ktDescriptor_get(format.typeId);
if(!typeDesc.toString) if(!type->toString)
safethrow("type descriptor doesnt have toString() func",;); safethrow("type descriptor doesnt have toString() func",;);
StringBuilder* strb=StringBuilder_create(); StringBuilder* strb=StringBuilder_create();
StringBuilder_append_char(strb, '['); StringBuilder_append_char(strb, '[');
for (u16 e=1; e<count; e++){ for (u16 e=1; e<count; e++){
StringBuilder_append_char(strb, ' '); StringBuilder_append_char(strb, ' ');
char* elStr=typeDesc.toString(array+typeDesc.size*e, &format); char* elStr=type->toString(array+type->size*e, &format);
StringBuilder_append_cptr(strb, elStr); StringBuilder_append_cptr(strb, elStr);
StringBuilder_append_char(strb, ','); StringBuilder_append_char(strb, ',');
} }

View File

@ -29,45 +29,45 @@ static inline __kp_value_union __kpVU_i(i64 f) { return (__kp_value_union){ .i64
#define __kpVU(V) __kpVU_selectType(V) #define __kpVU(V) __kpVU_selectType(V)
#define __kp_argsToFmts8(\ #define __kp_argsToFmts8( \
a0, a1, a2, a3, a4, a5, a6, a7,...)\ a0, a1, a2, a3, a4, a5, a6, a7,...) \
((i32[]){ a0,a2,a4,a6 }) ((i32[]){ a0,a2,a4,a6 })
#define __kp_argsToObjs8(\ #define __kp_argsToObjs8( \
a0, a1, a2, a3, a4, a5, a6, a7,...)\ a0, a1, a2, a3, a4, a5, a6, a7,...) \
((__kp_value_union[]){ __kpVU(a1),__kpVU(a3),__kpVU(a5),__kpVU(a7) }) ((__kp_value_union[]){ __kpVU(a1),__kpVU(a3),__kpVU(a5),__kpVU(a7) })
#define __kp_argsToFmts16(\ #define __kp_argsToFmts16( \
a0, a1, a2, a3, a4, a5, a6, a7,\ a0, a1, a2, a3, a4, a5, a6, a7, \
a8, a9, a10,a11,a12,a13,a14,a15,...)\ a8, a9, a10,a11,a12,a13,a14,a15,...) \
((i32[]){ a0,a2,a4,a6,a8,a10,a12,a14 }) ((i32[]){ a0,a2,a4,a6,a8,a10,a12,a14 })
#define __kp_argsToObjs16(\ #define __kp_argsToObjs16( \
a0, a1, a2, a3, a4, a5, a6, a7,\ a0, a1, a2, a3, a4, a5, a6, a7, \
a8, a9, a10,a11,a12,a13,a14,a15,...)\ a8, a9, a10,a11,a12,a13,a14,a15,...) \
((__kp_value_union[]){ __kpVU(a1),__kpVU(a3),__kpVU(a5),__kpVU(a7),__kpVU(a9),__kpVU(a11),__kpVU(a13),__kpVU(a15) }) ((__kp_value_union[]){ __kpVU(a1),__kpVU(a3),__kpVU(a5),__kpVU(a7),__kpVU(a9),__kpVU(a11),__kpVU(a13),__kpVU(a15) })
#define __kp_argsToFmts32(\ #define __kp_argsToFmts32( \
a0, a1, a2, a3, a4, a5, a6, a7,\ a0, a1, a2, a3, a4, a5, a6, a7, \
a8, a9, a10,a11,a12,a13,a14,a15,\ a8, a9, a10,a11,a12,a13,a14,a15, \
a16,a17,a18,a19,a20,a21,a22,a23,\ a16,a17,a18,a19,a20,a21,a22,a23, \
a24,a25,a26,a27,a28,a29,a30,a31,...)\ a24,a25,a26,a27,a28,a29,a30,a31,...) \
((i32[]){ a0,a2,a4,a6,a8,a10,a12,a14,a16,a18,a20,a22,a24,a26,a28,a30 }) ((i32[]){ a0,a2,a4,a6,a8,a10,a12,a14,a16,a18,a20,a22,a24,a26,a28,a30 })
#define __kp_argsToObjs32(\ #define __kp_argsToObjs32( \
a0, a1, a2, a3, a4, a5, a6, a7,\ a0, a1, a2, a3, a4, a5, a6, a7, \
a8, a9, a10,a11,a12,a13,a14,a15,\ a8, a9, a10,a11,a12,a13,a14,a15, \
a16,a17,a18,a19,a20,a21,a22,a23,\ a16,a17,a18,a19,a20,a21,a22,a23, \
a24,a25,a26,a27,a28,a29,a30,a31,...)\ a24,a25,a26,a27,a28,a29,a30,a31,...) \
((__kp_value_union[]){ __kpVU(a1),__kpVU(a3),__kpVU(a5),__kpVU(a7),__kpVU(a9),__kpVU(a11),__kpVU(a13),__kpVU(a15),__kpVU(a17),__kpVU(a19),__kpVU(a21),__kpVU(a23),__kpVU(a25),__kpVU(a27),__kpVU(a29),__kpVU(a31) }) ((__kp_value_union[]){ __kpVU(a1),__kpVU(a3),__kpVU(a5),__kpVU(a7),__kpVU(a9),__kpVU(a11),__kpVU(a13),__kpVU(a15),__kpVU(a17),__kpVU(a19),__kpVU(a21),__kpVU(a23),__kpVU(a25),__kpVU(a27),__kpVU(a29),__kpVU(a31) })
#define __32zeroes 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 #define __32zeroes 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
#define __kp_argsToArrs(COUNT,ARGS...)\ #define __kp_argsToArrs(COUNT,ARGS...) \
(kp_fmt*)(\ (kp_fmt*)( \
COUNT<=8 ? __kp_argsToFmts8(ARGS) :\ COUNT<=8 ? __kp_argsToFmts8(ARGS) : \
COUNT<=16 ? __kp_argsToFmts16(ARGS) :\ COUNT<=16 ? __kp_argsToFmts16(ARGS) : \
__kp_argsToFmts32(ARGS)),\ __kp_argsToFmts32(ARGS)), \
(__kp_value_union*)(\ (__kp_value_union*)( \
COUNT<=8 ? __kp_argsToObjs8(ARGS) :\ COUNT<=8 ? __kp_argsToObjs8(ARGS) : \
COUNT<=16 ? __kp_argsToObjs16(ARGS) :\ COUNT<=16 ? __kp_argsToObjs16(ARGS) : \
__kp_argsToObjs32(ARGS)) __kp_argsToObjs32(ARGS))
@ -75,8 +75,8 @@ Maybe __ksprint(u8 n, kp_fmt* formats, __kp_value_union* objects);
/// @param ARGS kp_fmt, value, kp_fmt, value... /// @param ARGS kp_fmt, value, kp_fmt, value...
///@returns Maybe<char*> ///@returns Maybe<char*>
#define ksprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION,\ #define ksprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION, \
__ksprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes))\ __ksprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) \
) )
/*-Wint-conversion warning was produced during value to __kp_value_union conversion*/ /*-Wint-conversion warning was produced during value to __kp_value_union conversion*/
@ -85,8 +85,8 @@ Maybe __kfprint(FILE* fd, u8 n, kp_fmt* formats, __kp_value_union* objects);
/// @param FD FILE* /// @param FD FILE*
/// @param ARGS kp_fmt, value, kp_fmt, value... /// @param ARGS kp_fmt, value, kp_fmt, value...
///@returns Maybe<void> ///@returns Maybe<void>
#define kfprint(FD, ARGS...) WARNING_DISABLE( W_INT_CONVERSION,\ #define kfprint(FD, ARGS...) WARNING_DISABLE( W_INT_CONVERSION, \
__kfprint(FD, count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes))\ __kfprint(FD, count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) \
) )
void __kprint(u8 n, kp_fmt* formats, __kp_value_union* objects); void __kprint(u8 n, kp_fmt* formats, __kp_value_union* objects);
@ -94,8 +94,8 @@ void __kprint(u8 n, kp_fmt* formats, __kp_value_union* objects);
///can use non-catchable throw !!! ///can use non-catchable throw !!!
///@param ARGS kp_fmt, value, kp_fmt, value... ///@param ARGS kp_fmt, value, kp_fmt, value...
///@returns void ///@returns void
#define kprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION,\ #define kprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION, \
__kprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes))\ __kprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) \
) )
///@param f bgColor | fgColor ///@param f bgColor | fgColor

View File

@ -8,7 +8,7 @@ extern "C" {
// ^ ^^^^ // ^ ^^^^
// | color num // | color num
// fgColorSet flag // fgColorSet flag
PACK_ENUM(kp_fgColor, PACKED_ENUM(kp_fgColor,
/// black foreground /// black foreground
kp_fgBlack = 0x80000000, kp_fgBlack = 0x80000000,
/// dark red foreground /// dark red foreground
@ -46,7 +46,7 @@ PACK_ENUM(kp_fgColor,
// 01000000 00000000 00000000 00000000 // 01000000 00000000 00000000 00000000
// ^ ^^^^ // ^ ^^^^
// bgColorSet flag color num // bgColorSet flag color num
PACK_ENUM(kp_bgColor, PACKED_ENUM(kp_bgColor,
/// black background /// black background
kp_bgBlack = 0x40000000, kp_bgBlack = 0x40000000,
/// dark red background /// dark red background

View File

@ -10,7 +10,7 @@ extern "C" {
/// kprint_format /// kprint_format
typedef u32 kp_fmt; typedef u32 kp_fmt;
PACK_ENUM(kp_dataFmt, PACKED_ENUM(kp_dataFmt,
// 00000000 00000000 00000000 00000000 // 00000000 00000000 00000000 00000000
// ^^^^ // ^^^^
// type // type

View File

@ -8,33 +8,33 @@
char data[]="iojihiojopijiugbjmoihftytryfdrh"; char data[]="iojihiojopijiugbjmoihftytryfdrh";
#define test_hashfunc(hasht, hashf)({\ #define test_hashfunc(hasht, hashf)({ \
kprintf("\e[94mfunction: \e[92m" #hashf "\n");\ kprintf("\e[94mfunction: \e[92m" #hashf "\n"); \
hasht h=0;\ hasht h=0; \
optime("speed test", 1, ({\ optime("speed test", 1, ({ \
for(u32 i=0; i<SPEED_TESTS; i++)\ for(u32 i=0; i<SPEED_TESTS; i++) \
h=hashf(h, data, sizeof(data));\ h=hashf(h, data, sizeof(data)); \
}));\ })); \
/*kprintf("\e[94mhash of \"\e[90m%s\e[94m\": \e[92m%x\n",data, h);*/\ /*kprintf("\e[94mhash of \"\e[90m%s\e[94m\": \e[92m%x\n",data, h);*/ \
Autoarr(hasht)* hashes=Autoarr_create(hasht,512,32768);\ Autoarr(hasht)* hashes=Autoarr_create(hasht,512,32768); \
optime("collision test",1,({\ optime("collision test",1,({ \
u32 collisions=0;\ u32 collisions=0; \
for(u64 i=0;i< COLLISION_TESTS;i++){\ for(u64 i=0;i< COLLISION_TESTS;i++){ \
hasht h=hashb(hashf, (u8*)&i, sizeof(i));\ hasht h=hashb(hashf, (u8*)&i, sizeof(i)); \
bool col=false;\ bool col=false; \
Autoarr_foreach(hashes,e,({\ Autoarr_foreach(hashes,e,({ \
if(e==h) {\ if(e==h) { \
col=true;\ col=true; \
break;\ break; \
}\ } \
}));\ })); \
if(col) collisions++;\ if(col) collisions++; \
else Autoarr_add(hashes,h);\ else Autoarr_add(hashes,h); \
}\ } \
kprintf("\e[93m%u \e[94mcollisions detected in %u hashes\n", collisions, COLLISION_TESTS);\ kprintf("\e[93m%u \e[94mcollisions detected in %u hashes\n", collisions, COLLISION_TESTS); \
}));\ })); \
Autoarr_free(hashes, true);\ Autoarr_free(hashes, true); \
kprintf("\e[96m--------------------------------------\n");\ kprintf("\e[96m--------------------------------------\n"); \
}) })
void test_hash_functions(){ void test_hash_functions(){

View File

@ -5,11 +5,11 @@
#endif #endif
#define testColor(COLOR) \ #define testColor(COLOR) \
kprint_setColor(kp_bgBlack|kp_fg##COLOR);\ kprint_setColor(kp_bgBlack|kp_fg##COLOR); \
kprintf(#COLOR " ");\ kprintf(#COLOR " "); \
kprint_setColor(kp_bg##COLOR|kp_fgGray);\ kprint_setColor(kp_bg##COLOR|kp_fgGray); \
kprintf(#COLOR);\ kprintf(#COLOR); \
kprint_setColor(kp_bgBlack|kp_fgBlack);\ kprint_setColor(kp_bgBlack|kp_fgBlack); \
kprintf("\n"); kprintf("\n");
void test_kprint_colors(){ void test_kprint_colors(){

View File

@ -2,21 +2,21 @@
#include "../src/random/krandom.h" #include "../src/random/krandom.h"
#define test_alg(ALG, VALUE_SIZE, EXPECTED_FROM_ZERO){\ #define test_alg(ALG, VALUE_SIZE, EXPECTED_FROM_ZERO){ \
kprintf("\e[94mrng algorithm: \e[96m" #ALG "\n");\ kprintf("\e[94mrng algorithm: \e[96m" #ALG "\n"); \
void* s= ALG##_init(0);\ void* s= ALG##_init(0); \
u##VALUE_SIZE r=ALG##_next(s);\ u##VALUE_SIZE r=ALG##_next(s); \
kprintf("\e[97m next from zero seed:");\ kprintf("\e[97m next from zero seed:"); \
if(r!=EXPECTED_FROM_ZERO){\ if(r!=EXPECTED_FROM_ZERO){ \
kprintf("\e[91m " IFWIN("%llu\n","%lu\n"), (u64)r);\ kprintf("\e[91m " IFWIN("%llu\n","%lu\n"), (u64)r); \
throw(ERR_UNEXPECTEDVAL);\ throw(ERR_UNEXPECTEDVAL); \
}\ } \
kprintf("\e[92m " IFWIN("%llu\n","%lu\n"), (u64)r);\ kprintf("\e[92m " IFWIN("%llu\n","%lu\n"), (u64)r); \
ALG##_free(s);\ ALG##_free(s); \
s= ALG##_initFromTime();\ s= ALG##_initFromTime(); \
r=ALG##_next(s);\ r=ALG##_next(s); \
ALG##_free(s);\ ALG##_free(s); \
kprintf("\e[97m next from time seed:\e[92m " IFWIN("%llu\n","%lu\n"), (u64)r);\ kprintf("\e[97m next from time seed:\e[92m " IFWIN("%llu\n","%lu\n"), (u64)r); \
} }
void test_rng_algorithms(){ void test_rng_algorithms(){

View File

@ -2,8 +2,8 @@
void test_type_system(){ void test_type_system(){
for(ktid id=0; id<ktid_last; id++){ for(ktid id=0; id<ktid_last; id++){
ktDescriptor d=ktDescriptor_get(id); ktDescriptor* type=ktDescriptor_get(id);
kprintf("\e[37m{ id:%u name:%s size:%u freeMembers:%p toString:%p }\n", kprintf("\e[37m{ id:%u name:%s size:%u freeMembers:%p toString:%p }\n",
d.id, d.name, d.size, d.freeMembers, d.toString); type->id, type->name, type->size, type->freeMembers, type->toString);
} }
} }