breaking changes in type system
This commit is contained in:
parent
305854e721
commit
95fec8d166
@ -12,6 +12,7 @@ Array_define(i32)
|
|||||||
Array_define(u32)
|
Array_define(u32)
|
||||||
Array_define(i64)
|
Array_define(i64)
|
||||||
Array_define(u64)
|
Array_define(u64)
|
||||||
|
Array_define(Pointer)
|
||||||
|
|
||||||
Array_define(Unitype)
|
Array_define(Unitype)
|
||||||
|
|
||||||
|
|||||||
@ -19,6 +19,7 @@ Array_declare(i32)
|
|||||||
Array_declare(u32)
|
Array_declare(u32)
|
||||||
Array_declare(i64)
|
Array_declare(i64)
|
||||||
Array_declare(u64)
|
Array_declare(u64)
|
||||||
|
Array_declare(Pointer)
|
||||||
|
|
||||||
Array_declare(Unitype)
|
Array_declare(Unitype)
|
||||||
|
|
||||||
|
|||||||
@ -7,13 +7,11 @@ extern "C" {
|
|||||||
#include "../base/base.h"
|
#include "../base/base.h"
|
||||||
|
|
||||||
#define Array_declare(type) \
|
#define Array_declare(type) \
|
||||||
typedef struct Array_##type{\
|
STRUCT(Array_##type, \
|
||||||
type* values; \
|
type* values; \
|
||||||
u32 length; \
|
u32 length; \
|
||||||
bool allocatedOnHeap; \
|
bool allocatedOnHeap; \
|
||||||
} Array_##type;\
|
) \
|
||||||
\
|
|
||||||
ktid_declare(Array_##type);\
|
|
||||||
\
|
\
|
||||||
static inline Array_##type Array_##type##_allocValues(u32 length){ \
|
static inline Array_##type Array_##type##_allocValues(u32 length){ \
|
||||||
return (Array_##type) { \
|
return (Array_##type) { \
|
||||||
|
|||||||
@ -7,7 +7,7 @@ extern "C" {
|
|||||||
#include "../base/base.h"
|
#include "../base/base.h"
|
||||||
|
|
||||||
#define Array_define(type) \
|
#define Array_define(type) \
|
||||||
ktid_define(Array_##type);
|
kt_define(Array_##type, (freeMembers_t)Array_##type##_free, NULL);
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,3 +12,4 @@ Autoarr_define(u32)
|
|||||||
Autoarr_define(i32)
|
Autoarr_define(i32)
|
||||||
Autoarr_define(u64)
|
Autoarr_define(u64)
|
||||||
Autoarr_define(i64)
|
Autoarr_define(i64)
|
||||||
|
Autoarr_define(Pointer)
|
||||||
|
|||||||
@ -20,7 +20,7 @@ Autoarr_declare(i32)
|
|||||||
Autoarr_declare(u32)
|
Autoarr_declare(u32)
|
||||||
Autoarr_declare(i64)
|
Autoarr_declare(i64)
|
||||||
Autoarr_declare(u64)
|
Autoarr_declare(u64)
|
||||||
|
Autoarr_declare(Pointer)
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,25 +10,23 @@ extern "C" {
|
|||||||
\
|
\
|
||||||
struct Autoarr_##type; \
|
struct Autoarr_##type; \
|
||||||
\
|
\
|
||||||
typedef struct {\
|
STRUCT(__functions_list_t_##type, \
|
||||||
void (*add)(struct Autoarr_##type* ar, type element); \
|
void (*add)(struct Autoarr_##type* ar, type element); \
|
||||||
type (*get)(struct Autoarr_##type* ar, u32 index); \
|
type (*get)(struct Autoarr_##type* ar, u32 index); \
|
||||||
type* (*getptr)(struct Autoarr_##type* ar, u32 index); \
|
type* (*getptr)(struct Autoarr_##type* ar, u32 index); \
|
||||||
void (*set)(struct Autoarr_##type* ar, u32 index, type element); \
|
void (*set)(struct Autoarr_##type* ar, u32 index, type element); \
|
||||||
void (*freear)(struct Autoarr_##type* ar, bool freePtr); \
|
void (*freear)(struct Autoarr_##type* ar, bool freePtr); \
|
||||||
type* (*toArray)(struct Autoarr_##type* ar); \
|
type* (*toArray)(struct Autoarr_##type* ar); \
|
||||||
} __functions_list_t_##type;\
|
) \
|
||||||
\
|
\
|
||||||
typedef struct Autoarr_##type{\
|
STRUCT(Autoarr_##type, \
|
||||||
u16 blocks_count; \
|
u16 blocks_count; \
|
||||||
u16 max_blocks_count; \
|
u16 max_blocks_count; \
|
||||||
u16 block_length; \
|
u16 block_length; \
|
||||||
u16 max_block_length; \
|
u16 max_block_length; \
|
||||||
type** values; \
|
type** values; \
|
||||||
__functions_list_t_##type* functions; \
|
__functions_list_t_##type* functions; \
|
||||||
} Autoarr_##type;\
|
) \
|
||||||
\
|
|
||||||
ktid_declare(Autoarr_##type);\
|
|
||||||
\
|
\
|
||||||
Autoarr_##type* __Autoarr_create_##type(u16 max_blocks_count, u16 max_block_length); \
|
Autoarr_##type* __Autoarr_create_##type(u16 max_blocks_count, u16 max_block_length); \
|
||||||
void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr); \
|
void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr); \
|
||||||
|
|||||||
@ -8,7 +8,7 @@ extern "C" {
|
|||||||
|
|
||||||
#define Autoarr_define(type) \
|
#define Autoarr_define(type) \
|
||||||
\
|
\
|
||||||
ktid_define(Autoarr_##type);\
|
kt_define(Autoarr_##type, ____Autoarr_free_##type, NULL); \
|
||||||
\
|
\
|
||||||
void __Autoarr_add_##type(Autoarr_##type* ar, type element){ \
|
void __Autoarr_add_##type(Autoarr_##type* ar, type element){ \
|
||||||
if(!ar->values){ \
|
if(!ar->values){ \
|
||||||
|
|||||||
@ -154,10 +154,10 @@ Maybe __ReadList(DeserializeSharedData* shared){
|
|||||||
Autoarr(Unitype)* list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
|
Autoarr(Unitype)* list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
|
||||||
bool readingList=true;
|
bool readingList=true;
|
||||||
while (true){
|
while (true){
|
||||||
try(ReadValue((&readingList)), val, Autoarr_free(list, true))
|
try(ReadValue((&readingList)), m_val, Autoarr_free(list, true))
|
||||||
Autoarr_add(list,val.value);
|
Autoarr_add(list,m_val.value);
|
||||||
if (!readingList){
|
if (!readingList){
|
||||||
if(val.value.typeId==ktid_Null)
|
if(Unitype_isUniNull(m_val.value))
|
||||||
Autoarr_pop(list);
|
Autoarr_pop(list);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -275,7 +275,7 @@ Maybe __ReadValue(DeserializeSharedData* shared, bool* readingList){
|
|||||||
case ';':
|
case ';':
|
||||||
case ',':
|
case ',':
|
||||||
if(valueStr.length!=0){
|
if(valueStr.length!=0){
|
||||||
if(value.typeId!=ktid_Null)
|
if(!Unitype_isUniNull(value))
|
||||||
safethrow_wrongchar(c,Unitype_free(value));
|
safethrow_wrongchar(c,Unitype_free(value));
|
||||||
try(ParseValue(valueStr),maybeParsed,;)
|
try(ParseValue(valueStr),maybeParsed,;)
|
||||||
value=maybeParsed.value;
|
value=maybeParsed.value;
|
||||||
|
|||||||
@ -2,10 +2,10 @@
|
|||||||
#include "../String/StringBuilder.h"
|
#include "../String/StringBuilder.h"
|
||||||
|
|
||||||
|
|
||||||
typedef struct SerializeSharedData{
|
STRUCT(SerializeSharedData,
|
||||||
StringBuilder* sh_builder;
|
StringBuilder* sh_builder;
|
||||||
u8 sh_tabs;
|
u8 sh_tabs;
|
||||||
} SerializeSharedData;
|
)
|
||||||
#define b shared->sh_builder
|
#define b shared->sh_builder
|
||||||
#define tabs shared->sh_tabs
|
#define tabs shared->sh_tabs
|
||||||
|
|
||||||
@ -46,7 +46,7 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){
|
|||||||
else if(u.typeId==ktid_name(bool)){
|
else if(u.typeId==ktid_name(bool)){
|
||||||
StringBuilder_append_cptr(b, u.Bool ? "true" : "false");
|
StringBuilder_append_cptr(b, u.Bool ? "true" : "false");
|
||||||
}
|
}
|
||||||
else if(u.typeId==ktid_Null){
|
else if(Unitype_isUniNull(u)){
|
||||||
safethrow("Null isn't supported in DtsodV24",;);
|
safethrow("Null isn't supported in DtsodV24",;);
|
||||||
}
|
}
|
||||||
else if(u.typeId==ktid_ptrName(Autoarr_Unitype)){
|
else if(u.typeId==ktid_ptrName(Autoarr_Unitype)){
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
#include "../String/StringBuilder.h"
|
#include "../String/StringBuilder.h"
|
||||||
#include "io_includes.h"
|
#include "io_includes.h"
|
||||||
|
|
||||||
ktid_define(File);
|
kt_define(File, (freeMembers_t)file_close, NULL)
|
||||||
|
|
||||||
bool file_exists(const char* path){
|
bool file_exists(const char* path){
|
||||||
if(path[0]=='.'){
|
if(path[0]=='.'){
|
||||||
|
|||||||
@ -9,14 +9,14 @@ extern "C" {
|
|||||||
#include "../String/string.h"
|
#include "../String/string.h"
|
||||||
|
|
||||||
typedef FILE File;
|
typedef FILE File;
|
||||||
ktid_declare(File);
|
kt_declare(File);
|
||||||
|
|
||||||
bool file_exists(const char* path);
|
bool file_exists(const char* path);
|
||||||
|
|
||||||
///@return Maybe<void>
|
///@return Maybe<void>
|
||||||
Maybe file_delete(const char* path, bool recursive);
|
Maybe file_delete(const char* path, bool recursive);
|
||||||
|
|
||||||
PACK_ENUM(FileOpenMode,
|
PACKED_ENUM(FileOpenMode,
|
||||||
// open a file for reading
|
// open a file for reading
|
||||||
FileOpenMode_Read=1,
|
FileOpenMode_Read=1,
|
||||||
// (re)create a file for writing
|
// (re)create a file for writing
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
#include "Hashtable.h"
|
#include "Hashtable.h"
|
||||||
|
|
||||||
ktid_define(Hashtable);
|
kt_define(Hashtable, __Hashtable_free, NULL);
|
||||||
|
|
||||||
// amount of rows
|
// amount of rows
|
||||||
static const u16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521};
|
static const u16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521};
|
||||||
@ -95,7 +95,7 @@ Unitype Hashtable_get(Hashtable* ht, char* key){
|
|||||||
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){
|
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){
|
||||||
Unitype u=Hashtable_get(ht,key);
|
Unitype u=Hashtable_get(ht,key);
|
||||||
*output=u;
|
*output=u;
|
||||||
return u.typeId!=ktid_Null;
|
return Unitype_isUniNull(u);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u){
|
void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u){
|
||||||
|
|||||||
@ -4,14 +4,14 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "../base/base.h"
|
||||||
#include "../HashFunctions/hash.h"
|
#include "../HashFunctions/hash.h"
|
||||||
#include "KeyValuePair.h"
|
#include "KeyValuePair.h"
|
||||||
|
|
||||||
typedef struct Hashtable{
|
STRUCT(Hashtable,
|
||||||
u8 hein; // height=HT_HEIGHTS[hein]
|
u8 hein; // height=HT_HEIGHTS[hein]
|
||||||
Autoarr(KVPair)** rows; // Autoarr[height]
|
Autoarr(KVPair)** rows; // Autoarr[height]
|
||||||
} Hashtable;
|
)
|
||||||
ktid_declare(Hashtable);
|
|
||||||
|
|
||||||
Hashtable* Hashtable_create();
|
Hashtable* Hashtable_create();
|
||||||
void Hashtable_free(Hashtable* ht);
|
void Hashtable_free(Hashtable* ht);
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
#include "KeyValuePair.h"
|
#include "KeyValuePair.h"
|
||||||
|
|
||||||
ktid_define(KVPair);
|
kt_define(KVPair, __KVPair_free, NULL);
|
||||||
|
|
||||||
Autoarr_define(KVPair)
|
Autoarr_define(KVPair)
|
||||||
|
|
||||||
|
|||||||
@ -7,11 +7,10 @@ extern "C" {
|
|||||||
#include "../base/base.h"
|
#include "../base/base.h"
|
||||||
#include "../Autoarr/Autoarr.h"
|
#include "../Autoarr/Autoarr.h"
|
||||||
|
|
||||||
typedef struct KVPair{
|
STRUCT(KVPair,
|
||||||
char* key;
|
char* key;
|
||||||
Unitype value;
|
Unitype value;
|
||||||
} KVPair;
|
)
|
||||||
ktid_declare(KVPair);
|
|
||||||
|
|
||||||
Autoarr_declare(KVPair)
|
Autoarr_declare(KVPair)
|
||||||
|
|
||||||
|
|||||||
@ -1,12 +1,11 @@
|
|||||||
#include "SearchTree.h"
|
#include "SearchTree.h"
|
||||||
|
|
||||||
ktid_define(STNode);
|
kt_define(STNode, __STNode_free, NULL);
|
||||||
|
|
||||||
STNode* STNode_create(){
|
STNode* STNode_create(){
|
||||||
STNode* node=malloc(sizeof(STNode));
|
STNode* node=malloc(sizeof(STNode));
|
||||||
node->branches=NULL;
|
node->branches=NULL;
|
||||||
node->value.typeId=ktid_Null;
|
node->value=UniNull;
|
||||||
node->value.UInt64=0;
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -7,11 +7,10 @@ extern "C" {
|
|||||||
#include "../base/base.h"
|
#include "../base/base.h"
|
||||||
#include "../String/string.h"
|
#include "../String/string.h"
|
||||||
|
|
||||||
typedef struct SearchTreeNode{
|
STRUCT(STNode,
|
||||||
struct SearchTreeNode**** branches; // *STNode[8][8][4]
|
struct STNode**** branches; // *STNode[8][8][4]
|
||||||
Unitype value;
|
Unitype value;
|
||||||
} STNode;
|
)
|
||||||
ktid_declare(STNode);
|
|
||||||
|
|
||||||
STNode* STNode_create();
|
STNode* STNode_create();
|
||||||
void STNode_free(STNode* node);
|
void STNode_free(STNode* node);
|
||||||
|
|||||||
@ -1,8 +1,6 @@
|
|||||||
#include "StringBuilder.h"
|
#include "StringBuilder.h"
|
||||||
|
|
||||||
Autoarr_define(string)
|
kt_define(StringBuilder, __StringBuilder_free, NULL);
|
||||||
|
|
||||||
ktid_define(StringBuilder);
|
|
||||||
|
|
||||||
#define BL_C 32
|
#define BL_C 32
|
||||||
#define BL_L 1024
|
#define BL_L 1024
|
||||||
|
|||||||
@ -7,13 +7,10 @@ extern "C" {
|
|||||||
#include "../Autoarr/Autoarr.h"
|
#include "../Autoarr/Autoarr.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
|
||||||
Autoarr_declare(string)
|
STRUCT(StringBuilder,
|
||||||
|
|
||||||
typedef struct StringBuilder{
|
|
||||||
Autoarr(string)* compl_bufs;
|
Autoarr(string)* compl_bufs;
|
||||||
Autoarr(i8)* curr_buf;
|
Autoarr(i8)* curr_buf;
|
||||||
} StringBuilder;
|
)
|
||||||
ktid_declare(StringBuilder);
|
|
||||||
|
|
||||||
StringBuilder* StringBuilder_create(void);
|
StringBuilder* StringBuilder_create(void);
|
||||||
void StringBuilder_free(StringBuilder* b);
|
void StringBuilder_free(StringBuilder* b);
|
||||||
|
|||||||
@ -1,7 +1,8 @@
|
|||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
|
||||||
ktid_define(string);
|
kt_define(string, NULL, NULL);
|
||||||
Array_define(string);
|
Array_define(string)
|
||||||
|
Autoarr_define(string)
|
||||||
|
|
||||||
// copies str content to new char pointer value (adding '\0' at the end)
|
// copies str content to new char pointer value (adding '\0' at the end)
|
||||||
char* string_extract(string str){
|
char* string_extract(string str){
|
||||||
|
|||||||
@ -6,15 +6,17 @@ extern "C" {
|
|||||||
|
|
||||||
#include "../base/base.h"
|
#include "../base/base.h"
|
||||||
#include "../Array/Array.h"
|
#include "../Array/Array.h"
|
||||||
|
#include "../Autoarr/Autoarr.h"
|
||||||
|
|
||||||
// my fixed length string struct
|
// my fixed length string struct
|
||||||
// doesn't store '\0' at the end
|
// doesn't store '\0' at the end
|
||||||
typedef struct string{
|
STRUCT(string,
|
||||||
char* ptr; // char pointer
|
char* ptr; // char pointer
|
||||||
u64 length; // amount of chars in ptr value
|
u64 length; // amount of chars in ptr value
|
||||||
} string;
|
)
|
||||||
ktid_declare(string);
|
|
||||||
Array_declare(string);
|
Array_declare(string)
|
||||||
|
Autoarr_declare(string)
|
||||||
|
|
||||||
static const string stringNull={NULL,0};
|
static const string stringNull={NULL,0};
|
||||||
|
|
||||||
|
|||||||
@ -5,12 +5,13 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "std.h"
|
#include "std.h"
|
||||||
|
#include "type_system/typedef_macros.h"
|
||||||
|
|
||||||
PACK_ENUM(Endian,
|
PACKED_ENUM(Endian,
|
||||||
UnknownEndian=0,
|
UnknownEndian=0,
|
||||||
LittleEndian=1,
|
LittleEndian=1,
|
||||||
BigEndian=2
|
BigEndian=2
|
||||||
);
|
)
|
||||||
|
|
||||||
Endian getEndian();
|
Endian getEndian();
|
||||||
|
|
||||||
|
|||||||
@ -5,9 +5,9 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "std.h"
|
#include "std.h"
|
||||||
#include "type_system/unitype.h"
|
#include "type_system/type_system.h"
|
||||||
|
|
||||||
PACK_ENUM(ErrorId,
|
PACKED_ENUM(ErrorId,
|
||||||
SUCCESS, // not an error
|
SUCCESS, // not an error
|
||||||
ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX,
|
ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX,
|
||||||
ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR,
|
ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR,
|
||||||
@ -20,10 +20,10 @@ char* errname(ErrorId err);
|
|||||||
char* __genErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname);
|
char* __genErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname);
|
||||||
char* __extendErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname);
|
char* __extendErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname);
|
||||||
|
|
||||||
typedef struct Maybe{
|
STRUCT(Maybe,
|
||||||
Unitype value;
|
Unitype value;
|
||||||
char* errmsg;
|
char* errmsg;
|
||||||
} Maybe;
|
)
|
||||||
|
|
||||||
// return it if func doesn't return anything
|
// return it if func doesn't return anything
|
||||||
// .value .errmsg
|
// .value .errmsg
|
||||||
|
|||||||
@ -22,6 +22,7 @@ typedef int64_t i64;
|
|||||||
typedef uint64_t u64;
|
typedef uint64_t u64;
|
||||||
typedef float f32;
|
typedef float f32;
|
||||||
typedef double f64;
|
typedef double f64;
|
||||||
|
typedef void* Pointer;
|
||||||
|
|
||||||
// Usually bool from stdbool.h is defined as macro,
|
// Usually bool from stdbool.h is defined as macro,
|
||||||
// so in other macros like ktid_##TYPE it will be replaced by _Bool.
|
// so in other macros like ktid_##TYPE it will be replaced by _Bool.
|
||||||
@ -119,10 +120,6 @@ You can even embed it into macro in header (see kprint.h)
|
|||||||
CODE; \
|
CODE; \
|
||||||
PRAGMA_WARNING_POP
|
PRAGMA_WARNING_POP
|
||||||
|
|
||||||
#define PACK_ENUM(ENUM_NAME, ENUM_MEMBERS...) typedef enum ENUM_NAME {\
|
|
||||||
ENUM_MEMBERS\
|
|
||||||
} __attribute__((__packed__)) ENUM_NAME;
|
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -4,17 +4,21 @@ For using some kerep capabilities, such as generic structs, unitype, and kprint,
|
|||||||
|
|
||||||
## type id
|
## type id
|
||||||
|
|
||||||
Every registered type has its own id (`ktid`), which should be declared in header file and defined in source file.
|
Every registered type has its own `ktDescriptor` and `ktid` is an index of the descriptor in descriptors array.
|
||||||
Example:
|
Descriptor should be declared in header file.
|
||||||
|
Following macro declares `typedef struct` and `ktDescriptor`
|
||||||
```c
|
```c
|
||||||
//someStruct.h
|
//someStruct.h
|
||||||
typedef struct { } someStruct;
|
STRUCT(someStruct,
|
||||||
ktid_declare(someStruct);
|
i32 i; i32 j; i32 k;
|
||||||
|
);
|
||||||
```
|
```
|
||||||
|
then you need to define descriptor in a source file
|
||||||
```c
|
```c
|
||||||
//someStruct.c
|
//someStruct.c
|
||||||
ktid_define(someStruct);
|
kt_define(someStruct);
|
||||||
```
|
```
|
||||||
|
and register it.
|
||||||
|
|
||||||
## type descriptors
|
## type descriptors
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,13 @@
|
|||||||
#include "../base.h"
|
#include "../base.h"
|
||||||
#include "../../kprint/kprint_format.h"
|
#include "../../kprint/kprint_format.h"
|
||||||
|
|
||||||
|
|
||||||
|
// accepts char* (ptr to char) and char** (ptr to string)
|
||||||
char* __toString_char(void* c, u32 fmt) {
|
char* __toString_char(void* c, u32 fmt) {
|
||||||
|
// *c=char*
|
||||||
|
if(kp_fmt_dataFormat(fmt)==kp_s){
|
||||||
|
return cptr_copy(*(char**)c); // to avoid segmentation fault on free() when *c allocalet on stack
|
||||||
|
}
|
||||||
// *c=char
|
// *c=char
|
||||||
if(kp_fmt_dataFormat(fmt)==kp_c){
|
if(kp_fmt_dataFormat(fmt)==kp_c){
|
||||||
char* cc=malloc(2);
|
char* cc=malloc(2);
|
||||||
@ -10,10 +16,6 @@ char* __toString_char(void* c, u32 fmt) {
|
|||||||
cc[1]=0;
|
cc[1]=0;
|
||||||
return cc;
|
return cc;
|
||||||
}
|
}
|
||||||
// *c=cstring
|
|
||||||
else if(kp_fmt_dataFormat(fmt)==kp_s){
|
|
||||||
return cptr_copy(*(char**)c);
|
|
||||||
}
|
|
||||||
else throw(ERR_FORMAT);
|
else throw(ERR_FORMAT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,7 +203,19 @@ __toString_i32_def(64)
|
|||||||
__toString_u_def(8)
|
__toString_u_def(8)
|
||||||
__toString_u_def(16)
|
__toString_u_def(16)
|
||||||
__toString_u_def(32)
|
__toString_u_def(32)
|
||||||
__toString_u_def(64)
|
// __toString_u_def(64)
|
||||||
|
char* __toString_u64(void* _n, u32 f){
|
||||||
|
switch(kp_fmt_dataFormat(f)){
|
||||||
|
case kp_u: ;
|
||||||
|
u64 n=*(u64*)_n;
|
||||||
|
return toString_u64(n, kp_fmt_withPostfix(f), kp_fmt_isUpper(f));
|
||||||
|
case kp_b:
|
||||||
|
return toString_bin(_n, 64/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f));
|
||||||
|
case kp_h:
|
||||||
|
return toString_hex(_n, 64/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f));
|
||||||
|
default:
|
||||||
|
kprintf("\n%u\n", kp_fmt_dataFormat(f)); throw(ERR_FORMAT); return NULL; }
|
||||||
|
}
|
||||||
|
|
||||||
#define __toString_float_def(BITS) char* __toString_f##BITS(void* _n, u32 f){ \
|
#define __toString_float_def(BITS) char* __toString_f##BITS(void* _n, u32 f){ \
|
||||||
switch(kp_fmt_dataFormat(f)){ \
|
switch(kp_fmt_dataFormat(f)){ \
|
||||||
|
|||||||
@ -6,8 +6,8 @@ extern "C" {
|
|||||||
|
|
||||||
#include "../errors.h"
|
#include "../errors.h"
|
||||||
|
|
||||||
// char and cstring
|
// accepts char* (ptr to char) and char** (ptr to string)
|
||||||
// has different output for fmtChar and fmtString
|
// uses format kp_s and kp_c to determine what type is <c> argument
|
||||||
char* __toString_char(void* c, u32 fmt);
|
char* __toString_char(void* c, u32 fmt);
|
||||||
|
|
||||||
// bool
|
// bool
|
||||||
|
|||||||
@ -4,85 +4,92 @@
|
|||||||
#include "../../SearchTree/SearchTree.h"
|
#include "../../SearchTree/SearchTree.h"
|
||||||
#include "../../Hashtable/Hashtable.h"
|
#include "../../Hashtable/Hashtable.h"
|
||||||
#include "../../String/StringBuilder.h"
|
#include "../../String/StringBuilder.h"
|
||||||
|
#include "../../Filesystem/filesystem.h"
|
||||||
#include "base_toString.h"
|
#include "base_toString.h"
|
||||||
|
|
||||||
void ktDescriptors_initKerepTypes(){
|
void ktDescriptors_initKerepTypes(){
|
||||||
// null
|
|
||||||
__kt_register("Null", sizeof(NULL), NULL, NULL);
|
|
||||||
ktid_Null=ktid_last;
|
|
||||||
// base types
|
// base types
|
||||||
kt_register(char, NULL, __toString_char);
|
kt_register(Pointer);
|
||||||
kt_register(bool, NULL, __toString_bool);
|
if(ktid_Pointer!=0) // this can break UnitypeNull
|
||||||
kt_register(f32, NULL, __toString_f32);
|
throw("ktid_Pointer!=0, you must init kerep types before any other types");
|
||||||
kt_register(f64, NULL, __toString_f64);
|
|
||||||
kt_register(i8, NULL, __toString_i8);
|
kt_register(char);
|
||||||
kt_register(u8, NULL, __toString_u8);
|
kt_register(bool);
|
||||||
kt_register(i16, NULL, __toString_i16);
|
kt_register(f32);
|
||||||
kt_register(u16, NULL, __toString_u16);
|
kt_register(f64);
|
||||||
kt_register(i32, NULL, __toString_i32);
|
kt_register(i8);
|
||||||
kt_register(u32, NULL, __toString_u32);
|
kt_register(u8);
|
||||||
kt_register(i64, NULL, __toString_i64);
|
kt_register(i16);
|
||||||
kt_register(u64, NULL, __toString_u64);
|
kt_register(u16);
|
||||||
|
kt_register(i32);
|
||||||
|
kt_register(u32);
|
||||||
|
kt_register(i64);
|
||||||
|
kt_register(u64);
|
||||||
|
|
||||||
// ktDescriptor
|
// ktDescriptor
|
||||||
kt_register(ktDescriptor, NULL, NULL);
|
kt_register(ktDescriptor);
|
||||||
|
|
||||||
|
|
||||||
// base type arrays
|
// base type arrays
|
||||||
kt_register(Array_char, (freeMembers_t)Array_char_free, NULL);
|
kt_register(Array_char);
|
||||||
kt_register(Array_bool, (freeMembers_t)Array_bool_free, NULL);
|
kt_register(Array_bool);
|
||||||
kt_register(Array_f32, (freeMembers_t)Array_f32_free, NULL);
|
kt_register(Array_f32);
|
||||||
kt_register(Array_f64, (freeMembers_t)Array_f64_free, NULL);
|
kt_register(Array_f64);
|
||||||
kt_register(Array_i8, (freeMembers_t)Array_i8_free, NULL);
|
kt_register(Array_i8);
|
||||||
kt_register(Array_u8, (freeMembers_t)Array_u8_free, NULL);
|
kt_register(Array_u8);
|
||||||
kt_register(Array_i16, (freeMembers_t)Array_i16_free, NULL);
|
kt_register(Array_i16);
|
||||||
kt_register(Array_u16, (freeMembers_t)Array_u16_free, NULL);
|
kt_register(Array_u16);
|
||||||
kt_register(Array_i32, (freeMembers_t)Array_i32_free, NULL);
|
kt_register(Array_i32);
|
||||||
kt_register(Array_u32, (freeMembers_t)Array_u32_free, NULL);
|
kt_register(Array_u32);
|
||||||
kt_register(Array_i64, (freeMembers_t)Array_i64_free, NULL);
|
kt_register(Array_i64);
|
||||||
kt_register(Array_u64, (freeMembers_t)Array_u64_free, NULL);
|
kt_register(Array_u64);
|
||||||
|
kt_register(Array_Pointer);
|
||||||
|
|
||||||
// base type autoarrs
|
// base type autoarrs
|
||||||
kt_register(Autoarr_char, ____Autoarr_free_char, NULL);
|
kt_register(Autoarr_char);
|
||||||
kt_register(Autoarr_bool, ____Autoarr_free_bool, NULL);
|
kt_register(Autoarr_bool);
|
||||||
kt_register(Autoarr_f32, ____Autoarr_free_f32, NULL);
|
kt_register(Autoarr_f32);
|
||||||
kt_register(Autoarr_f64, ____Autoarr_free_f64, NULL);
|
kt_register(Autoarr_f64);
|
||||||
kt_register(Autoarr_i8, ____Autoarr_free_i8, NULL);
|
kt_register(Autoarr_i8);
|
||||||
kt_register(Autoarr_u8, ____Autoarr_free_u8, NULL);
|
kt_register(Autoarr_u8);
|
||||||
kt_register(Autoarr_i16, ____Autoarr_free_i16, NULL);
|
kt_register(Autoarr_i16);
|
||||||
kt_register(Autoarr_u16, ____Autoarr_free_u16, NULL);
|
kt_register(Autoarr_u16);
|
||||||
kt_register(Autoarr_i32, ____Autoarr_free_i32, NULL);
|
kt_register(Autoarr_i32);
|
||||||
kt_register(Autoarr_u32, ____Autoarr_free_u32, NULL);
|
kt_register(Autoarr_u32);
|
||||||
kt_register(Autoarr_i64, ____Autoarr_free_i64, NULL);
|
kt_register(Autoarr_i64);
|
||||||
kt_register(Autoarr_u64, ____Autoarr_free_u64, NULL);
|
kt_register(Autoarr_u64);
|
||||||
|
kt_register(Autoarr_Pointer);
|
||||||
|
|
||||||
// Unitype
|
// Unitype
|
||||||
kt_register(Unitype, __UnitypePtr_free, NULL);
|
kt_register(Unitype);
|
||||||
kt_register(Array_Unitype, __Array_Unitype_free_, NULL);
|
kt_register(Array_Unitype);
|
||||||
kt_register(Autoarr_Unitype, ____Autoarr_free_Unitype_, NULL);
|
kt_register(Autoarr_Unitype);
|
||||||
// replacing autogenerated freear() function to custom
|
// replacing autogenerated freear() function to custom
|
||||||
Autoarr_Unitype* _uar=Autoarr_create(Unitype, 1, 1);
|
Autoarr_Unitype* _uar=Autoarr_create(Unitype, 1, 1);
|
||||||
_uar->functions->freear=__Autoarr_free_Unitype_;
|
_uar->functions->freear=__Autoarr_free_Unitype_;
|
||||||
Autoarr_free(_uar, true);
|
Autoarr_free(_uar, true);
|
||||||
|
|
||||||
// SearchTreeNode
|
// STNode
|
||||||
kt_register(STNode, __STNode_free, NULL);
|
kt_register(STNode);
|
||||||
|
|
||||||
// KeyValuePair
|
// KeyValuePair
|
||||||
kt_register(KVPair, __KVPair_free, NULL);
|
kt_register(KVPair);
|
||||||
kt_register(Autoarr_KVPair, ____Autoarr_free_KVPair_, NULL);
|
kt_register(Autoarr_KVPair);
|
||||||
// replacing autogenerated freear() function to custom
|
// replacing autogenerated freear() function to custom
|
||||||
Autoarr_KVPair* _kvpar=Autoarr_create(KVPair, 1, 1);
|
Autoarr_KVPair* _kvpar=Autoarr_create(KVPair, 1, 1);
|
||||||
_kvpar->functions->freear=__Autoarr_free_KVPair_;
|
_kvpar->functions->freear=__Autoarr_free_KVPair_;
|
||||||
Autoarr_free(_kvpar, true);
|
Autoarr_free(_kvpar, true);
|
||||||
|
|
||||||
// Hashtable
|
// Hashtable
|
||||||
kt_register(Hashtable, __Hashtable_free, NULL);
|
kt_register(Hashtable);
|
||||||
|
|
||||||
// string
|
// string
|
||||||
kt_register(string, NULL, NULL);
|
kt_register(string);
|
||||||
kt_register(Array_string, (freeMembers_t)Array_string_free, NULL);
|
kt_register(Array_string);
|
||||||
kt_register(Autoarr_string, ____Autoarr_free_string, NULL);
|
kt_register(Autoarr_string);
|
||||||
|
|
||||||
// StringBuilder
|
// StringBuilder
|
||||||
kt_register(StringBuilder, __StringBuilder_free, NULL);
|
kt_register(StringBuilder);
|
||||||
|
|
||||||
|
//File
|
||||||
|
kt_register(File);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,16 +6,40 @@ extern "C" {
|
|||||||
|
|
||||||
#include "../std.h"
|
#include "../std.h"
|
||||||
#include "ktid.h"
|
#include "ktid.h"
|
||||||
|
#include "typedef_macros.h"
|
||||||
|
|
||||||
|
#define kt_declare(TYPE)\
|
||||||
|
ktid_declare(TYPE);\
|
||||||
|
extern ktDescriptor ktDescriptor_##TYPE; \
|
||||||
|
extern ktDescriptor ktDescriptor_##TYPE##_Ptr;
|
||||||
|
|
||||||
|
#define kt_define(TYPE, FREE_FUNC, TOSTRING_FUNC)\
|
||||||
|
ktid_define(TYPE); \
|
||||||
|
ktDescriptor ktDescriptor_##TYPE={ \
|
||||||
|
.name=#TYPE, \
|
||||||
|
.id=ktid_undefined, \
|
||||||
|
.size=sizeof(TYPE), \
|
||||||
|
.freeMembers=FREE_FUNC, \
|
||||||
|
.toString=TOSTRING_FUNC \
|
||||||
|
}; \
|
||||||
|
ktDescriptor ktDescriptor_##TYPE##_Ptr={\
|
||||||
|
.name=#TYPE "_Ptr", \
|
||||||
|
.id=ktid_undefined, \
|
||||||
|
.size=sizeof(TYPE), \
|
||||||
|
.freeMembers=FREE_FUNC, \
|
||||||
|
.toString=TOSTRING_FUNC \
|
||||||
|
};
|
||||||
|
|
||||||
typedef void (*freeMembers_t)(void*);
|
typedef void (*freeMembers_t)(void*);
|
||||||
typedef char* (*toString_t)(void* obj, u32 fmt);
|
typedef char* (*toString_t)(void* obj, u32 fmt);
|
||||||
typedef struct ktDescriptor{
|
|
||||||
|
STRUCT(ktDescriptor,
|
||||||
char* name;
|
char* name;
|
||||||
ktid id;
|
ktid id;
|
||||||
u16 size;
|
u16 size;
|
||||||
freeMembers_t freeMembers; // NULL or function which frees all struct members
|
freeMembers_t freeMembers; // NULL or function which frees all struct members
|
||||||
toString_t toString; // NULL or function which generates string representaion of object
|
toString_t toString; // NULL or function which generates string representaion of object
|
||||||
} ktDescriptor;
|
)
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,62 +1,57 @@
|
|||||||
#include "../../Autoarr/Autoarr.h"
|
#include "../../Autoarr/Autoarr.h"
|
||||||
|
#include "type_system.h"
|
||||||
|
#include "base_toString.h"
|
||||||
|
|
||||||
Autoarr_declare(ktDescriptor)
|
kt_define(Pointer, free, __toString_u64);
|
||||||
Autoarr_define(ktDescriptor)
|
kt_define(char,NULL, __toString_char);
|
||||||
|
kt_define(bool,NULL, __toString_bool);
|
||||||
|
kt_define(f32, NULL, __toString_f32);
|
||||||
|
kt_define(f64, NULL, __toString_f64);
|
||||||
|
kt_define(i8, NULL, __toString_i8);
|
||||||
|
kt_define(u8, NULL, __toString_u8);
|
||||||
|
kt_define(i16, NULL, __toString_i16);
|
||||||
|
kt_define(u16, NULL, __toString_u16);
|
||||||
|
kt_define(i32, NULL, __toString_i32);
|
||||||
|
kt_define(u32, NULL, __toString_u32);
|
||||||
|
kt_define(i64, NULL, __toString_i64);
|
||||||
|
kt_define(u64, NULL, __toString_u64);
|
||||||
|
|
||||||
ktid ktid_Null=-1;
|
kt_define(ktDescriptor, NULL, NULL);
|
||||||
|
|
||||||
ktid_define(char);
|
typedef ktDescriptor* ktDescriptor_Ptr;
|
||||||
ktid_define(bool);
|
|
||||||
ktid_define(f32);
|
|
||||||
ktid_define(f64);
|
|
||||||
ktid_define(i8);
|
|
||||||
ktid_define(u8);
|
|
||||||
ktid_define(i16);
|
|
||||||
ktid_define(u16);
|
|
||||||
ktid_define(i32);
|
|
||||||
ktid_define(u32);
|
|
||||||
ktid_define(i64);
|
|
||||||
ktid_define(u64);
|
|
||||||
|
|
||||||
ktid_define(ktDescriptor);
|
|
||||||
|
|
||||||
// type descriptors are stored here during initialization
|
// type descriptors are stored here during initialization
|
||||||
Autoarr(ktDescriptor)* __ktDescriptors=NULL;
|
Autoarr(Pointer)* __descriptorPointers=NULL;
|
||||||
// here type descriptors are stored when initialization is complited
|
// here type descriptors are stored when initialization is complited
|
||||||
ktDescriptor* typeDescriptors=NULL;
|
ktDescriptor** typeDescriptors=NULL;
|
||||||
ktid ktid_last=-1;
|
ktid ktid_last=-1;
|
||||||
|
|
||||||
typedef enum{
|
ENUM(ktDescriptorsState,
|
||||||
NotInitialized, Initializing, Initialized
|
NotInitialized, Initializing, Initialized
|
||||||
} ktDescriptorsState;
|
)
|
||||||
ktDescriptorsState initState=NotInitialized;
|
ktDescriptorsState initState=NotInitialized;
|
||||||
|
|
||||||
void ktDescriptors_beginInit(){
|
void ktDescriptors_beginInit(){
|
||||||
kprintf("\e[94mtype descriptors initializing...\n");
|
kprintf("\e[94mtype descriptors initializing...\n");
|
||||||
__ktDescriptors=Autoarr_create(ktDescriptor, 256, 256);
|
__descriptorPointers=Autoarr_create(Pointer, 256, 256);
|
||||||
if(__ktDescriptors==NULL) throw(ERR_NULLPTR);
|
if(__descriptorPointers==NULL)
|
||||||
|
throw(ERR_NULLPTR);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ktDescriptors_endInit(){
|
void ktDescriptors_endInit(){
|
||||||
typeDescriptors=Autoarr_toArray(__ktDescriptors);
|
typeDescriptors=(ktDescriptor**)Autoarr_toArray(__descriptorPointers);
|
||||||
Autoarr_free(__ktDescriptors,true);
|
Autoarr_free(__descriptorPointers,true);
|
||||||
if(typeDescriptors==NULL) throw(ERR_NULLPTR);
|
if(typeDescriptors==NULL) throw(ERR_NULLPTR);
|
||||||
kprintf("\e[92minitialized %u type descriptors\n", ktid_last);
|
kprintf("\e[92minitialized %u type descriptors\n", ktid_last);
|
||||||
}
|
}
|
||||||
|
|
||||||
void __kt_register(char* name, i16 size, void (*freeMembers)(void*), char* (*toString)(void*, u32)){
|
void __kt_register(ktDescriptor* descriptor){
|
||||||
ktDescriptor typeDesc={
|
descriptor->id=++ktid_last;
|
||||||
.name=name,
|
Autoarr_add(__descriptorPointers, descriptor);
|
||||||
.size=size,
|
|
||||||
.id=++ktid_last,
|
|
||||||
.freeMembers=freeMembers,
|
|
||||||
.toString=toString
|
|
||||||
};
|
|
||||||
Autoarr_add(__ktDescriptors, typeDesc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ktDescriptor ktDescriptor_get(ktid id){
|
ktDescriptor* ktDescriptor_get(ktid id){
|
||||||
if(id>ktid_last) {
|
if(id>ktid_last || id==ktid_undefined) {
|
||||||
kprintf("\ntype id: %u\n",id);
|
kprintf("\ntype id: %u\n",id);
|
||||||
throw("invalid type id");
|
throw("invalid type id");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,39 +9,38 @@ extern "C" {
|
|||||||
#include "ktDescriptor.h"
|
#include "ktDescriptor.h"
|
||||||
|
|
||||||
extern ktid ktid_last;
|
extern ktid ktid_last;
|
||||||
void __kt_register(char* name, i16 size, void (*freeMembers)(void*), char* (*toString)(void*, u32));
|
void __kt_register(ktDescriptor* descriptor);
|
||||||
|
|
||||||
#define kt_register(TYPE, FREE_MEMBERS_FUNC, TO_STRING_FUNC)\
|
#define kt_register(TYPE) \
|
||||||
__kt_register(#TYPE, sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\
|
__kt_register(&ktDescriptor_##TYPE); \
|
||||||
ktid_##TYPE=ktid_last; \
|
ktid_##TYPE=ktid_last; \
|
||||||
__kt_register(#TYPE "*", sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\
|
__kt_register(&ktDescriptor_##TYPE##_Ptr); \
|
||||||
ktid_##TYPE##_Ptr=ktid_last;
|
ktid_##TYPE##_Ptr=ktid_last;
|
||||||
|
|
||||||
void ktDescriptors_beginInit();
|
void ktDescriptors_beginInit();
|
||||||
void ktDescriptors_endInit();
|
void ktDescriptors_endInit();
|
||||||
|
|
||||||
/// @param id id of registered type
|
/// @param id id of registered type
|
||||||
ktDescriptor ktDescriptor_get(ktid id);
|
ktDescriptor* ktDescriptor_get(ktid id);
|
||||||
|
|
||||||
// call it to free heap-allocated ktDescriptors array
|
// call it to free heap-allocated ktDescriptors array
|
||||||
void ktDescriptors_free();
|
void ktDescriptors_free();
|
||||||
|
|
||||||
extern ktid ktid_Null;
|
kt_declare(Pointer);
|
||||||
|
kt_declare(char);
|
||||||
|
kt_declare(bool);
|
||||||
|
kt_declare(f32);
|
||||||
|
kt_declare(f64);
|
||||||
|
kt_declare(i8);
|
||||||
|
kt_declare(u8);
|
||||||
|
kt_declare(i16);
|
||||||
|
kt_declare(u16);
|
||||||
|
kt_declare(i32);
|
||||||
|
kt_declare(u32);
|
||||||
|
kt_declare(i64);
|
||||||
|
kt_declare(u64);
|
||||||
|
|
||||||
ktid_declare(char);
|
kt_declare(ktDescriptor);
|
||||||
ktid_declare(bool);
|
|
||||||
ktid_declare(f32);
|
|
||||||
ktid_declare(f64);
|
|
||||||
ktid_declare(i8);
|
|
||||||
ktid_declare(u8);
|
|
||||||
ktid_declare(i16);
|
|
||||||
ktid_declare(u16);
|
|
||||||
ktid_declare(i32);
|
|
||||||
ktid_declare(u32);
|
|
||||||
ktid_declare(i64);
|
|
||||||
ktid_declare(u64);
|
|
||||||
|
|
||||||
ktid_declare(ktDescriptor);
|
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,10 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "../std.h"
|
#include "../std.h"
|
||||||
|
#include "typedef_macros.h"
|
||||||
|
|
||||||
typedef u16 ktid;
|
typedef u16 ktid;
|
||||||
|
static const ktid ktid_undefined=-1;
|
||||||
|
|
||||||
#define ktid_name(TYPE) ktid_##TYPE
|
#define ktid_name(TYPE) ktid_##TYPE
|
||||||
#define ktid_ptrName(TYPE) ktid_##TYPE##_Ptr
|
#define ktid_ptrName(TYPE) ktid_##TYPE##_Ptr
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
#include "init.h"
|
#include "init.h"
|
||||||
#include "ktid.h"
|
#include "ktid.h"
|
||||||
#include "ktDescriptor.h"
|
#include "ktDescriptor.h"
|
||||||
#include "kt_functions.h"
|
#include "kt_functions.h"
|
||||||
#include "unitype.h"
|
#include "unitype.h"
|
||||||
|
#include "typedef_macros.h"
|
||||||
|
|||||||
14
src/base/type_system/typedef_macros.h
Normal file
14
src/base/type_system/typedef_macros.h
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define ENUM(ENUM_NAME, ENUM_MEMBERS...) typedef enum ENUM_NAME { \
|
||||||
|
ENUM_MEMBERS \
|
||||||
|
} ENUM_NAME;
|
||||||
|
|
||||||
|
#define PACKED_ENUM(ENUM_NAME, ENUM_MEMBERS...) typedef enum ENUM_NAME { \
|
||||||
|
ENUM_MEMBERS \
|
||||||
|
} __attribute__((__packed__)) ENUM_NAME;
|
||||||
|
|
||||||
|
#define STRUCT(STRUCT_NAME, STRUCT_MEMBERS...) typedef struct STRUCT_NAME { \
|
||||||
|
STRUCT_MEMBERS \
|
||||||
|
} STRUCT_NAME; \
|
||||||
|
kt_declare(STRUCT_NAME);
|
||||||
@ -1,52 +1,90 @@
|
|||||||
#include "../base.h"
|
#include "../base.h"
|
||||||
|
#include "../../kprint/kprint_format.h"
|
||||||
|
|
||||||
ktid_define(Unitype);
|
|
||||||
|
char* __Unitype_toString(void* _u, u32 fmt){
|
||||||
|
return Unitype_toString(*(Unitype*)_u, fmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
kt_define(Unitype, __UnitypePtr_free, __Unitype_toString);
|
||||||
|
|
||||||
void Unitype_free(Unitype u){
|
void Unitype_free(Unitype u){
|
||||||
ktDescriptor type=ktDescriptor_get(u.typeId);
|
if(u.typeId==ktid_undefined){
|
||||||
if(type.freeMembers)
|
if(u.VoidPtr!=NULL)
|
||||||
type.freeMembers(u.VoidPtr);
|
throw("unitype with undefined typeId has value");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ktDescriptor* type=ktDescriptor_get(u.typeId);
|
||||||
|
if(type->freeMembers)
|
||||||
|
type->freeMembers(u.VoidPtr);
|
||||||
if(u.allocatedInHeap)
|
if(u.allocatedInHeap)
|
||||||
free(u.VoidPtr);
|
free(u.VoidPtr);
|
||||||
}
|
}
|
||||||
void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); }
|
void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); }
|
||||||
|
|
||||||
char* toString_Unitype(void* _u, u32 fmt){
|
|
||||||
Unitype* u=_u;
|
char* Unitype_toString(Unitype u, u32 fmt){
|
||||||
ktDescriptor type=ktDescriptor_get(u->typeId);
|
if(u.typeId==ktid_undefined){
|
||||||
char* valuestr=type.toString(_u, fmt);
|
if(u.VoidPtr!=NULL)
|
||||||
char* rezult=cptr_concat("{ type: ", type.name,
|
throw("unitype with undefined typeId has value");
|
||||||
", allocated on heap: ", (u->allocatedInHeap ? "true" : "false"),
|
return cptr_copy("{ERROR_TYPE}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(fmt==0) {
|
||||||
|
if(u.typeId==ktid_name(bool) ||
|
||||||
|
u.typeId==ktid_name(i8) || u.typeId==ktid_name(i16) ||
|
||||||
|
u.typeId==ktid_name(i32) || u.typeId==ktid_name(i64)){
|
||||||
|
// auto format set
|
||||||
|
fmt=kp_i;
|
||||||
|
// replaces value with pointer to value to pass into toString_i64(void*, u32)
|
||||||
|
i64 value=u.Int64;
|
||||||
|
u.VoidPtr=&value;
|
||||||
|
}
|
||||||
|
else if(u.typeId==ktid_name(u8) || u.typeId==ktid_name(u16) ||
|
||||||
|
u.typeId==ktid_name(u32) || u.typeId==ktid_name(u64)){
|
||||||
|
fmt=kp_u;
|
||||||
|
u64 value=u.UInt64;
|
||||||
|
u.VoidPtr=&value;
|
||||||
|
}
|
||||||
|
else if(u.typeId==ktid_name(f32) || u.typeId==ktid_name(f64)){
|
||||||
|
fmt=kp_f;
|
||||||
|
f64 value=u.Float64;
|
||||||
|
u.VoidPtr=&value;
|
||||||
|
}
|
||||||
|
else if(u.typeId==ktid_name(char)){
|
||||||
|
fmt=kp_c;
|
||||||
|
i64 value=u.Int64;
|
||||||
|
u.VoidPtr=&value;
|
||||||
|
}
|
||||||
|
else if(u.typeId==ktid_ptrName(char)){
|
||||||
|
char* value=u.VoidPtr;
|
||||||
|
u.VoidPtr=&value;
|
||||||
|
fmt=kp_s;
|
||||||
|
}
|
||||||
|
else if(u.typeId==ktid_name(Pointer)){
|
||||||
|
if(u.VoidPtr==NULL)
|
||||||
|
return cptr_copy("{ UniNull }");
|
||||||
|
fmt=kp_h;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ktDescriptor* type=ktDescriptor_get(u.typeId);
|
||||||
|
char* valuestr;
|
||||||
|
if(type->toString)
|
||||||
|
valuestr=type->toString(u.VoidPtr, fmt);
|
||||||
|
else valuestr="ERR_NO_TOSTRING_FUNC";
|
||||||
|
char* rezult=cptr_concat("{ type: ", type->name,
|
||||||
|
", allocated on heap: ", (u.allocatedInHeap ? "true" : "false"),
|
||||||
", value:", valuestr, " }");
|
", value:", valuestr, " }");
|
||||||
|
if(type->toString)
|
||||||
free(valuestr);
|
free(valuestr);
|
||||||
return rezult;
|
return rezult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define BUFSIZE 64
|
|
||||||
char* sprintuni(Unitype v){
|
|
||||||
char* buf=malloc(BUFSIZE);
|
|
||||||
ktDescriptor type=ktDescriptor_get(v.typeId);
|
|
||||||
if(v.typeId==ktid_Null)
|
|
||||||
sprintf_s(buf, BUFSIZE, "{Null}");
|
|
||||||
else if(v.typeId==ktid_name(f64))
|
|
||||||
sprintf_s(buf, BUFSIZE, "{%s : %lf}", type.name,v.Float64);
|
|
||||||
else if(v.typeId==ktid_name(bool) || v.typeId==ktid_name(u64))
|
|
||||||
sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%llu", "%lu") "}", type.name,v.UInt64);
|
|
||||||
else if(v.typeId==ktid_name(i64))
|
|
||||||
sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%lld", "%ld") "}", type.name,v.Int64);
|
|
||||||
else if(v.typeId==ktid_ptrName(char)){
|
|
||||||
size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2;
|
|
||||||
buf=realloc(buf, newBUFSIZE);
|
|
||||||
sprintf_s(buf, BUFSIZE, "{%s : \"%s\"}", type.name,(char*)v.VoidPtr);
|
|
||||||
}
|
|
||||||
else sprintf_s(buf, BUFSIZE, "{%s : %p}", type.name,v.VoidPtr);
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
void printuni(Unitype v){
|
void printuni(Unitype v){
|
||||||
char* s=sprintuni(v);
|
char* s=Unitype_toString(v,0);
|
||||||
fputs(s, stdout);
|
fputs(s, stdout);
|
||||||
|
fputc('\n',stdout);
|
||||||
free(s);
|
free(s);
|
||||||
}
|
}
|
||||||
@ -5,8 +5,9 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "ktid.h"
|
#include "ktid.h"
|
||||||
|
#include "typedef_macros.h"
|
||||||
|
|
||||||
typedef struct Unitype{
|
STRUCT(Unitype,
|
||||||
union {
|
union {
|
||||||
i64 Int64;
|
i64 Int64;
|
||||||
u64 UInt64;
|
u64 UInt64;
|
||||||
@ -17,8 +18,7 @@ typedef struct Unitype{
|
|||||||
};
|
};
|
||||||
ktid typeId;
|
ktid typeId;
|
||||||
bool allocatedInHeap; // should Unitype_free call free() to VoidPtr*
|
bool allocatedInHeap; // should Unitype_free call free() to VoidPtr*
|
||||||
} Unitype;
|
)
|
||||||
ktid_declare(Unitype);
|
|
||||||
|
|
||||||
|
|
||||||
#define __UniDef(FIELD, TYPE, VAL) (Unitype){ \
|
#define __UniDef(FIELD, TYPE, VAL) (Unitype){ \
|
||||||
@ -34,15 +34,18 @@ ktid_declare(Unitype);
|
|||||||
#define UniHeapPtr(TYPE, VAL) (Unitype){ \
|
#define UniHeapPtr(TYPE, VAL) (Unitype){ \
|
||||||
.VoidPtr=VAL, .typeId=ktid_ptrName(TYPE), .allocatedInHeap=true}
|
.VoidPtr=VAL, .typeId=ktid_ptrName(TYPE), .allocatedInHeap=true}
|
||||||
|
|
||||||
#define UniNull (Unitype){.Int64=0, .typeId=ktid_Null, .allocatedInHeap=false}
|
// 0==ktid_Pointer
|
||||||
|
#define UniNull (Unitype){.Int64=0, .typeId=0, .allocatedInHeap=false}
|
||||||
#define UniTrue UniBool(true)
|
#define UniTrue UniBool(true)
|
||||||
#define UniFalse UniBool(false)
|
#define UniFalse UniBool(false)
|
||||||
|
|
||||||
|
#define Unitype_isUniNull(UNI) (UNI.typeId==ktid_Pointer && UNI.VoidPtr==NULL)
|
||||||
|
|
||||||
// frees VoidPtr value or does nothing if type isn't pointer
|
// frees VoidPtr value or does nothing if type isn't pointer
|
||||||
void Unitype_free(Unitype u);
|
void Unitype_free(Unitype u);
|
||||||
void __UnitypePtr_free(void* u);
|
void __UnitypePtr_free(void* u);
|
||||||
|
char* Unitype_toString(Unitype v, u32 fmt);
|
||||||
void printuni(Unitype v);
|
void printuni(Unitype v);
|
||||||
char* sprintuni(Unitype v);
|
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -47,4 +47,4 @@ I don't really like printf function (and its variants), so i made safer and more
|
|||||||
Maybe m=MaybeNull;
|
Maybe m=MaybeNull;
|
||||||
kprint(kp_fgBlue|kp_s, "Maybe: ", kp_fgGreen|ktid_MaybePtr, &m);
|
kprint(kp_fgBlue|kp_s, "Maybe: ", kp_fgGreen|ktid_MaybePtr, &m);
|
||||||
```
|
```
|
||||||
output: <span style="color:blue">Maybe:</span> <span style="color:lightgreen">{value={0, ktid_Null}}</span>
|
output: <span style="color:blue">Maybe:</span> <span style="color:lightgreen">{value: { Pointer, 0x0 }}</span>
|
||||||
|
|||||||
@ -15,7 +15,7 @@ ktid __typeFromFormat(kp_fmt f){
|
|||||||
case kp_f:
|
case kp_f:
|
||||||
return ktid_name(f64);
|
return ktid_name(f64);
|
||||||
case kp_c:
|
case kp_c:
|
||||||
return ktid_char;
|
return ktid_name(char);
|
||||||
case kp_s:
|
case kp_s:
|
||||||
return ktid_ptrName(char);
|
return ktid_ptrName(char);
|
||||||
default:
|
default:
|
||||||
@ -26,12 +26,12 @@ ktid __typeFromFormat(kp_fmt f){
|
|||||||
Maybe __next_toString(kp_fmt f, __kp_value_union* object){
|
Maybe __next_toString(kp_fmt f, __kp_value_union* object){
|
||||||
// detecting type
|
// detecting type
|
||||||
ktid typeId=__typeFromFormat(f);
|
ktid typeId=__typeFromFormat(f);
|
||||||
if(typeId==-1)
|
if(typeId==ktid_undefined)
|
||||||
safethrow("typeId is not set, can't autodetect type",;);
|
safethrow("typeId is undefined, can't autodetect type",;);
|
||||||
ktDescriptor typeDesc=ktDescriptor_get(typeId);
|
ktDescriptor* type=ktDescriptor_get(typeId);
|
||||||
if(!typeDesc.toString)
|
if(!type->toString)
|
||||||
safethrow("type descriptor doesnt have toString() func",;);
|
safethrow("type descriptor doesnt have toString() func",;);
|
||||||
return SUCCESS(UniHeapPtr(char, typeDesc.toString(object, f)));
|
return SUCCESS(UniHeapPtr(char, type->toString(object, f)));
|
||||||
}
|
}
|
||||||
|
|
||||||
Maybe check_argsN(u8 n){
|
Maybe check_argsN(u8 n){
|
||||||
@ -160,14 +160,14 @@ void kprint_setColor(kp_fmt f){
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Maybe ksprint_ar(u32 count, kp_fmt format, ktid typeId, void* array){
|
/* Maybe ksprint_ar(u32 count, kp_fmt format, ktid typeId, void* array){
|
||||||
ktDescriptor typeDesc=ktDescriptor_get(format.typeId);
|
ktDescriptor* type=ktDescriptor_get(format.typeId);
|
||||||
if(!typeDesc.toString)
|
if(!type->toString)
|
||||||
safethrow("type descriptor doesnt have toString() func",;);
|
safethrow("type descriptor doesnt have toString() func",;);
|
||||||
StringBuilder* strb=StringBuilder_create();
|
StringBuilder* strb=StringBuilder_create();
|
||||||
StringBuilder_append_char(strb, '[');
|
StringBuilder_append_char(strb, '[');
|
||||||
for (u16 e=1; e<count; e++){
|
for (u16 e=1; e<count; e++){
|
||||||
StringBuilder_append_char(strb, ' ');
|
StringBuilder_append_char(strb, ' ');
|
||||||
char* elStr=typeDesc.toString(array+typeDesc.size*e, &format);
|
char* elStr=type->toString(array+type->size*e, &format);
|
||||||
StringBuilder_append_cptr(strb, elStr);
|
StringBuilder_append_cptr(strb, elStr);
|
||||||
StringBuilder_append_char(strb, ',');
|
StringBuilder_append_char(strb, ',');
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,7 +8,7 @@ extern "C" {
|
|||||||
// ^ ^^^^
|
// ^ ^^^^
|
||||||
// | color num
|
// | color num
|
||||||
// fgColorSet flag
|
// fgColorSet flag
|
||||||
PACK_ENUM(kp_fgColor,
|
PACKED_ENUM(kp_fgColor,
|
||||||
/// black foreground
|
/// black foreground
|
||||||
kp_fgBlack = 0x80000000,
|
kp_fgBlack = 0x80000000,
|
||||||
/// dark red foreground
|
/// dark red foreground
|
||||||
@ -46,7 +46,7 @@ PACK_ENUM(kp_fgColor,
|
|||||||
// 01000000 00000000 00000000 00000000
|
// 01000000 00000000 00000000 00000000
|
||||||
// ^ ^^^^
|
// ^ ^^^^
|
||||||
// bgColorSet flag color num
|
// bgColorSet flag color num
|
||||||
PACK_ENUM(kp_bgColor,
|
PACKED_ENUM(kp_bgColor,
|
||||||
/// black background
|
/// black background
|
||||||
kp_bgBlack = 0x40000000,
|
kp_bgBlack = 0x40000000,
|
||||||
/// dark red background
|
/// dark red background
|
||||||
|
|||||||
@ -10,7 +10,7 @@ extern "C" {
|
|||||||
/// kprint_format
|
/// kprint_format
|
||||||
typedef u32 kp_fmt;
|
typedef u32 kp_fmt;
|
||||||
|
|
||||||
PACK_ENUM(kp_dataFmt,
|
PACKED_ENUM(kp_dataFmt,
|
||||||
// 00000000 00000000 00000000 00000000
|
// 00000000 00000000 00000000 00000000
|
||||||
// ^^^^
|
// ^^^^
|
||||||
// type
|
// type
|
||||||
|
|||||||
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
void test_type_system(){
|
void test_type_system(){
|
||||||
for(ktid id=0; id<ktid_last; id++){
|
for(ktid id=0; id<ktid_last; id++){
|
||||||
ktDescriptor d=ktDescriptor_get(id);
|
ktDescriptor* type=ktDescriptor_get(id);
|
||||||
kprintf("\e[37m{ id:%u name:%s size:%u freeMembers:%p toString:%p }\n",
|
kprintf("\e[37m{ id:%u name:%s size:%u freeMembers:%p toString:%p }\n",
|
||||||
d.id, d.name, d.size, d.freeMembers, d.toString);
|
type->id, type->name, type->size, type->freeMembers, type->toString);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user