From dc6a52608a25526f16748a6aa3403300481bfcfa Mon Sep 17 00:00:00 2001 From: timerix Date: Tue, 6 Sep 2022 20:06:08 +0600 Subject: [PATCH] a lot of changes --- src/DtsodParser/DtsodV24_deserialize.c | 6 +- src/base/base.h | 4 +- src/base/cptr.h | 2 +- src/base/errors.h | 2 +- src/base/kprint/README.md | 35 ++++- src/base/kprint/data_fmt.txt | 13 -- src/base/kprint/kprint.c | 151 +++++++++------------ src/base/kprint/kprint.h | 3 + src/base/kprint/kprint_colors.h | 72 +++++----- src/base/kprint/kprint_format.h | 68 ++++------ src/base/kprint/kprint_format.md | 72 ++++++++++ src/base/optime.h | 2 +- src/base/type_system/README.md | 0 src/base/type_system/kerepTypeDescriptor.h | 22 +++ src/base/type_system/kerepTypeId.h | 17 +++ src/base/{ => type_system}/types.c | 6 +- src/base/{ => type_system}/types.h | 22 +-- src/base/{ => type_system}/unitype.c | 8 +- src/base/{ => type_system}/unitype.h | 2 +- tests/test_hashtable.c | 2 +- tests/test_safethrow.c | 2 +- 21 files changed, 294 insertions(+), 217 deletions(-) delete mode 100644 src/base/kprint/data_fmt.txt create mode 100644 src/base/kprint/kprint_format.md create mode 100644 src/base/type_system/README.md create mode 100644 src/base/type_system/kerepTypeDescriptor.h create mode 100644 src/base/type_system/kerepTypeId.h rename src/base/{ => type_system}/types.c (96%) rename src/base/{ => type_system}/types.h (75%) rename src/base/{ => type_system}/unitype.c (81%) rename src/base/{ => type_system}/unitype.h (98%) diff --git a/src/DtsodParser/DtsodV24_deserialize.c b/src/DtsodParser/DtsodV24_deserialize.c index 8acc293..ecf1322 100644 --- a/src/DtsodParser/DtsodV24_deserialize.c +++ b/src/DtsodParser/DtsodV24_deserialize.c @@ -182,7 +182,7 @@ Maybe __ParseValue(DeserializeSharedData* shared, string str){ // Float64 case 'f': { char* _c=string_extract(str); - Unitype rez=Uni(Float64,strtod(_c,NULL)); + Unitype rez=UniFloat64(strtod(_c,NULL)); free(_c); return SUCCESS(rez); } @@ -199,7 +199,7 @@ Maybe __ParseValue(DeserializeSharedData* shared, string str){ safethrow(err,free(_c)); } free(_c); - return SUCCESS(Uni(UInt64,lu)); + return SUCCESS(UniUInt64(lu)); } // Int64 case '0': case '1': case '2': case '3': case '4': @@ -215,7 +215,7 @@ Maybe __ParseValue(DeserializeSharedData* shared, string str){ safethrow(err,free(_c)); } free(_c); - return SUCCESS(Uni(Int64,li)); + return SUCCESS(UniInt64(li)); } // wrong type default: diff --git a/src/base/base.h b/src/base/base.h index c728a94..fce8588 100644 --- a/src/base/base.h +++ b/src/base/base.h @@ -8,8 +8,8 @@ extern "C" { #include "errors.h" #include "cptr.h" #include "optime.h" -#include "types.h" -#include "unitype.h" +#include "type_system/types.h" +#include "type_system/unitype.h" #include "init.h" #include "kprint/kprint.h" diff --git a/src/base/cptr.h b/src/base/cptr.h index b750842..6372931 100644 --- a/src/base/cptr.h +++ b/src/base/cptr.h @@ -4,7 +4,7 @@ extern "C" { #endif -#include "types.h" +#include "std.h" // returns length of char buffer (without \0) uint32 cptr_length(char* str); diff --git a/src/base/errors.h b/src/base/errors.h index 06f0017..324695e 100644 --- a/src/base/errors.h +++ b/src/base/errors.h @@ -5,7 +5,7 @@ extern "C" { #endif #include "std.h" -#include "unitype.h" +#include "type_system/unitype.h" typedef enum ErrorId { SUCCESS, // not an error diff --git a/src/base/kprint/README.md b/src/base/kprint/README.md index e375f83..c02cdac 100644 --- a/src/base/kprint/README.md +++ b/src/base/kprint/README.md @@ -1,2 +1,33 @@ -## kprint -I don't really like printf function, so i made safer and more convinient replacement. \ No newline at end of file +# kprint +I don't really like printf function (and its variants), so i made safer and more convinient replacement. + +| function | returns | arguments | +|----------|---------|-----------| +| kprint | void/throw | kprint_format, void*, kprint_format, void*... | +| ksprint | Maybe | kprint_format, void*, kprint_format, void*... | +| kfprint | Maybe | FILE*, kprint_format, void*, kprint_format, void*... | + +## how to use it: ++ **format construction:** + ``` + kprint_format fmt= kprint_fgColor | kprint_fbgColor | kprint_fdataFmt | flags | kerepTypeId; + ``` + [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 + + flags can be set to modify TypeDescriptor.toString() behavior + + don't forget to set TypeDescriptor.toString when registering type, or kprint will crash + ++ **using base type arguments:** + you can just put them into a function + ``` + kprint(kprint_fmtHex | kprint_fmtUppercase | kprint_fmtWithPrefix, 255); + ``` + output: 0xFF ++ **using other registered types:** + should be sent as pointers + ``` + Maybe m=MaybeNull; + kprint(kprint_fgBlue | kprint_fmtString, "Maybe: ", kprint_fgGreen | kerepTypeId_MaybePtr, &m); + ``` + output: Maybe: {value={0, kerepTypeId_Null}} \ No newline at end of file diff --git a/src/base/kprint/data_fmt.txt b/src/base/kprint/data_fmt.txt deleted file mode 100644 index 31f434f..0000000 --- a/src/base/kprint/data_fmt.txt +++ /dev/null @@ -1,13 +0,0 @@ -[data types] -int 0x2000 00100000 00000000 -uint + withPostfix + uppercase 0x2001 00100000 00000001 -hex + withPrefix + uppercase 0x2002 00100000 00000010 -bin + withPrefix 0x2003 00100000 00000011 -float + withPostfix + uppercase 0x3000 00110000 00000000 -char 0x3001 00110000 00000001 -string 0x3002 00110000 00000010 -??? 0x3003 00110000 00000011 - -[optional flags] -withPrefix = withPostfix 0x0004 00000000 00000100 -upperase 0x0008 00000000 00001000 \ No newline at end of file diff --git a/src/base/kprint/kprint.c b/src/base/kprint/kprint.c index 2fb60a8..fc89c72 100644 --- a/src/base/kprint/kprint.c +++ b/src/base/kprint/kprint.c @@ -1,112 +1,87 @@ -#include "../base.h" +/* #include "../base.h" #include "../../String/StringBuilder.h" -Maybe __next_toString(uint16 size, va_list args, StringBuilder* strb){ - kprint_format format=va_arg(args, kprint_format); - if(!format.dataFmtSet) - safethrow("format is not set", StringBuilder_free(strb)); +Maybe __next_toString(kprint_format format, void* object){ // detecting type - if(!format.typeId) switch(format.dataFmt){ + if(!format.typeId) switch((kprint_dataFormat)((uint32)0 | format.dataFmt)){ case kprint_fmtInt: case kprint_fmtHex: - case kprint_fmtBin: - switch(size){ - case 1: format.typeId=kerepTypeId_Int8; break; - case 2: format.typeId=kerepTypeId_Int16; break; - case 4: format.typeId=kerepTypeId_Int32; break; - case 8: format.typeId=kerepTypeId_Int64; break; - default: safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb)); - } - break; + case kprint_fmtBin: + format.typeId=kerepTypeId_Int64; break; case kprint_fmtUInt: - switch(size){ - case 1: format.typeId=kerepTypeId_UInt8; break; - case 2: format.typeId=kerepTypeId_UInt16; break; - case 4: format.typeId=kerepTypeId_UInt32; break; - case 8: format.typeId=kerepTypeId_UInt64; break; - default: safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb)); - } - break; + format.typeId=kerepTypeId_UInt64; break; case kprint_fmtFloat: - switch(size){ - case 4: format.typeId=kerepTypeId_Float32; break; - case 8: format.typeId=kerepTypeId_Float64; break; - default: safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb)); - } - break; + format.typeId=kerepTypeId_Float64; break; case kprint_fmtChar: - if(size!=1) - safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb)); - format.typeId=kerepTypeId_Char; - break; + format.typeId=kerepTypeId_Char; break; case kprint_fmtString: - switch(size){ - case sizeof(char*): format.typeId=kerepTypeId_CharPtr; break; - case sizeof(string): format.typeId=kerepTypeId_string; break; - default: safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb)); - } - break; + format.typeId=kerepTypeId_CharPtr; break; default: - safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb)); + safethrow("typeId is not set, can't autodetect type",;); } - try(kerepTypeDescriptor_get(format.typeId),mtd,StringBuilder_free(strb)); + try(kerepTypeDescriptor_get(format.typeId),mtd,;); kerepTypeDescriptor typeDesc=*(kerepTypeDescriptor*)mtd.value.VoidPtr; - if(size + Maybe (*toString)(void*, int32); // NULL or function which generates string representaion of object +} kerepTypeDescriptor; + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/base/type_system/kerepTypeId.h b/src/base/type_system/kerepTypeId.h new file mode 100644 index 0000000..6f4f4af --- /dev/null +++ b/src/base/type_system/kerepTypeId.h @@ -0,0 +1,17 @@ +#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 \ No newline at end of file diff --git a/src/base/types.c b/src/base/type_system/types.c similarity index 96% rename from src/base/types.c rename to src/base/type_system/types.c index 9bf5ce1..82d2d6d 100644 --- a/src/base/types.c +++ b/src/base/type_system/types.c @@ -1,5 +1,5 @@ -#include "../Autoarr/Autoarr.h" -#include "unitype.h" +#include "../../Autoarr/Autoarr.h" +#include "types.h" Autoarr_declare(kerepTypeDescriptor) Autoarr_define(kerepTypeDescriptor) @@ -59,7 +59,7 @@ void kerepTypeDescriptors_endInit(){ printf("\e[92minitialized %u type descriptors\n", kerepTypeId_last); } -void __kerepType_register(char* name, int16 size, void (*freeMembers)(void*), char* (*toString)(void*, int32)){ +void __kerepType_register(char* name, int16 size, void (*freeMembers)(void*), Maybe (*toString)(void*, int32)){ kerepTypeDescriptor typeDesc={ .name=name, .size=size, diff --git a/src/base/types.h b/src/base/type_system/types.h similarity index 75% rename from src/base/types.h rename to src/base/type_system/types.h index 96ff861..8cbaa6b 100644 --- a/src/base/types.h +++ b/src/base/type_system/types.h @@ -4,26 +4,13 @@ extern "C" { #endif -#include "std.h" +#include "../std.h" +#include "kerepTypeId.h" +#include "kerepTypeDescriptor.h" -typedef uint16 kerepTypeId; - -typedef struct kerepTypeDescriptor{ - char* name; - kerepTypeId id; - uint16 size; - void (*freeMembers)(void*); // NULL or function which frees all struct members - ///@return Maybe - Maybe (*toString)(void*, int32); // NULL or function which generates string representaion of object -} kerepTypeDescriptor; - -#define kerepTypeId_declare(ID_VAR_NAME)\ - extern kerepTypeId ID_VAR_NAME -#define kerepTypeId_define(ID_VAR_NAME)\ - kerepTypeId ID_VAR_NAME=-1 extern kerepTypeId kerepTypeId_last; -void __kerepType_register(char* name, int16 size, void (*freeMembers)(void*), char* (*toString)(void*, int32)); +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);\ @@ -35,6 +22,7 @@ void kerepTypeDescriptors_endInit(); /// @return Maybe Maybe kerepTypeDescriptor_get(kerepTypeId id); + kerepTypeId_declare(kerepTypeId_Null); kerepTypeId_declare(kerepTypeId_Char); diff --git a/src/base/unitype.c b/src/base/type_system/unitype.c similarity index 81% rename from src/base/unitype.c rename to src/base/type_system/unitype.c index 2167a9c..8b44641 100644 --- a/src/base/unitype.c +++ b/src/base/type_system/unitype.c @@ -1,10 +1,11 @@ -#include "base.h" +#include "../base.h" kerepTypeId_define(kerepTypeId_Unitype); kerepTypeId_define(kerepTypeId_UnitypePtr); void Unitype_free(Unitype u){ - kerepTypeDescriptor type=kerepTypeDescriptor_get(u.typeId); + tryLast(kerepTypeDescriptor_get(u.typeId), mType); + kerepTypeDescriptor type=*(kerepTypeDescriptor*)mType.value.VoidPtr; if(type.freeMembers) type.freeMembers(u.VoidPtr); if(u.allocatedInHeap) @@ -15,7 +16,8 @@ void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); } #define BUFSIZE 64 char* sprintuni(Unitype v){ char* buf=malloc(BUFSIZE); - kerepTypeDescriptor type=kerepTypeDescriptor_get(v.typeId); + tryLast(kerepTypeDescriptor_get(v.typeId), mType); + kerepTypeDescriptor type=*(kerepTypeDescriptor*)mType.value.VoidPtr; if(v.typeId==kerepTypeId_Null) sprintf_s(buf, BUFSIZE, "{Null}"); else if(v.typeId==kerepTypeId_Float64) diff --git a/src/base/unitype.h b/src/base/type_system/unitype.h similarity index 98% rename from src/base/unitype.h rename to src/base/type_system/unitype.h index 5dfa097..9f28bf7 100644 --- a/src/base/unitype.h +++ b/src/base/type_system/unitype.h @@ -4,7 +4,7 @@ extern "C" { #endif -#include "types.h" +#include "kerepTypeId.h" typedef struct Unitype{ union { diff --git a/tests/test_hashtable.c b/tests/test_hashtable.c index 8ff1d53..e68d02a 100644 --- a/tests/test_hashtable.c +++ b/tests/test_hashtable.c @@ -47,7 +47,7 @@ char* genkey(uint32 i){ void fill(Hashtable* ht){ for(uint32 i=0;i<100000;i++) - Hashtable_add(ht,genkey(i),Uni(UInt64,i)); + Hashtable_add(ht,genkey(i),UniUInt64(i)); } Unitype gett(Hashtable* ht){ diff --git a/tests/test_safethrow.c b/tests/test_safethrow.c index 4ccd4dc..f37812d 100644 --- a/tests/test_safethrow.c +++ b/tests/test_safethrow.c @@ -1,7 +1,7 @@ #include "tests.h" Maybe dont_throw(){ - return SUCCESS(Uni(UInt64, 9/2)); + return SUCCESS(UniUInt64(9/2)); } Maybe throw_error(){