a lot of changes

This commit is contained in:
timerix 2022-09-06 20:06:08 +06:00
parent d449901ab6
commit dc6a52608a
21 changed files with 294 additions and 217 deletions

View File

@ -182,7 +182,7 @@ Maybe __ParseValue(DeserializeSharedData* shared, string str){
// Float64 // Float64
case 'f': { case 'f': {
char* _c=string_extract(str); char* _c=string_extract(str);
Unitype rez=Uni(Float64,strtod(_c,NULL)); Unitype rez=UniFloat64(strtod(_c,NULL));
free(_c); free(_c);
return SUCCESS(rez); return SUCCESS(rez);
} }
@ -199,7 +199,7 @@ Maybe __ParseValue(DeserializeSharedData* shared, string str){
safethrow(err,free(_c)); safethrow(err,free(_c));
} }
free(_c); free(_c);
return SUCCESS(Uni(UInt64,lu)); return SUCCESS(UniUInt64(lu));
} }
// Int64 // Int64
case '0': case '1': case '2': case '3': case '4': case '0': case '1': case '2': case '3': case '4':
@ -215,7 +215,7 @@ Maybe __ParseValue(DeserializeSharedData* shared, string str){
safethrow(err,free(_c)); safethrow(err,free(_c));
} }
free(_c); free(_c);
return SUCCESS(Uni(Int64,li)); return SUCCESS(UniInt64(li));
} }
// wrong type // wrong type
default: default:

View File

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

View File

@ -4,7 +4,7 @@
extern "C" { extern "C" {
#endif #endif
#include "types.h" #include "std.h"
// returns length of char buffer (without \0) // returns length of char buffer (without \0)
uint32 cptr_length(char* str); uint32 cptr_length(char* str);

View File

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

View File

@ -1,2 +1,33 @@
## kprint # kprint
I don't really like printf function, so i made safer and more convinient replacement. 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 | 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: <span style="color:blue">Maybe:</span> <span style="color:lightgreen">{value={0, kerepTypeId_Null}}</span>

View File

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

View File

@ -1,112 +1,87 @@
#include "../base.h" /* #include "../base.h"
#include "../../String/StringBuilder.h" #include "../../String/StringBuilder.h"
Maybe __next_toString(uint16 size, va_list args, StringBuilder* strb){ Maybe __next_toString(kprint_format format, void* object){
kprint_format format=va_arg(args, kprint_format);
if(!format.dataFmtSet)
safethrow("format is not set", StringBuilder_free(strb));
// detecting type // detecting type
if(!format.typeId) switch(format.dataFmt){ if(!format.typeId) switch((kprint_dataFormat)((uint32)0 | format.dataFmt)){
case kprint_fmtInt: case kprint_fmtInt:
case kprint_fmtHex: case kprint_fmtHex:
case kprint_fmtBin: case kprint_fmtBin:
switch(size){ format.typeId=kerepTypeId_Int64; break;
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_fmtUInt: case kprint_fmtUInt:
switch(size){ format.typeId=kerepTypeId_UInt64; break;
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;
case kprint_fmtFloat: case kprint_fmtFloat:
switch(size){ format.typeId=kerepTypeId_Float64; break;
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;
case kprint_fmtChar: case kprint_fmtChar:
if(size!=1) format.typeId=kerepTypeId_Char; break;
safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb));
format.typeId=kerepTypeId_Char;
break;
case kprint_fmtString: case kprint_fmtString:
switch(size){ format.typeId=kerepTypeId_CharPtr; break;
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;
default: 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; kerepTypeDescriptor typeDesc=*(kerepTypeDescriptor*)mtd.value.VoidPtr;
if(size<typeDesc.size) if(!typeDesc.toString)
safethrow("sizeof(arg) < arg_typeDesc.size",StringBuilder_free(strb)); safethrow("type descriptor doesnt have toString() func",;);
if(!typeDesc.toString){ try(typeDesc.toString(object, &format), mStr,;);
return SUCCESS(mStr.value);
} }
else {
void* obj=va_arg(args, size);
// value is single variable
if(size==typeDesc.size){
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);
} }
// value is array char* rezult=StringBuilder_build(strb).ptr;
else{ return SUCCESS(UniHeap(kerepTypeId_CharPtr, rezult));
if(size%typeDesc.size!=0) }
safethrow("array size is not corresponding to type size",StringBuilder_free(strb));
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, kerepTypeId typeId, void* array){
try(kerepTypeDescriptor_get(format.typeId),mtd,;);
kerepTypeDescriptor typeDesc=*(kerepTypeDescriptor*)mtd.value.VoidPtr;
if(!typeDesc.toString)
safethrow("type descriptor doesnt have toString() func",;);
StringBuilder* strb=StringBuilder_create();
StringBuilder_append_char(strb, '['); StringBuilder_append_char(strb, '[');
uint16 count=size/typeDesc.size;
for (uint16 e=1; e<count; e++){ for (uint16 e=1; e<count; e++){
StringBuilder_append_char(strb, ' '); StringBuilder_append_char(strb, ' ');
try(typeDesc.toString(obj, &format), estr, StringBuilder_free(strb)); try(typeDesc.toString(array+typeDesc.size*e, &format), estr, StringBuilder_free(strb));
StringBuilder_append_cptr(strb, estr.value.VoidPtr); StringBuilder_append_cptr(strb, estr.value.VoidPtr);
StringBuilder_append_char(strb, ','); StringBuilder_append_char(strb, ',');
} }
StringBuilder_rmchar(strb); StringBuilder_rmchar(strb);
StringBuilder_append_char(strb, ' '); StringBuilder_append_char(strb, ' ');
StringBuilder_append_char(strb, ']'); StringBuilder_append_char(strb, ']');
} } */
}
}
Maybe __kvsprint(uint8 n, uint16* sizes, va_list args){
StringBuilder* strb=StringBuilder_create();
for(uint8 i=0; i<n; i++){
try(__next_toString(sizes[i], args, strb),_,;);
}
char* rezult=StringBuilder_build(strb).ptr;
return SUCCESS(UniHeap(kerepTypeId_CharPtr, rezult));
}
Maybe __ksprint(uint8 n, uint16 sizes[32], ...){
va_list args;
va_start(args, sizes);
try(__kvsprint(n, sizes, args),rezult,;);
va_end(args);
return rezult;
}
Maybe __kfprint(FILE* fd, uint8 n, uint16 sizes[32], ...){
va_list args;
va_start(args, sizes);
try(__kvsprint(n, sizes, args),maybeStr,;);
if(fputs(maybeStr.value.VoidPtr, fd)==EOF)
safethrow("can't write string to file",;);
free(maybeStr.value.VoidPtr);
va_end(args);
return MaybeNull;
}
void __kprint(uint8 n, uint16 sizes[32], ...){
}

View File

@ -70,6 +70,9 @@ Maybe __kfprint(FILE* fd, uint8 n, kprint_format* formats, void** objects);
void __kprint(uint8 n, kprint_format* formats, void** objects); void __kprint(uint8 n, kprint_format* formats, void** objects);
#define kprint(ARGS...) __kprint(count_args(ARGS), __kprint_argsToArrs(count_args(ARGS),ARGS)) #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 #if __cplusplus
} }
#endif #endif

View File

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

View File

@ -4,61 +4,41 @@
extern "C" { extern "C" {
#endif #endif
#include "../unitype.h" #include "../type_system/kerepTypeId.h"
typedef enum kprint_dataFormat{ typedef enum kprint_dataFormat{
// 00100000 00000000 // 00000000 00000000 00000000 00000000
// ^ ^^
// dataFmtSet flag type
kprint_fmtInt = 0x20000000, // default
// 00100000 00000001
kprint_fmtUInt = 0x20010000, // mandatory for uint
// 00100000 00000010
kprint_fmtHex = 0x20020000, // optional
// 00100000 00000011
kprint_fmtBin = 0x20030000, // optional
// 00110000 00000000
// ^^^^ // ^^^^
// dataFmtSet flag┘| type // type
// notInteger flag kprint_fmtInt = 0x00000000,
kprint_fmtFloat = 0x30000000, // mandatory for float kprint_fmtUInt = 0x00010000,
kprint_fmtHex = 0x00020000,
kprint_fmtBin = 0x00030000,
kprint_fmtFloat = 0x00040000,
kprint_fmtChar = 0x00050000,
kprint_fmtString = 0x00060000,
// 00110000 00000001 // 00100000 00000000 00000000 00000000
kprint_fmtChar = 0x30010000, // optional for char
// 00110000 00000010
kprint_fmtString = 0x30020000, // mandatory for char[], char* and string
// 00110000 00000011
//kprint_fmt? = 0x30030000, // unused
// 00000000 00000100
// ^ // ^
// prefix/postfix flag // prefix/postfix flag
kprint_fmtWithPrefix=0x00040000, // optional for hex or binary kprint_fmtWithPrefix=0x20000000,
kprint_fmtWithPostfix=kprint_fmtWithPrefix, // optional for uint or float kprint_fmtWithPostfix=kprint_fmtWithPrefix,
// 00000000 00001000 // 00010000 00000000 00000000 00000000
// ^ // ^
// uppercase flag // optional for hex, uint or float // uppercase flag
kprint_fmtUppercase=0x00080000 kprint_fmtUppercase=0x10000000
} kprint_dataFmt; } kprint_dataFormat;
typedef struct { typedef struct {
unsigned char fgColorChanged : 1; unsigned char fgColorChanged : 1;
unsigned char bgColorChanged : 1; unsigned char bgColorChanged : 1;
unsigned char dataFmtSet : 1; unsigned char withPrefix : 1;
unsigned char notInteger : 1; unsigned char uppercase : 1;
unsigned char fgColor : 4; unsigned char fgColor : 4;
unsigned char bgColor : 4; unsigned char bgColor : 4;
unsigned char uppercase : 1; unsigned char dataFmt : 4;
unsigned char withPrefix : 1;
unsigned char dataFmt : 2;
kerepTypeId typeId; kerepTypeId typeId;
} kprint_format; } kprint_format;

View File

@ -0,0 +1,72 @@
# kerep_format
```
00000000 00000000 00000000 00000000
fgColorSet┘│││└┼┴┘ └┼┴┘└┴┴┤ kerepTypeId
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 #pragma once
#include "types.h" #include "std.h"
// executes codeblock and prints execution time // executes codeblock and prints execution time
#ifdef CLOCK_REALTIME // non-standard high-precision clock #ifdef CLOCK_REALTIME // non-standard high-precision clock

View File

View File

@ -0,0 +1,22 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../std.h"
#include "../errors.h"
#include "kerepTypeId.h"
typedef struct kerepTypeDescriptor{
char* name;
kerepTypeId id;
uint16 size;
void (*freeMembers)(void*); // NULL or function which frees all struct members
///@return Maybe<char*>
Maybe (*toString)(void*, int32); // NULL or function which generates string representaion of object
} kerepTypeDescriptor;
#if __cplusplus
}
#endif

View File

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

View File

@ -1,5 +1,5 @@
#include "../Autoarr/Autoarr.h" #include "../../Autoarr/Autoarr.h"
#include "unitype.h" #include "types.h"
Autoarr_declare(kerepTypeDescriptor) Autoarr_declare(kerepTypeDescriptor)
Autoarr_define(kerepTypeDescriptor) Autoarr_define(kerepTypeDescriptor)
@ -59,7 +59,7 @@ void kerepTypeDescriptors_endInit(){
printf("\e[92minitialized %u type descriptors\n", kerepTypeId_last); 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={ kerepTypeDescriptor typeDesc={
.name=name, .name=name,
.size=size, .size=size,

View File

@ -4,26 +4,13 @@
extern "C" { extern "C" {
#endif #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<char*>
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; 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)\ #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);\ __kerepType_register(#ID_VAR_NAME, sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\
@ -35,6 +22,7 @@ void kerepTypeDescriptors_endInit();
/// @return Maybe<kerepTypeDescriptor*> /// @return Maybe<kerepTypeDescriptor*>
Maybe kerepTypeDescriptor_get(kerepTypeId id); Maybe kerepTypeDescriptor_get(kerepTypeId id);
kerepTypeId_declare(kerepTypeId_Null); kerepTypeId_declare(kerepTypeId_Null);
kerepTypeId_declare(kerepTypeId_Char); kerepTypeId_declare(kerepTypeId_Char);

View File

@ -1,10 +1,11 @@
#include "base.h" #include "../base.h"
kerepTypeId_define(kerepTypeId_Unitype); kerepTypeId_define(kerepTypeId_Unitype);
kerepTypeId_define(kerepTypeId_UnitypePtr); kerepTypeId_define(kerepTypeId_UnitypePtr);
void Unitype_free(Unitype u){ 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) if(type.freeMembers)
type.freeMembers(u.VoidPtr); type.freeMembers(u.VoidPtr);
if(u.allocatedInHeap) if(u.allocatedInHeap)
@ -15,7 +16,8 @@ void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); }
#define BUFSIZE 64 #define BUFSIZE 64
char* sprintuni(Unitype v){ char* sprintuni(Unitype v){
char* buf=malloc(BUFSIZE); 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) if(v.typeId==kerepTypeId_Null)
sprintf_s(buf, BUFSIZE, "{Null}"); sprintf_s(buf, BUFSIZE, "{Null}");
else if(v.typeId==kerepTypeId_Float64) else if(v.typeId==kerepTypeId_Float64)

View File

@ -4,7 +4,7 @@
extern "C" { extern "C" {
#endif #endif
#include "types.h" #include "kerepTypeId.h"
typedef struct Unitype{ typedef struct Unitype{
union { union {

View File

@ -47,7 +47,7 @@ char* genkey(uint32 i){
void fill(Hashtable* ht){ void fill(Hashtable* ht){
for(uint32 i=0;i<100000;i++) 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){ Unitype gett(Hashtable* ht){

View File

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