Merge pull request #11 from Timerix22/new_unitype

new build system
This commit is contained in:
Timerix22 2022-09-09 22:14:26 +06:00 committed by GitHub
commit d9b0daf543
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
64 changed files with 971 additions and 634 deletions

1
.gitignore vendored
View File

@ -5,7 +5,6 @@ obj/
# user files
.old*/
.vs/
.vscode/
.vshistory/
.editorconfig
*.user

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "cbuild"]
path = cbuild
url = https://github.com/Timerix22/cbuild.git

1
.vscode/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
settings.json

34
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,34 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Debug",
"type": "cppdbg",
"request": "launch",
"preLaunchTask": "build_exec_dbg",
"program": "${workspaceFolder}/bin/kerep.com",
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"externalConsole": true,
"internalConsoleOptions": "openOnSessionStart",
"miDebuggerPath": "/usr/bin/gdb",
"MIMode": "gdb",
"pipeTransport": {
"debuggerPath": "gdb",
"pipeProgram": "bash",
"pipeArgs": ["-c"],
"pipeCwd": "${workspaceFolder}"
}
},
{
"name": "(msvc) Debug",
"type": "cppvsdbg",
"request": "launch",
"preLaunchTask": "build_dbg",
"cwd": "${workspaceFolder}\\bin",
"program": "${workspaceFolder}\\bin\\kerep.com",
"stopAtEntry": false,
"console": "integratedTerminal"
}
]
}

57
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,57 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build_exec",
"detail": "build project",
"type": "cppbuild",
"command": "make",
"args": [
"build_exec"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared",
"showReuseMessage": false,
"clear": true
}
},
{
"label": "build_exec_dbg",
"detail": "build project with debug symbols",
"type": "cppbuild",
"command": "make",
"args": [
"build_exec_dbg"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build"
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared",
"showReuseMessage": false,
"clear": true
}
}
]
}

View File

@ -1,24 +1,24 @@
###### Building tasks #######
build_test:
@build_scripts/build_configurations/build_test.sh
build_test_dbg:
@build_scripts/build_configurations/build_test_dbg.sh
###### Build cbuild/default_tasks #######
build_exec:
@cbuild/call_task.sh build_exec
build_exec_dbg:
@cbuild/call_task.sh build_exec_dbg
build_shared_lib:
@build_scripts/build_configurations/build_shared_lib.sh
@cbuild/call_task.sh build_shared_lib
build_shared_lib_dbg:
@cbuild/call_task.sh build_shared_lib_dbg
build_static_lib:
@build_scripts/build_configurations/build_static_lib.sh
@cbuild/call_task.sh build_static_lib
build_static_lib_dbg:
@build_scripts/build_configurations/build_static_lib_dbg.sh
@cbuild/call_task.sh build_static_lib_dbg
###### Testing tasks #######
test: build_test
@build_scripts/build_configurations/test.sh
###### Launch cbuild/default_tasks #######
exec: build_exec
@cbuild/call_task.sh exec
test_valgrind: build_test_dbg
@build_scripts/build_configurations/test_valgrind.sh
valgrind: build_exec_dbg
@cbuild/call_task.sh valgrind
all: build_test
all: build_exec

View File

@ -1,10 +0,0 @@
#!/bin/bash
source build_scripts/init.sh
print "${CYAN}==========[build_shared_lib]==========\n"
clear_dir "$OUTDIR"
clear_dir "$OBJDIR"
compile_c "$BUILD_SHARED_LIB_C_ARGS" "$SRC_C tests/test_marshalling.c"
compile_cpp "$BUILD_SHARED_LIB_CPP_ARGS" "$SRC_CPP"
link "$BUILD_SHARED_LIB_CPP_ARGS $BUILD_SHARED_LIB_LINKER_ARGS" "$SHARED_LIB_FILE"

View File

@ -1,10 +0,0 @@
#!/bin/bash
source build_scripts/init.sh
print "${CYAN}==========[build_static_lib]==========\n"
clear_dir "$OUTDIR"
clear_dir "$OBJDIR"
compile_c "$BUILD_STATIC_LIB_C_ARGS" "$SRC_C tests/test_marshalling.c"
compile_cpp "$BUILD_STATIC_LIB_CPP_ARGS" "$SRC_CPP"
pack_static_lib "$STATIC_LIB_FILE"

View File

@ -1,10 +0,0 @@
#!/bin/bash
source build_scripts/init.sh
print "${CYAN}==========[build_static_lib_dbg]==========\n"
clear_dir "$OUTDIR"
clear_dir "$OBJDIR"
compile_c "$BUILD_STATIC_LIB_DBG_C_ARGS" "$SRC_C tests/test_marshalling.c"
compile_cpp "$BUILD_STATIC_LIB_DBG_CPP_ARGS" "$SRC_CPP"
pack_static_lib "$STATIC_LIB_DBG_FILE"

View File

@ -1,10 +0,0 @@
#!/bin/bash
source build_scripts/init.sh
print "${CYAN}=============[build_test]=============\n"
clear_dir "$OUTDIR"
clear_dir "$OBJDIR"
compile_c "$BUILD_TEST_C_ARGS" "$SRC_C $TESTS_C"
compile_cpp "$BUILD_TEST_CPP_ARGS" "$SRC_CPP $TESTS_CPP"
link "$BUILD_TEST_CPP_ARGS $BUILD_TEST_LINKER_ARGS" $TEST_FILE

View File

@ -1,10 +0,0 @@
#!/bin/bash
source build_scripts/init.sh
print "${CYAN}===========[build_test_dbg]===========\n"
clear_dir "$OUTDIR"
clear_dir "$OBJDIR"
compile_c "$BUILD_TEST_DBG_C_ARGS" "$SRC_C $TESTS_C"
compile_cpp "$BUILD_TEST_DBG_CPP_ARGS" "$SRC_CPP $TESTS_CPP"
link "$BUILD_TEST_DBG_CPP_ARGS $BUILD_TEST_DBG_LINKER_ARGS" $TEST_DBG_FILE

View File

@ -1,8 +0,0 @@
#!/bin/bash
source build_scripts/init.sh
print "${CYAN}================[test]================\n"
cd $OUTDIR
./$TEST_FILE
cd ..

View File

@ -1,9 +0,0 @@
#!/bin/bash
source build_scripts/init.sh
print "${CYAN}===========[test_valgrind]============\n"
cd $OUTDIR
valgrind $VALGRIND_ARGS ./$TEST_DBG_FILE
cat "valgrind.log"
cd ..

View File

@ -1,11 +0,0 @@
#!/bin/bash
BLACK='\033[0;30m'
GRAY='\033[0;37m'
WHITE='\033[0;97m'
RED='\033[0;91m'
GREEN='\033[0;92m'
YELLOW='\033[0;93m'
BLUE='\033[0;94m'
PURPLE='\033[0;95m'
CYAN='\033[0;96m'

View File

@ -1,43 +0,0 @@
#!/bin/bash
OUTDIR=bin
OBJDIR=obj
CMP_C=gcc
CMP_CPP=g++
STD_C=c11
STD_CPP=c++17
WARN_C="-Wall -Wno-discarded-qualifiers"
WARN_CPP="-Wall"
SRC_C="$( find src -name '*.c')"
SRC_CPP="$( find src -name '*.cpp')"
TESTS_C="$( find tests -name '*.c')"
TESTS_CPP="$(find tests -name '*.cpp')"
VALGRIND_ARGS="-s --log-file=valgrind.log --read-var-info=yes --track-origins=yes --fullpath-after=kerep/ --leak-check=full --show-leak-kinds=all"
# build_test
TEST_FILE=kerep.com
BUILD_TEST_C_ARGS="-O2"
BUILD_TEST_CPP_ARGS="$BUILD_TEST_C_ARGS"
BUILD_TEST_LINKER_ARGS=""
# build_test_dbg
TEST_DBG_FILE=$TEST_FILE.dbg
BUILD_TEST_DBG_C_ARGS="-O0 -g"
BUILD_TEST_DBG_CPP_ARGS="$BUILD_TEST_DBG_C_ARGS"
BUILD_TEST_DBG_LINKER_ARGS=""
# build_shared_lib
SHARED_LIB_FILE=kerep.so
BUILD_SHARED_LIB_C_ARGS="-O2 -fpic -flto -shared"
BUILD_SHARED_LIB_CPP_ARGS="$BUILD_SHARED_LIB_C_ARGS"
BUILD_SHARED_LIB_LINKER_ARGS="-Wl,-soname,$SHARED_LIB_FILE"
# build_static_lib
STATIC_LIB_FILE=kerep.a
BUILD_STATIC_LIB_C_ARGS="-O2 -fpic"
BUILD_STATIC_LIB_CPP_ARGS="$BUILD_STATIC_LIB_C_ARGS"
# build_static_lib_dbg
STATIC_LIB_DBG_FILE="$STATIC_LIB_FILE"
BUILD_STATIC_LIB_DBG_C_ARGS="-O0 -g"
BUILD_STATIC_LIB_DBG_CPP_ARGS="$BUILD_STATIC_LIB_DBG_C_ARGS"

View File

@ -1,89 +0,0 @@
#!/bin/bash
function print {
printf "$1$GRAY"
}
function clear_dir {
print "${BLUE}clearing $1\n"
rm -rf $1
mkdir $1
}
function compile {
local cmp=$1
print "${BLUE}compiler: ${GRAY}$cmp\n"
local std=$2
print "${BLUE}standard: ${GRAY}$std\n"
local warn=$3
print "${BLUE}warnings: ${GRAY}$warn\n"
local args=$4
print "${BLUE}args: ${GRAY}$args\n"
local sources=$5
print "${BLUE}sources: ${GRAY}$sources\n"
local compilation_error=0
for srcfile in $sources
do (
local object="$OBJDIR/$(basename $srcfile).o"
if ! $($cmp -std=$std $warn $args -c -o $object $srcfile)
then
print "${RED}some error happened\n"
compilation_error=1
fi
) & done
wait
if [ $compilation_error != 0 ]
then
exit 1
fi
}
# (args, sources)
function compile_c {
print "${CYAN}-------------[compile_c]--------------\n"
compile $CMP_C $STD_C "$WARN_C" "$1" "$2"
}
# (args, sources)
function compile_cpp {
print "${CYAN}------------[compile_cpp]-------------\n"
compile $CMP_CPP $STD_CPP "$WARN_CPP" "$1" "$2"
}
# (args, outfile)
function link {
print "${CYAN}----------------[link]----------------\n"
local args=$1
print "${BLUE}args: ${GRAY}$args\n"
local outfile=$OUTDIR/$2
print "${BLUE}outfile: ${GRAY}$outfile\n"
local objects="$(find $OBJDIR -name *.o)"
print "${BLUE}objects: ${GRAY}$objects\n"
if $CMP_CPP $args -o $outfile $(echo $objects | tr '\n' ' ')
then
print "${GREEN}file $CYAN$outfile ${GREEN}created\n"
rm -rf $OBJDIR
else
print "${RED}some error happened\n"
exit 1
fi
}
# (outfile)
function pack_static_lib {
print "${CYAN}----------------[link]----------------\n"
local outfile=$OUTDIR/$1
print "${BLUE}outfile: ${GRAY}$outfile\n"
local objects="$(find $OBJDIR -name *.o)"
print "${BLUE}objects: ${GRAY}$objects\n"
if ar rcs $outfile $(echo $objects | tr '\n' ' ')
then
print "${GREEN}file $CYAN$outfile ${GREEN}created\n"
rm -rf $OBJDIR
else
print "${RED}some error happened\n"
exit 1
fi
}

View File

@ -1,22 +0,0 @@
#!/bin/bash
tabs 4
source build_scripts/colors.sh
source build_scripts/functions.sh
if [ ! -f ".config" ]; then
print "${YELLOW}./.config doesn't exists\n"
cp build_scripts/default.config.sh .config
print "${YELLOW}default config created\n"
while true; do
print "${WHITE}continue? (y/n) "
read answ
case $answ in
[Yy] ) break;;
[Nn] ) exit;;
* ) print "${RED}incorrect answer\n";;
esac
done
fi
source .config

1
cbuild Submodule

@ -0,0 +1 @@
Subproject commit ff04da5c198cea82a7948eb882e3cc96c7b5686c

View File

@ -13,35 +13,35 @@ Autoarr_define(int32)
Autoarr_define(uint64)
Autoarr_define(int64)
kerepTypeId_define(kerepTypeId_AutoarrChar);
kerepTypeId_define(kerepTypeId_AutoarrBool);
kerepTypeId_define(kerepTypeId_AutoarrFloat32);
kerepTypeId_define(kerepTypeId_AutoarrFloat64);
kerepTypeId_define(kerepTypeId_AutoarrInt8);
kerepTypeId_define(kerepTypeId_AutoarrUInt8);
kerepTypeId_define(kerepTypeId_AutoarrInt16);
kerepTypeId_define(kerepTypeId_AutoarrUInt16);
kerepTypeId_define(kerepTypeId_AutoarrInt32);
kerepTypeId_define(kerepTypeId_AutoarrUInt32);
kerepTypeId_define(kerepTypeId_AutoarrInt64);
kerepTypeId_define(kerepTypeId_AutoarrUInt64);
ktId_define(ktId_AutoarrChar);
ktId_define(ktId_AutoarrBool);
ktId_define(ktId_AutoarrFloat32);
ktId_define(ktId_AutoarrFloat64);
ktId_define(ktId_AutoarrInt8);
ktId_define(ktId_AutoarrUInt8);
ktId_define(ktId_AutoarrInt16);
ktId_define(ktId_AutoarrUInt16);
ktId_define(ktId_AutoarrInt32);
ktId_define(ktId_AutoarrUInt32);
ktId_define(ktId_AutoarrInt64);
ktId_define(ktId_AutoarrUInt64);
kerepTypeId_define(kerepTypeId_AutoarrCharPtr);
kerepTypeId_define(kerepTypeId_AutoarrBoolPtr);
kerepTypeId_define(kerepTypeId_AutoarrFloat32Ptr);
kerepTypeId_define(kerepTypeId_AutoarrFloat64Ptr);
kerepTypeId_define(kerepTypeId_AutoarrInt8Ptr);
kerepTypeId_define(kerepTypeId_AutoarrUInt8Ptr);
kerepTypeId_define(kerepTypeId_AutoarrInt16Ptr);
kerepTypeId_define(kerepTypeId_AutoarrUInt16Ptr);
kerepTypeId_define(kerepTypeId_AutoarrInt32Ptr);
kerepTypeId_define(kerepTypeId_AutoarrUInt32Ptr);
kerepTypeId_define(kerepTypeId_AutoarrInt64Ptr);
kerepTypeId_define(kerepTypeId_AutoarrUInt64Ptr);
ktId_define(ktId_AutoarrCharPtr);
ktId_define(ktId_AutoarrBoolPtr);
ktId_define(ktId_AutoarrFloat32Ptr);
ktId_define(ktId_AutoarrFloat64Ptr);
ktId_define(ktId_AutoarrInt8Ptr);
ktId_define(ktId_AutoarrUInt8Ptr);
ktId_define(ktId_AutoarrInt16Ptr);
ktId_define(ktId_AutoarrUInt16Ptr);
ktId_define(ktId_AutoarrInt32Ptr);
ktId_define(ktId_AutoarrUInt32Ptr);
ktId_define(ktId_AutoarrInt64Ptr);
ktId_define(ktId_AutoarrUInt64Ptr);
Autoarr_define(Unitype)
kerepTypeId_define(kerepTypeId_AutoarrUnitype);
kerepTypeId_define(kerepTypeId_AutoarrUnitypePtr);
ktId_define(ktId_AutoarrUnitype);
ktId_define(ktId_AutoarrUnitypePtr);
// right func to clear array of unitype values
void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr){

View File

@ -20,35 +20,35 @@ Autoarr_declare(uint32)
Autoarr_declare(int64)
Autoarr_declare(uint64)
kerepTypeId_declare(kerepTypeId_AutoarrChar);
kerepTypeId_declare(kerepTypeId_AutoarrBool);
kerepTypeId_declare(kerepTypeId_AutoarrFloat32);
kerepTypeId_declare(kerepTypeId_AutoarrFloat64);
kerepTypeId_declare(kerepTypeId_AutoarrInt8);
kerepTypeId_declare(kerepTypeId_AutoarrUInt8);
kerepTypeId_declare(kerepTypeId_AutoarrInt16);
kerepTypeId_declare(kerepTypeId_AutoarrUInt16);
kerepTypeId_declare(kerepTypeId_AutoarrInt32);
kerepTypeId_declare(kerepTypeId_AutoarrUInt32);
kerepTypeId_declare(kerepTypeId_AutoarrInt64);
kerepTypeId_declare(kerepTypeId_AutoarrUInt64);
ktId_declare(ktId_AutoarrChar);
ktId_declare(ktId_AutoarrBool);
ktId_declare(ktId_AutoarrFloat32);
ktId_declare(ktId_AutoarrFloat64);
ktId_declare(ktId_AutoarrInt8);
ktId_declare(ktId_AutoarrUInt8);
ktId_declare(ktId_AutoarrInt16);
ktId_declare(ktId_AutoarrUInt16);
ktId_declare(ktId_AutoarrInt32);
ktId_declare(ktId_AutoarrUInt32);
ktId_declare(ktId_AutoarrInt64);
ktId_declare(ktId_AutoarrUInt64);
kerepTypeId_declare(kerepTypeId_AutoarrCharPtr);
kerepTypeId_declare(kerepTypeId_AutoarrBoolPtr);
kerepTypeId_declare(kerepTypeId_AutoarrFloat32Ptr);
kerepTypeId_declare(kerepTypeId_AutoarrFloat64Ptr);
kerepTypeId_declare(kerepTypeId_AutoarrInt8Ptr);
kerepTypeId_declare(kerepTypeId_AutoarrUInt8Ptr);
kerepTypeId_declare(kerepTypeId_AutoarrInt16Ptr);
kerepTypeId_declare(kerepTypeId_AutoarrUInt16Ptr);
kerepTypeId_declare(kerepTypeId_AutoarrInt32Ptr);
kerepTypeId_declare(kerepTypeId_AutoarrUInt32Ptr);
kerepTypeId_declare(kerepTypeId_AutoarrInt64Ptr);
kerepTypeId_declare(kerepTypeId_AutoarrUInt64Ptr);
ktId_declare(ktId_AutoarrCharPtr);
ktId_declare(ktId_AutoarrBoolPtr);
ktId_declare(ktId_AutoarrFloat32Ptr);
ktId_declare(ktId_AutoarrFloat64Ptr);
ktId_declare(ktId_AutoarrInt8Ptr);
ktId_declare(ktId_AutoarrUInt8Ptr);
ktId_declare(ktId_AutoarrInt16Ptr);
ktId_declare(ktId_AutoarrUInt16Ptr);
ktId_declare(ktId_AutoarrInt32Ptr);
ktId_declare(ktId_AutoarrUInt32Ptr);
ktId_declare(ktId_AutoarrInt64Ptr);
ktId_declare(ktId_AutoarrUInt64Ptr);
Autoarr_declare(Unitype)
kerepTypeId_declare(kerepTypeId_AutoarrUnitype);
kerepTypeId_declare(kerepTypeId_AutoarrUnitypePtr);
ktId_declare(ktId_AutoarrUnitype);
ktId_declare(ktId_AutoarrUnitypePtr);
// this function is injected in kerep_init()
void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr);

View File

@ -98,9 +98,9 @@ Maybe __ReadName(DeserializeSharedData* shared){
case '}':
if(!calledRecursively || nameStr.length!=0)
safethrow_wrongchar(c,;);
return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr,NULL));
return SUCCESS(UniHeap(ktId_CharPtr,NULL));
case ':':
return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr,string_extract(nameStr)));
return SUCCESS(UniHeap(ktId_CharPtr,string_extract(nameStr)));
case '$':
if(nameStr.length!=0)
safethrow_wrongchar(c,;);
@ -113,7 +113,7 @@ Maybe __ReadName(DeserializeSharedData* shared){
}
if(nameStr.length>0) safethrow(ERR_ENDOFSTR,;);
return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr,NULL));
return SUCCESS(UniHeap(ktId_CharPtr,NULL));
}
#define ReadName() __ReadName(shared)
@ -137,7 +137,7 @@ Maybe __ReadString(DeserializeSharedData* shared){
}
else {
char* str=StringBuilder_build(b).ptr;
return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr,str));
return SUCCESS(UniHeap(ktId_CharPtr,str));
}
}
else {
@ -157,13 +157,13 @@ Maybe __ReadList(DeserializeSharedData* shared){
try(ReadValue((&readingList)), val, Autoarr_free(list, true))
Autoarr_add(list,val.value);
if (!readingList){
if(val.value.typeId==kerepTypeId_Null)
if(val.value.typeId==ktId_Null)
Autoarr_pop(list);
break;
}
}
return SUCCESS(UniPtrHeap(kerepTypeId_AutoarrUnitypePtr,list));
return SUCCESS(UniHeap(ktId_AutoarrUnitypePtr,list));
};
#define ReadList() __ReadList(shared)
@ -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:
@ -275,7 +275,7 @@ Maybe __ReadValue(DeserializeSharedData* shared, bool* readingList){
case ';':
case ',':
if(valueStr.length!=0){
if(value.typeId!=kerepTypeId_Null)
if(value.typeId!=ktId_Null)
safethrow_wrongchar(c,Unitype_free(value));
try(ParseValue(valueStr),maybeParsed,;)
value=maybeParsed.value;
@ -321,7 +321,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively) {
}
else{
list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
Hashtable_add(dict,nameCPtr,UniPtrHeap(kerepTypeId_AutoarrUnitypePtr,list));
Hashtable_add(dict,nameCPtr,UniHeap(ktId_AutoarrUnitypePtr,list));
}
Autoarr_add(list,val.value);
}
@ -331,7 +331,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively) {
END:
*_text=text;
return SUCCESS(UniPtrHeap(kerepTypeId_HashtablePtr,dict));
return SUCCESS(UniHeap(ktId_HashtablePtr,dict));
}
Maybe DtsodV24_deserialize(char* _text) {

View File

@ -23,18 +23,18 @@ void __AppendTabs(SerializeSharedData* shared) {
Maybe __AppendValue(SerializeSharedData* shared, Unitype u);
#define AppendValue(UNI) __AppendValue(shared, UNI)
Maybe __AppendValue(SerializeSharedData* shared, Unitype u){
if(u.typeId==kerepTypeId_Int64){
if(u.typeId==ktId_Int64){
StringBuilder_append_int64(b,u.Int64);
}
else if(u.typeId==kerepTypeId_UInt64){
else if(u.typeId==ktId_UInt64){
StringBuilder_append_uint64(b,u.UInt64);
addc('u');
}
else if(u.typeId==kerepTypeId_Float64){
else if(u.typeId==ktId_Float64){
StringBuilder_append_float64(b,u.Float64);
addc('f');
}
else if(u.typeId==kerepTypeId_CharPtr){
else if(u.typeId==ktId_CharPtr){
addc('"');
char c;
while((c=*(char*)(u.VoidPtr++))){
@ -43,13 +43,13 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){
}
addc('"');
}
else if(u.typeId==kerepTypeId_Bool){
else if(u.typeId==ktId_Bool){
StringBuilder_append_cptr(b, u.Bool ? "true" : "false");
}
else if(u.typeId==kerepTypeId_Null){
else if(u.typeId==ktId_Null){
safethrow("Null isn't supported in DtsodV24",;);
}
else if(u.typeId==kerepTypeId_AutoarrUnitypePtr){
else if(u.typeId==ktId_AutoarrUnitypePtr){
if(Autoarr_length(((Autoarr_Unitype*)(u.VoidPtr)))){
addc('\n');
AppendTabs();
@ -72,7 +72,7 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){
addc(']');
}
}
else if(u.typeId==kerepTypeId_HashtablePtr){
else if(u.typeId==ktId_HashtablePtr){
// check hashtable is blank
bool hashtableNotBlank=false;
Hashtable_foreach(((Hashtable*)u.VoidPtr), __, ({
@ -126,5 +126,5 @@ Maybe DtsodV24_serialize(Hashtable* dtsod){
StringBuilder* sb=StringBuilder_create();
try(__serialize(sb,0,dtsod),__, StringBuilder_free(sb));
char* str=StringBuilder_build(sb).ptr;
return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr, str));
return SUCCESS(UniHeap(ktId_CharPtr, str));
}

View File

@ -1,7 +1,7 @@
#include "Hashtable.h"
kerepTypeId_define(kerepTypeId_Hashtable);
kerepTypeId_define(kerepTypeId_HashtablePtr);
ktId_define(ktId_Hashtable);
ktId_define(ktId_HashtablePtr);
// amount of rows
static const uint16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521};
@ -96,7 +96,7 @@ Unitype Hashtable_get(Hashtable* ht, char* key){
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){
Unitype u=Hashtable_get(ht,key);
*output=u;
return u.typeId!=kerepTypeId_Null;
return u.typeId!=ktId_Null;
}
void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u){

View File

@ -11,8 +11,8 @@ typedef struct Hashtable{
uint8 hein; // height=HT_HEIGHTS[hein]
Autoarr(KVPair)** rows; // Autoarr[height]
} Hashtable;
kerepTypeId_declare(kerepTypeId_Hashtable);
kerepTypeId_declare(kerepTypeId_HashtablePtr);
ktId_declare(ktId_Hashtable);
ktId_declare(ktId_HashtablePtr);
Hashtable* Hashtable_create();
void Hashtable_free(Hashtable* ht);

View File

@ -1,11 +1,11 @@
#include "KeyValuePair.h"
kerepTypeId_define(kerepTypeId_KVPair);
kerepTypeId_define(kerepTypeId_KVPairPtr);
ktId_define(ktId_KVPair);
ktId_define(ktId_KVPairPtr);
Autoarr_define(KVPair)
kerepTypeId_define(kerepTypeId_AutoarrKVPair);
kerepTypeId_define(kerepTypeId_AutoarrKVPairPtr);
ktId_define(ktId_AutoarrKVPair);
ktId_define(ktId_AutoarrKVPairPtr);
// proper way to clear a KVP
void KVPair_free(KVPair p){

View File

@ -11,12 +11,12 @@ typedef struct KVPair{
char* key;
Unitype value;
} KVPair;
kerepTypeId_declare(kerepTypeId_KVPair);
kerepTypeId_declare(kerepTypeId_KVPairPtr);
ktId_declare(ktId_KVPair);
ktId_declare(ktId_KVPairPtr);
Autoarr_declare(KVPair)
kerepTypeId_declare(kerepTypeId_AutoarrKVPair);
kerepTypeId_declare(kerepTypeId_AutoarrKVPairPtr);
ktId_declare(ktId_AutoarrKVPair);
ktId_declare(ktId_AutoarrKVPairPtr);
// proper way to clear a KVP
void KVPair_free(KVPair p);

View File

@ -1,12 +1,12 @@
#include "SearchTree.h"
kerepTypeId_define(kerepTypeId_STNode);
kerepTypeId_define(kerepTypeId_STNodePtr);
ktId_define(ktId_STNode);
ktId_define(ktId_STNodePtr);
STNode* STNode_create(){
STNode* node=malloc(sizeof(STNode));
node->branches=NULL;
node->value.typeId=kerepTypeId_Null;
node->value.typeId=ktId_Null;
node->value.UInt64=0;
return node;
}

View File

@ -11,8 +11,8 @@ typedef struct SearchTreeNode{
struct SearchTreeNode**** branches; // *STNode[8][8][4]
Unitype value;
} STNode;
kerepTypeId_declare(kerepTypeId_STNode);
kerepTypeId_declare(kerepTypeId_STNodePtr);
ktId_declare(ktId_STNode);
ktId_declare(ktId_STNodePtr);
STNode* STNode_create();
void STNode_free(STNode* node);

View File

@ -1,11 +1,11 @@
#include "StringBuilder.h"
Autoarr_define(string)
kerepTypeId_define(kerepTypeId_AutoarrString);
kerepTypeId_define(kerepTypeId_AutoarrStringPtr);
ktId_define(ktId_AutoarrString);
ktId_define(ktId_AutoarrStringPtr);
kerepTypeId_define(kerepTypeId_StringBuilder);
kerepTypeId_define(kerepTypeId_StringBuilderPtr);
ktId_define(ktId_StringBuilder);
ktId_define(ktId_StringBuilderPtr);
#define BL_C 32
#define BL_L 1024

View File

@ -8,15 +8,15 @@ extern "C" {
#include "string.h"
Autoarr_declare(string)
kerepTypeId_declare(kerepTypeId_AutoarrString);
kerepTypeId_declare(kerepTypeId_AutoarrStringPtr);
ktId_declare(ktId_AutoarrString);
ktId_declare(ktId_AutoarrStringPtr);
typedef struct StringBuilder{
Autoarr(string)* compl_bufs;
Autoarr(int8)* curr_buf;
} StringBuilder;
kerepTypeId_declare(kerepTypeId_StringBuilder);
kerepTypeId_declare(kerepTypeId_StringBuilderPtr);
ktId_declare(ktId_StringBuilder);
ktId_declare(ktId_StringBuilderPtr);
StringBuilder* StringBuilder_create(void);
void StringBuilder_free(StringBuilder* b);

View File

@ -1,5 +1,8 @@
#include "string.h"
ktId_define(ktId_string);
ktId_define(ktId_stringPtr);
// copies str content to new char pointer value (adding '\0' at the end)
char* string_extract(string str){
if(str.length==0) return NULL;

View File

@ -12,6 +12,8 @@ typedef struct string{
char* ptr; // char pointer
uint32 length; // amount of chars in ptr value
} string;
ktId_declare(ktId_string);
ktId_declare(ktId_stringPtr);
static const string stringNull={NULL,0};

View File

@ -8,9 +8,8 @@ extern "C" {
#include "errors.h"
#include "cptr.h"
#include "optime.h"
#include "types.h"
#include "unitype.h"
#include "init.h"
#include "type_system/type_system.h"
#include "kprint/kprint.h"
#if __cplusplus
}

View File

@ -34,3 +34,16 @@ char* char_multiply(char c, uint32 n){
rez[n]=c;
return rez;
}
bool cptr_startsWith(char* ptr, char* fragment){
for(char cs=*ptr, cf=*fragment; cf; cs=*++ptr, cf=*++fragment)
if(cs!=cf) return false;
return true;
}
bool cptr_endsWith(char* ptr, char* fragment){
ptr+=cptr_length(ptr)-cptr_length(fragment);
for(char cs=*ptr, cf=*fragment; cf; cs=*++ptr, cf=*++fragment)
if(cs!=cf) return false;
return true;
}

View File

@ -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);
@ -18,6 +18,10 @@ bool cptr_compare(char* key0, char* key1);
// multiplies char n times
char* char_multiply(char c, uint32 n);
bool cptr_startsWith(char* ptr, char* fragment);
bool cptr_endsWith(char* ptr, char* fragment);
#if __cplusplus
}
#endif

View File

@ -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
@ -59,12 +59,12 @@ char* __unknownErr( );
freeMem;\
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\
return _rezult;\
}else
}
#define tryLast(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){\
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\
__EXIT(_rezult.errmsg);\
}else
}
#endif

View File

@ -1,97 +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);
// base types
kerepType_register(char, kerepTypeId_Char, NULL);
kerepType_register(bool, kerepTypeId_Bool, NULL);
kerepType_register(float32, kerepTypeId_Float32, NULL);
kerepType_register(float64, kerepTypeId_Float64, NULL);
kerepType_register(int8, kerepTypeId_Int8, NULL);
kerepType_register(uint8, kerepTypeId_UInt8, NULL);
kerepType_register(int16, kerepTypeId_Int16, NULL);
kerepType_register(uint16, kerepTypeId_UInt16, NULL);
kerepType_register(int32, kerepTypeId_Int32, NULL);
kerepType_register(uint32, kerepTypeId_UInt32, NULL);
kerepType_register(int64, kerepTypeId_Int64, NULL);
kerepType_register(uint64, kerepTypeId_UInt64, NULL);
// base type pointers
kerepType_register(char*, kerepTypeId_CharPtr, NULL);
kerepType_register(bool*, kerepTypeId_BoolPtr, NULL);
kerepType_register(float32*, kerepTypeId_Float32Ptr, NULL);
kerepType_register(float64*, kerepTypeId_Float64Ptr, NULL);
kerepType_register(int8*, kerepTypeId_Int8Ptr, NULL);
kerepType_register(uint8*, kerepTypeId_UInt8Ptr, NULL);
kerepType_register(int16*, kerepTypeId_Int16Ptr, NULL);
kerepType_register(uint16*, kerepTypeId_UInt16Ptr, NULL);
kerepType_register(int32*, kerepTypeId_Int32Ptr, NULL);
kerepType_register(uint32*, kerepTypeId_UInt32Ptr, NULL);
kerepType_register(int64*, kerepTypeId_Int64Ptr, NULL);
kerepType_register(uint64*, kerepTypeId_UInt64Ptr, NULL);
// base type autoarrs
kerepType_register(Autoarr_char, kerepTypeId_AutoarrChar, ____Autoarr_free_char);
kerepType_register(Autoarr_bool, kerepTypeId_AutoarrBool, ____Autoarr_free_bool);
kerepType_register(Autoarr_float32, kerepTypeId_AutoarrFloat32, ____Autoarr_free_float32);
kerepType_register(Autoarr_float64, kerepTypeId_AutoarrFloat64, ____Autoarr_free_float64);
kerepType_register(Autoarr_int8, kerepTypeId_AutoarrInt8, ____Autoarr_free_int8);
kerepType_register(Autoarr_uint8, kerepTypeId_AutoarrUInt8, ____Autoarr_free_uint8);
kerepType_register(Autoarr_int16, kerepTypeId_AutoarrInt16, ____Autoarr_free_int16);
kerepType_register(Autoarr_uint16, kerepTypeId_AutoarrUInt16, ____Autoarr_free_uint16);
kerepType_register(Autoarr_int32, kerepTypeId_AutoarrInt32, ____Autoarr_free_int32);
kerepType_register(Autoarr_uint32, kerepTypeId_AutoarrUInt32, ____Autoarr_free_uint32);
kerepType_register(Autoarr_int64, kerepTypeId_AutoarrInt64, ____Autoarr_free_int64);
kerepType_register(Autoarr_uint64, kerepTypeId_AutoarrUInt64, ____Autoarr_free_uint64);
// base type autoarr pointers
kerepType_register(Autoarr_char*, kerepTypeId_AutoarrCharPtr, ____Autoarr_free_char);
kerepType_register(Autoarr_bool*, kerepTypeId_AutoarrBoolPtr, ____Autoarr_free_bool);
kerepType_register(Autoarr_float32*, kerepTypeId_AutoarrFloat32Ptr, ____Autoarr_free_float32);
kerepType_register(Autoarr_float64*, kerepTypeId_AutoarrFloat64Ptr, ____Autoarr_free_float64);
kerepType_register(Autoarr_int8*, kerepTypeId_AutoarrInt8Ptr, ____Autoarr_free_int8);
kerepType_register(Autoarr_uint8*, kerepTypeId_AutoarrUInt8Ptr, ____Autoarr_free_uint8);
kerepType_register(Autoarr_int16*, kerepTypeId_AutoarrInt16Ptr, ____Autoarr_free_int16);
kerepType_register(Autoarr_uint16*, kerepTypeId_AutoarrUInt16Ptr, ____Autoarr_free_uint16);
kerepType_register(Autoarr_int32*, kerepTypeId_AutoarrInt32Ptr, ____Autoarr_free_int32);
kerepType_register(Autoarr_uint32*, kerepTypeId_AutoarrUInt32Ptr, ____Autoarr_free_uint32);
kerepType_register(Autoarr_int64*, kerepTypeId_AutoarrInt64Ptr, ____Autoarr_free_int64);
kerepType_register(Autoarr_uint64*, kerepTypeId_AutoarrUInt64Ptr, ____Autoarr_free_uint64);
// Unitype
kerepType_register(Unitype, kerepTypeId_Unitype, __UnitypePtr_free);
kerepType_register(Unitype*, kerepTypeId_UnitypePtr, __UnitypePtr_free);
kerepType_register(Autoarr_Unitype, kerepTypeId_AutoarrUnitype, ____Autoarr_free_Unitype_);
kerepType_register(Autoarr_Unitype*, kerepTypeId_AutoarrUnitypePtr, ____Autoarr_free_Unitype_);
// replacing autogenerated freear() function to custom
Autoarr_Unitype* _uar=Autoarr_create(Unitype, 1, 1);
_uar->functions->freear=__Autoarr_free_Unitype_;
Autoarr_free(_uar, true);
// SearchTreeNode
kerepType_register(STNode, kerepTypeId_STNode, __STNode_free);
kerepType_register(STNode*, kerepTypeId_STNodePtr, __STNode_free);
// KeyValuePair
kerepType_register(KVPair, kerepTypeId_KVPair, __KVPair_free);
kerepType_register(KVPair*, kerepTypeId_KVPairPtr, __KVPair_free);
kerepType_register(Autoarr_KVPair, kerepTypeId_AutoarrKVPair, ____Autoarr_free_KVPair_);
kerepType_register(Autoarr_KVPair*, kerepTypeId_AutoarrKVPairPtr, ____Autoarr_free_KVPair_);
// replacing autogenerated freear() function to custom
Autoarr_KVPair* _kvpar=Autoarr_create(KVPair, 1, 1);
_kvpar->functions->freear=__Autoarr_free_KVPair_;
Autoarr_free(_kvpar, true);
// Hashtable
kerepType_register(Hashtable, kerepTypeId_Hashtable, __Hashtable_free);
kerepType_register(Hashtable*, kerepTypeId_HashtablePtr, __Hashtable_free);
// StringBuilder
kerepType_register(Autoarr_string, kerepTypeId_AutoarrString, ____Autoarr_free_string);
kerepType_register(Autoarr_string*, kerepTypeId_AutoarrStringPtr, ____Autoarr_free_string);
kerepType_register(StringBuilder, kerepTypeId_StringBuilder, __StringBuilder_free);
kerepType_register(StringBuilder*, kerepTypeId_StringBuilderPtr, __StringBuilder_free);
}

View File

@ -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

33
src/base/kprint/README.md Normal file
View File

@ -0,0 +1,33 @@
# 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<char*> | kprint_format, void*, kprint_format, void*... |
| kfprint | Maybe<void> | FILE*, kprint_format, void*, kprint_format, void*... |
## how to use it:
+ **format construction:**
```
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 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
+ **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 | ktId_MaybePtr, &m);
```
output: <span style="color:blue">Maybe:</span> <span style="color:lightgreen">{value={0, ktId_Null}}</span>

87
src/base/kprint/kprint.c Normal file
View File

@ -0,0 +1,87 @@
/* #include "../base.h"
#include "../../String/StringBuilder.h"
Maybe __next_toString(kprint_format format, void* object){
// detecting type
if(!format.typeId) switch((kprint_dataFormat)((uint32)0 | format.dataFmt)){
case kprint_fmtInt:
case kprint_fmtHex:
case kprint_fmtBin:
format.typeId=ktId_Int64; break;
case kprint_fmtUInt:
format.typeId=ktId_UInt64; break;
case kprint_fmtFloat:
format.typeId=ktId_Float64; break;
case kprint_fmtChar:
format.typeId=ktId_Char; break;
case kprint_fmtString:
format.typeId=ktId_CharPtr; break;
default:
safethrow("typeId is not set, can't autodetect type",;);
}
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,;);
return SUCCESS(mStr.value);
}
Maybe __ksprint(uint8 n, kprint_format* formats, void** objects){
StringBuilder* strb=StringBuilder_create();
for(uint8 i=0; i<n; i++){
try(__next_toString(formats[i], objects[i]),mStr,;);
StringBuilder_append_cptr(strb, mStr.value.VoidPtr);
}
char* rezult=StringBuilder_build(strb).ptr;
return SUCCESS(UniHeap(ktId_CharPtr, rezult));
}
Maybe __kfprint(FILE* file, uint8 n, kprint_format* formats, void** objects){
for(uint8 i=0; i<n; i++){
try(__next_toString(formats[i], objects[i]),maybeStr,;);
if(fputs(maybeStr.value.VoidPtr, file)==EOF)
safethrow("can't write string to file", Unitype_free(maybeStr.value));
Unitype_free(maybeStr.value);
}
return MaybeNull;
}
void __kprint(uint8 n, kprint_format* formats, void** objects){
for(uint8 i=0; i<n; i++){
kprint_format fmt=formats[i];
kprint_setColor(fmt);
tryLast(__next_toString(fmt, objects[i]),maybeStr);
if(fputs(maybeStr.value.VoidPtr, stdout)==EOF)\
throw("can't write string to stdout");
//, Unitype_free(maybeStr.value)
Unitype_free(maybeStr.value);
}
}
void kprint_setColor(kprint_format f){
if(!f.bgColorChanged | !f.fgColorChanged)
return;
}
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();
StringBuilder_append_char(strb, '[');
for (uint16 e=1; e<count; e++){
StringBuilder_append_char(strb, ' ');
try(typeDesc.toString(array+typeDesc.size*e, &format), estr, StringBuilder_free(strb));
StringBuilder_append_cptr(strb, estr.value.VoidPtr);
StringBuilder_append_char(strb, ',');
}
StringBuilder_rmchar(strb);
StringBuilder_append_char(strb, ' ');
StringBuilder_append_char(strb, ']');
} */

78
src/base/kprint/kprint.h Normal file
View File

@ -0,0 +1,78 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../errors.h"
#include "kprint_colors.h"
#include "kprint_format.h"
#define __count_args(\
a0, a1, a2, a3, a4, a5, a6, a7,\
a8, a9, a10,a11,a12,a13,a14,a15,\
a16,a17,a18,a19,a20,a21,a22,a23,\
a24,a25,a26,a27,a28,a29,a30,a31,\
a32) a32
#define count_args(ARGS...) __count_args(\
ARGS,\
32,31,30,29,28,27,26,25,\
24,23,22,21,20,19,18,17,\
16,15,14,13,12,11,10,9,\
8, 7, 6, 5, 4, 3, 2, 1, 0)
#define __kprint_argsToFormats8(\
a0, a1, a2, a3, a4, a5, a6, a7)\
(kprint_format[]{ a0,a2,a4,a6 })
#define __kprint_argsToObjects8(\
a0, a1, a2, a3, a4, a5, a6, a7)\
(void*[]{ a1,a3,a5,a7 })
#define __kprint_argsToFormats16(\
a0, a1, a2, a3, a4, a5, a6, a7,\
a8, a9, a10,a11,a12,a13,a14,a15)\
(kprint_format[]{ a0,a2,a4,a6,a8,a10,a12,a14 })
#define __kprint_argsToObjects16(\
a0, a1, a2, a3, a4, a5, a6, a7,\
a8, a9, a10,a11,a12,a13,a14,a15)\
(void*[]{ a1,a3,a5,a7,a9,a11,a13,a15 })
#define __kprint_argsToFormats32(\
a0, a1, a2, a3, a4, a5, a6, a7,\
a8, a9, a10,a11,a12,a13,a14,a15,\
a16,a17,a18,a19,a20,a21,a22,a23,\
a24,a25,a26,a27,a28,a29,a30,a31)\
(kprint_format[]{ a0,a2,a4,a6,a8,a10,a12,a14,a16,a18,a20,a22,a24,a26,a28,a30 }
#define __kprint_argsToObjects32(\
a0, a1, a2, a3, a4, a5, a6, a7,\
a8, a9, a10,a11,a12,a13,a14,a15,\
a16,a17,a18,a19,a20,a21,a22,a23,\
a24,a25,a26,a27,a28,a29,a30,a31)\
(void*[]{ a1,a3,a5,a7,a9,a11,a13,a15,a17,a19,a21,a23,a25,a27,a29,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 __kprint_argsToArrs(COUNT,ARGS...)(\
(COUNT<=8 __kprint_argsToFormats8(ARGS,__32zeroes) :\
COUNT<=16 __kprint_argsToFormats16(ARGS,__32zeroes) :\
__kprint_argsToFormats32(ARGS,__32zeroes)),\
(COUNT<=8 __kprint_argsToObjects8(ARGS,__32zeroes) :\
COUNT<=16 __kprint_argsToObjects16(ARGS) :\
__kprint_argsToObjects32(ARGS,__32zeroes))
Maybe __ksprint(uint8 n, kprint_format* formats, void** objects);
#define ksprint(ARGS...) __ksprint(count_args(ARGS), __kprint_argsToArrs(count_args(ARGS),ARGS))
Maybe __kfprint(FILE* fd, uint8 n, kprint_format* formats, void** objects);
#define kfprint(FD, ARGS...) __kfprint(FD, count_args(ARGS), __kprint_argsToArrs(count_args(ARGS),ARGS))
void __kprint(uint8 n, kprint_format* formats, void** objects);
#define kprint(ARGS...) __kprint(count_args(ARGS), __kprint_argsToArrs(count_args(ARGS),ARGS))
// can take (bgColor | fgColor)
void kprint_setColor(kprint_format f);
#if __cplusplus
}
#endif

View File

@ -0,0 +1,55 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
typedef enum kprint_fgColor{
// 10000000 00000000 00000000 00000000
// ^ ^^^^
// | color num
// fgColorChanged flag
kprint_fgBlack = 0x80000000,
kprint_fgDarkRed = 0x81000000,
kprint_fgDarkGreen = 0x82000000,
kprint_fgDarkYellow = 0x83000000,
kprint_fgDarkBlue = 0x84000000,
kprint_fgDarkMagneta= 0x85000000,
kprint_fgDarkCyan = 0x86000000,
kprint_fgGray = 0x87000000,
kprint_fgDarkGray = 0x88000000,
kprint_fgRed = 0x89000000,
kprint_fgGreen = 0x8a000000,
kprint_fgYellow = 0x8b000000,
kprint_fgBlue = 0x8c000000,
kprint_fgMagneta = 0x8d000000,
kprint_fgCyan = 0x8e000000,
kprint_fgWhite = 0x8f000000
} kprint_fgColor;
typedef enum kprint_bgColor{
// 01000000 00000000 00000000 00000000
// ^ ^^^^
// bgColorChanged flag color num
kprint_bgBlack = 0x40000000,
kprint_bgDarkRed = 0x40100000,
kprint_bgDarkGreen = 0x40200000,
kprint_bgDarkYellow = 0x40300000,
kprint_bgDarkBlue = 0x40400000,
kprint_bgDarkMagneta= 0x40500000,
kprint_bgDarkCyan = 0x40600000,
kprint_bgGray = 0x40700000,
kprint_bgDarkGray = 0x40800000,
kprint_bgRed = 0x40900000,
kprint_bgGreen = 0x40a00000,
kprint_bgYellow = 0x40b00000,
kprint_bgBlue = 0x40c00000,
kprint_bgMagneta = 0x40d00000,
kprint_bgCyan = 0x40e00000,
kprint_bgWhite = 0x40f00000
} kprint_bgColor;
#if __cplusplus
}
#endif

View File

@ -0,0 +1,47 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../type_system/ktId.h"
typedef enum kprint_dataFormat{
// 00000000 00000000 00000000 00000000
// ^^^^
// type
kprint_fmtInt = 0x00000000,
kprint_fmtUInt = 0x00010000,
kprint_fmtHex = 0x00020000,
kprint_fmtBin = 0x00030000,
kprint_fmtFloat = 0x00040000,
kprint_fmtChar = 0x00050000,
kprint_fmtString = 0x00060000,
// 00100000 00000000 00000000 00000000
// ^
// prefix/postfix flag
kprint_fmtWithPrefix=0x20000000,
kprint_fmtWithPostfix=kprint_fmtWithPrefix,
// 00010000 00000000 00000000 00000000
// ^
// uppercase flag
kprint_fmtUppercase=0x10000000
} kprint_dataFormat;
typedef struct {
unsigned char fgColorChanged : 1;
unsigned char bgColorChanged : 1;
unsigned char withPrefix : 1;
unsigned char uppercase : 1;
unsigned char fgColor : 4;
unsigned char bgColor : 4;
unsigned char dataFmt : 4;
ktId typeId;
} kprint_format;
#if __cplusplus
}
#endif

View File

@ -0,0 +1,72 @@
# kerep_format
```
00000000 00000000 00000000 00000000
fgColorSet┘│││└┼┴┘ └┼┴┘└┴┴┤ ktId
bgColorSet─┘││ │ bgColor └data format
prefix┬────┘│ └fgColor
postfix └uppercase
```
## Console colors
### *Foreground*
| kprint_fg | hex | bin |
|-----------|-----|-----|
| Black | 0x80000000 | 10000000 00000000... |
| DarkRed | 0x81000000 | 10000001 00000000... |
| DarkGreen | 0x82000000 | 10000010 00000000... |
| DarkYellow | 0x83000000 | 10000011 00000000... |
| DarkBlue | 0x84000000 | 10000100 00000000... |
| DarkMagneta | 0x85000000 | 10000101 00000000... |
| DarkCyan | 0x86000000 | 10000110 00000000... |
| Gray | 0x87000000 | 10000111 00000000... |
| DarkGray | 0x88000000 | 10001000 00000000... |
| Red | 0x89000000 | 10001001 00000000... |
| Green | 0x8a000000 | 10001010 00000000... |
| Yellow | 0x8b000000 | 10001011 00000000... |
| Blue | 0x8c000000 | 10001100 00000000... |
| Magneta | 0x8d000000 | 10001101 00000000... |
| Cyan | 0x8e000000 | 10001110 00000000... |
| White | 0x8f000000 | 10001111 00000000... |
### *Background*
| kprint_bg | hex | bin |
|-----------|-----|-----|
| Black | 0x40000000 | 01000000 00000000... |
| DarkRed | 0x40100000 | 01000000 00010000... |
| DarkGreen | 0x40200000 | 01000000 00100000... |
| DarkYellow | 0x40300000 | 01000000 00110000... |
| DarkBlue | 0x40400000 | 01000000 01000000... |
| DarkMagneta | 0x40500000 | 01000000 01010000... |
| DarkCyan | 0x40600000 | 01000000 01100000... |
| Gray | 0x40700000 | 01000000 01110000... |
| DarkGray | 0x40800000 | 01000000 10000000... |
| Red | 0x40900000 | 01000000 10010000... |
| Green | 0x40a00000 | 01000000 10100000... |
| Yellow | 0x40b00000 | 01000000 10110000... |
| Blue | 0x40c00000 | 01000000 11000000... |
| Magneta | 0x40d00000 | 01000000 11010000... |
| Cyan | 0x40e00000 | 01000000 11100000... |
| White | 0x40f00000 | 01000000 11110000... |
## Data format
| kprint_fmt | possible flags | data types | hex value | bin value |
|------------|----------------|------------|-----------|-----------|
| Int | | int8... int64 | 0x00000000 | 00000000 00000000... |
| UInt | WithPostfix, Uppercase | uint8... uint64 | 0x00010000 | 00000000 00000001... |
| Hex | WithPrefix, Uppercase | any | 0x00020000 | 00000000 00000010... |
| Bin | WithPrefix, | any | 0x00030000 | 00000000 00000011... |
| Float | WithPostfix, Uppercase | float32, float64 | 0x00040000 | 00000000 00000100... |
| Char | | char | 0x00050000 | 00000000 00000101... |
| String | | char* | 0x00060000 | 00000000 00000110... |
### *Flags*
| kprint_fmt | hex value | bin value |
|-------------|------------|-----------|
| withPrefix | 0x20000000 | 00100000 00000000... |
| withPostfix | 0x20000000 | 00100000 00000000... |
| upperase | 0x10000000 | 00010000 00000000... |

View File

@ -1,6 +1,6 @@
#pragma once
#include "types.h"
#include "std.h"
// executes codeblock and prints execution time
#ifdef CLOCK_REALTIME // non-standard high-precision clock

View File

@ -8,6 +8,7 @@ extern "C" {
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include <locale.h>
#include <time.h>
#include <setjmp.h>

View File

@ -0,0 +1,30 @@
# kerep type system
For using some kerep capabilities, such as generic structs, unitype, and kprint, types should be *registered*.
## type id
Every registered type has its own id (`ktId`), which should be declared in header file and defined in source file.
Example:
```c
//someStruct.h
typedef struct { } someStruct;
ktId_declare(ktId_someStruct);
ktId_declare(ktId_someStructPtr); // pointer to type is another type
```
```c
//someStruct.c
ktId_define(ktId_someStruct);
ktId_define(ktId_someStructPtr);
```
## type descriptors
Every registered type should have it's own descriptor (`ktDescriptor`). It's a struct, which contains some information about type and pointers to some specific functions for this type (`toString`, `freeMembers`).
## type registration
To finally register a type, you should call macro `kt_register()` between `ktDescriptors_beginInit()` and `ktDescriptors_endInit()`. Better do it at the start of your program. To register all types from kerep, call `ktDescriptors_initKerepTypes()`.
Examples:
+ [ktDescriptors_initKerepTypes()](src/base/type_system/init.c)
+ [kerep types registration](tests/main.cpp)

103
src/base/type_system/init.c Normal file
View 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);
}

View 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

View File

@ -0,0 +1,22 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../std.h"
#include "../errors.h"
#include "ktId.h"
typedef struct ktDescriptor{
char* name;
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
} ktDescriptor;
#if __cplusplus
}
#endif

View 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

View 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]));
}

View 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

View File

@ -0,0 +1,4 @@
#include "init.h"
#include "kt_functions.h"
#include "unitype.h"

View File

@ -1,12 +1,13 @@
#include "base.h"
#include "../base.h"
kerepTypeId_define(kerepTypeId_Unitype);
kerepTypeId_define(kerepTypeId_UnitypePtr);
ktId_define(ktId_Unitype);
ktId_define(ktId_UnitypePtr);
void Unitype_free(Unitype u){
kerepTypeDescriptor type=kerepTypeDescriptor_get(u.typeId);
if(type.free_members)
type.free_members(u.VoidPtr);
tryLast(ktDescriptor_get(u.typeId), mType);
ktDescriptor type=*(ktDescriptor*)mType.value.VoidPtr;
if(type.freeMembers)
type.freeMembers(u.VoidPtr);
if(u.allocatedInHeap)
free(u.VoidPtr);
}
@ -15,16 +16,17 @@ void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); }
#define BUFSIZE 64
char* sprintuni(Unitype v){
char* buf=malloc(BUFSIZE);
kerepTypeDescriptor type=kerepTypeDescriptor_get(v.typeId);
if(v.typeId==kerepTypeId_Null)
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);

View File

@ -4,7 +4,7 @@
extern "C" {
#endif
#include "types.h"
#include "ktId.h"
typedef struct Unitype{
union {
@ -15,27 +15,27 @@ 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 Uni(TYPE, VAL) (Unitype){\
.TYPE=VAL, .typeId=kerepTypeId_##TYPE, .allocatedInHeap=false}
#define __UniDef(TYPE, VAL) (Unitype){\
.TYPE=VAL, .typeId=ktId_##TYPE, .allocatedInHeap=false}
#define UniInt64(VAL) Uni(Int64, VAL)
#define UniUInt64(VAL) Uni(UInt64, VAL)
#define UniFloat64(VAL) Uni(Float64, VAL)
#define UniBool(VAL) Uni(Bool, VAL)
#define UniInt64(VAL) __UniDef(Int64, VAL)
#define UniUInt64(VAL) __UniDef(UInt64, VAL)
#define UniFloat64(VAL) __UniDef(Float64, VAL)
#define UniBool(VAL) __UniDef(Bool, VAL)
#define UniPtrStack(ID_VAR_NAME, VAL) (Unitype){\
#define UniStack(ID_VAR_NAME, VAL) (Unitype){\
.VoidPtr=VAL, .typeId=ID_VAR_NAME, .allocatedInHeap=false}
#define UniPtrHeap(ID_VAR_NAME, VAL) (Unitype){\
#define UniHeap(ID_VAR_NAME, VAL) (Unitype){\
.VoidPtr=VAL, .typeId=ID_VAR_NAME, .allocatedInHeap=true}
#define UniNull UniPtrStack(kerepTypeId_Null, NULL)
#define UniNull UniStack(ktId_Null, NULL)
#define UniTrue UniBool(true)
#define UniFalse UniBool(false)

View File

@ -1,71 +0,0 @@
#include "../Autoarr/Autoarr.h"
#include "unitype.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);
// 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 (*free_members)(void*)){
kerepTypeDescriptor typeDesc={
.name=name,
.size=size,
.free_members=free_members,
.id=++kerepTypeId_last
};
Autoarr_add(__kerepTypeDescriptors, typeDesc);
}
kerepTypeDescriptor kerepTypeDescriptor_get(kerepTypeId id){
return typeDescriptors[id];
}

View File

@ -1,64 +0,0 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "std.h"
typedef uint16 kerepTypeId;
typedef struct kerepTypeDescriptor{
void (*free_members)(void*); // NULL or function which frees all struct members
char* name;
kerepTypeId id;
uint16 size;
} 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 (*free_members)(void*));
#define kerepType_register(TYPE, ID_VAR_NAME, FREE_MEMBERS_FUNC)\
__kerepType_register(#ID_VAR_NAME, sizeof(TYPE), FREE_MEMBERS_FUNC);\
ID_VAR_NAME=kerepTypeId_last;
void kerepTypeDescriptors_beginInit();
void kerepTypeDescriptors_endInit();
kerepTypeDescriptor 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);
#if __cplusplus
}
#endif

View File

@ -13,9 +13,9 @@ void test_all(){
int main(){
setlocale(LC_ALL, "en-US.Unicode");
kerepTypeDescriptors_beginInit();
kerepTypeDescriptors_initKerepTypes();
kerepTypeDescriptors_endInit();
ktDescriptors_beginInit();
ktDescriptors_initKerepTypes();
ktDescriptors_endInit();
printf("\e[97mkerep tests are starting!\n");
optime("test_all",1,test_all());
printf("\e[0m\n");

View File

@ -18,7 +18,7 @@ void print_dtsod(Hashtable* dtsod){
printf("\e[92m");
Hashtable_foreach(dtsod, p,({
printkvp(p);
if(p.value.typeId==kerepTypeId_HashtablePtr){
if(p.value.typeId==ktId_HashtablePtr){
printf(": {\n");
Hashtable* sub=p.value.VoidPtr;
Hashtable_foreach(sub, _p,({

View File

@ -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){

View File

@ -4,7 +4,7 @@ EXPORT void CALL test_marshalling(char* text, KVPair** kptr){
KVPair* k=malloc(sizeof(KVPair));
k->key="message";
char* tc=cptr_copy(text);
Unitype u=UniPtrHeap(kerepTypeId_CharPtr,tc);
Unitype u=UniHeap(ktId_CharPtr,tc);
k->value=u;
*kptr=k;
}

View File

@ -1,7 +1,7 @@
#include "tests.h"
Maybe dont_throw(){
return SUCCESS(Uni(UInt64, 9/2));
return SUCCESS(UniUInt64(9/2));
}
Maybe throw_error(){

View File

@ -44,11 +44,11 @@ void test_searchtree(){
printuni(u);
ST_push(node,"channel_id", u);
printf(" -> channel_id\n ");
u=UniPtrHeap(kerepTypeId_CharPtr, cptr_copy("32.2004"));
u=UniHeap(ktId_CharPtr, cptr_copy("32.2004"));
printuni(u);
ST_push(node,"message_id", u);
printf(" -> message_id\n ");
u=UniPtrStack(kerepTypeId_CharPtr,"some text UwU");
u=UniStack(ktId_CharPtr,"some text UwU");
printuni(u);
ST_push(node,"text", u);
printf(" -> text\n");