From 2a31d770edefef463598b98bec7a760344f5beed Mon Sep 17 00:00:00 2001 From: MCorange Date: Sat, 31 May 2025 16:12:31 +0300 Subject: [PATCH] BUUUUUNCH of shit, but added logger, more stl nonsense, improved code quaklity, squashed bugs. --- .vscode/launch.json | 2 +- Makefile | 10 +- src/include/collections/hasmap.hpp | 35 + src/include/collections/vec.hpp | 4 +- src/include/config/platform.hpp | 23 + src/include/format/fmt.hpp | 13 +- src/include/format/obj_fmt_helper.hpp | 43 ++ src/include/hash/hash.hpp | 17 + src/include/io/print.hpp | 6 +- src/include/logger.hpp | 60 ++ src/include/option.hpp | 15 + src/include/result.hpp | 17 +- src/include/std.hpp | 2 +- src/include/stddef.hpp | 7 + src/include/stl.hpp | 7 + src/include/stl/add_lvalue_reference.hpp | 35 + src/include/stl/add_rvalue_reference.hpp | 36 + src/include/stl/integer_sequence.hpp | 67 ++ src/include/stl/is_constructable.hpp | 32 + src/include/stl/is_integral.hpp | 53 ++ src/include/stl/is_referenceable.hpp | 15 + src/include/stl/void_t.hpp | 10 + src/include/sync/mutex.hpp | 77 ++ src/include/sync/spinlock.hpp | 64 ++ src/include/thirdparty/field_reflection.hpp | 0 .../thirdparty/field_reflection_std.hpp | 701 ++++++++++++++++++ src/internals.cpp | 15 + src/logger.cpp | 106 +++ test/Makefile | 6 +- test/test | Bin 58680 -> 125800 bytes test/test.cpp | 86 ++- 31 files changed, 1543 insertions(+), 21 deletions(-) create mode 100644 src/include/collections/hasmap.hpp create mode 100644 src/include/config/platform.hpp create mode 100644 src/include/format/obj_fmt_helper.hpp create mode 100644 src/include/hash/hash.hpp create mode 100644 src/include/logger.hpp create mode 100644 src/include/stddef.hpp create mode 100644 src/include/stl/add_lvalue_reference.hpp create mode 100644 src/include/stl/add_rvalue_reference.hpp create mode 100644 src/include/stl/integer_sequence.hpp create mode 100644 src/include/stl/is_constructable.hpp create mode 100644 src/include/stl/is_integral.hpp create mode 100644 src/include/stl/is_referenceable.hpp create mode 100644 src/include/stl/void_t.hpp create mode 100644 src/include/sync/mutex.hpp create mode 100644 src/include/sync/spinlock.hpp create mode 100644 src/include/thirdparty/field_reflection.hpp create mode 100644 src/include/thirdparty/field_reflection_std.hpp create mode 100644 src/internals.cpp create mode 100644 src/logger.cpp diff --git a/.vscode/launch.json b/.vscode/launch.json index d4d8817..20ca61f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,7 +6,7 @@ "configurations": [ { "name": "(gdb) Launch", - "type": "cppdbg", + "type": "by-gdb", "request": "launch", "program": "${workspaceFolder}/test/test", "args": [], diff --git a/Makefile b/Makefile index 16d1a61..6328dfe 100644 --- a/Makefile +++ b/Makefile @@ -2,11 +2,11 @@ LIB=libc+ COM_FLAGS = -fPIC -Isrc/include CC_FLAGS = -std=c23 -ggdb -CXX_FLAGS = -std=c++23 -nostdinc++ -ggdb -LD_FLAGS = -nostdlib++ -lsupc++ -ggdb -CC=gcc -CXX=g++ -LD=gcc +CXX_FLAGS = -std=c++23 -nostdinc++ -ggdb -fexceptions +LD_FLAGS = -nostdlib++ -lsupc++ -ggdb -fexceptions +CC=clang +CXX=clang +LD=clang AR=ar cxx_sources=$(wildcard src/*.cpp) diff --git a/src/include/collections/hasmap.hpp b/src/include/collections/hasmap.hpp new file mode 100644 index 0000000..b7add1a --- /dev/null +++ b/src/include/collections/hasmap.hpp @@ -0,0 +1,35 @@ +#ifndef _H_LIBCP_COLLECTIONS_HASHMAP +#define _H_LIBCP_COLLECTIONS_HASHMAP + +#include "option.hpp" +#include +#include + +namespace __internal { + + template + struct HashMapPart { + private: + // uint8_t hash[4]; + K key; + V val; + + public: + }; +} // namespace __internal + +template +class HashMap { +private: + Vec<__internal::HashMapPart> items; + +public: + HashMap() = default; + ~HashMap() = default; + + Option insert(K& key, V& value) {} + + Option get(K& key) {} +}; + +#endif // !_H_LIBCP_COLLECTIONS_HASHMAP diff --git a/src/include/collections/vec.hpp b/src/include/collections/vec.hpp index e24c75d..1ff501c 100644 --- a/src/include/collections/vec.hpp +++ b/src/include/collections/vec.hpp @@ -1,10 +1,10 @@ #ifndef _H_LIBCP_COLLECTIONS_VEC #define _H_LIBCP_COLLECTIONS_VEC -#include "result.hpp" +#include #include #include -#include +#include template class Vec { diff --git a/src/include/config/platform.hpp b/src/include/config/platform.hpp new file mode 100644 index 0000000..3c809a9 --- /dev/null +++ b/src/include/config/platform.hpp @@ -0,0 +1,23 @@ +#ifndef _H_LIBCP_CONFIG_PLATFORM + +#if defined(__linux__) +#define PLATFORM_LINUX +#elif defined(__APPLE__) && defined(__MACH__) +#define PLATFORM_MACOS +#elif defined(__FreeBSD__) +#define PLATFORM_FREEBSD +#elif defined(__NetBSD__) +#define PLATFORM_NETBSD +#elif defined(__OpenBSD__) +#define PLATFORM_OPENBSD +#elif defined(_WIN32) +#define PLATFORM_WINDOWS +#else +#define PLATFORM_UNKNOWN +#endif + +#ifdef PLATFORM_UNKNOWN +#error This platform is not supported by this library yet +#endif // PLATFORM_UNKNOWN + +#endif // !_H_LIBCP_CONFIG_PLATFORM diff --git a/src/include/format/fmt.hpp b/src/include/format/fmt.hpp index 6d7af90..044bc44 100644 --- a/src/include/format/fmt.hpp +++ b/src/include/format/fmt.hpp @@ -18,7 +18,7 @@ concept Format = requires(T t) { #ifndef __CONCEPT_ONLY -namespace __internal { +namespace { String format(Vec& args, String& _fmt) { const char* fmt = _fmt.as_cstr(); String str = String(); @@ -33,7 +33,9 @@ namespace __internal { } if (fmt[i + 1] == '}') { i += 1; - String* arg = args.nth(args_idx).unwrap(); + if (args.len() <= args_idx) { + } + String* arg = args.nth(args_idx).expect("Amount of arguments dont match amount of placeholders in format string"); str.push_str(*arg); args_idx += 1; } @@ -54,17 +56,18 @@ namespace __internal { args.push(Formatter::fmt_to_str(var1)); } - return __internal::format(args, fmt, var2...); + return format(args, fmt, var2...); } -} // namespace __internal +} // namespace template String format(const char* fmt, Types... vars) { Vec args = Vec(); String fmt_ = String(fmt); - return __internal::format(args, fmt_, vars...); + return format(args, fmt_, vars...); } +// Has to be here, cause c++ template struct Formatter> { static String fmt_to_str(Vec& val) { diff --git a/src/include/format/obj_fmt_helper.hpp b/src/include/format/obj_fmt_helper.hpp new file mode 100644 index 0000000..428468c --- /dev/null +++ b/src/include/format/obj_fmt_helper.hpp @@ -0,0 +1,43 @@ +#ifndef _H_LIBCP_FORMAT_OBJ_FMT_HELPER +#define _H_LIBCP_FORMAT_OBJ_FMT_HELPER + +#include +#include +#include +#include + +class ObjFmtHelper { +private: + String buffer = String(); + size_t nest_level = 0; + +public: + ObjFmtHelper(const char* name) { + buffer.push_str(name); + buffer.push_str(" {\n"); + }; + + ObjFmtHelper(int level, const char* name) { + this->nest_level = level; + buffer.push_str(name); + buffer.push_str(" {\n"); + }; + + ~ObjFmtHelper() = default; + + template + void add_field(const char* name, T val) { + for (size_t i = 0; i < nest_level + 1; i++) { + this->buffer.push_str(" "); + } + auto s = format("{}: {}\n", name, val); + this->buffer.push_str(s); + } + + String finish() { + this->buffer.push_str("}\n"); + return this->buffer; + } +}; + +#endif // !_H_LIBCP_FORMAT_OBJ_FMT_HELPER diff --git a/src/include/hash/hash.hpp b/src/include/hash/hash.hpp new file mode 100644 index 0000000..6cba728 --- /dev/null +++ b/src/include/hash/hash.hpp @@ -0,0 +1,17 @@ + + +#include +#include +#include + +#define HASH_SIZE 4 + +template +struct Hasher; + +template +concept Hash = requires(T t) { + { t.hash() } -> stl::same_as; +} || requires(T t) { + { Hasher::hash(t) } -> stl::same_as; +}; diff --git a/src/include/io/print.hpp b/src/include/io/print.hpp index fc6918a..93c88ce 100644 --- a/src/include/io/print.hpp +++ b/src/include/io/print.hpp @@ -4,14 +4,16 @@ #include extern "C" { -int puts(const char* s); int putchar(int c); } template void print(const char* fmt, Types... args) { String s = format(fmt, args...); - puts(s.as_cstr()); + char* cs = s.as_cstr(); + for (int i = 0; i < s.len(); i++) { + putchar(cs[i]); + } } template diff --git a/src/include/logger.hpp b/src/include/logger.hpp new file mode 100644 index 0000000..0be0a50 --- /dev/null +++ b/src/include/logger.hpp @@ -0,0 +1,60 @@ +#include "format/fmt.hpp" +#ifndef _H_LIBCP_LOGGER + +#include +#include +#include + +namespace log { +#define LOG_LEVEL_COUNT 4 + + enum LogLevel { LOG_OFF = -1, LOG_ERROR = 0, LOG_WARN, LOG_INFO, LOG_DEBUG }; + + class Logger { + private: + LogLevel filter_level = LOG_INFO; + const char* prefixes[LOG_LEVEL_COUNT]; + + public: + void log(LogLevel level, const char* file, const int line, const char* func, const char* str); + void log(LogLevel level, const char* file, const int line, const char* func, String str); + + void set_log_level(LogLevel level); + const char* get_prefix(LogLevel level); + void load_default_prefixes_with_color(); + void load_default_prefixes(); + void load_custom_prefixes(const char* pfx[LOG_LEVEL_COUNT]); + + Logger(); + ~Logger() = default; + + private: + bool should_display(LogLevel level); + }; + + namespace { + SpinLock LOGGER = SpinLock(new log::Logger()); + } + + template + void debug(const char* fmt, Args... args, const char* file = __builtin_FILE(), int line = __builtin_LINE(), const char* function = __builtin_FUNCTION()) { + LOGGER.lock().get()->log(LOG_DEBUG, file, line, function, format(fmt, args...)); + } + + template + void info(const char* fmt, Args... args, const char* file = __builtin_FILE(), int line = __builtin_LINE(), const char* function = __builtin_FUNCTION()) { + LOGGER.lock().get()->log(LOG_INFO, file, line, function, format(fmt, args...)); + } + + template + void warn(const char* fmt, Args... args, const char* file = __builtin_FILE(), int line = __builtin_LINE(), const char* function = __builtin_FUNCTION()) { + LOGGER.lock().get()->log(LOG_WARN, file, line, function, format(fmt, args...)); + } + + template + void error(const char* fmt, Args... args, const char* file = __builtin_FILE(), int line = __builtin_LINE(), const char* function = __builtin_FUNCTION()) { + LOGGER.lock().get()->log(LOG_ERROR, file, line, function, format(fmt, args...)); + } +} // namespace log + +#endif // !_H_LIBCP_LOGGER diff --git a/src/include/option.hpp b/src/include/option.hpp index c5dd4b7..5e6a92e 100644 --- a/src/include/option.hpp +++ b/src/include/option.hpp @@ -3,6 +3,10 @@ #define _H_LIBCP_OPTION #include +extern "C" { +int puts(const char* s); +} + template class Option { bool has_value = false; @@ -35,8 +39,19 @@ public: return !has_value; } + T& expect(const char* str) { + if (!has_value) { + puts("PANIC: Unwrapped a None Option: "); + puts(str); + puts("\n"); + __builtin_trap(); + } + return this->storage; + } + T& unwrap() { if (!has_value) { + puts("PANIC: Unwrapped a None Option\n"); __builtin_trap(); } return this->storage; diff --git a/src/include/result.hpp b/src/include/result.hpp index 3d19a34..5bdeba3 100644 --- a/src/include/result.hpp +++ b/src/include/result.hpp @@ -3,6 +3,10 @@ #include +extern "C" { +int puts(const char* s); +}; + template struct ResultErrBase { bool is_okay = true; @@ -80,9 +84,20 @@ public: } } + T& expect(const char* str) { + if (!this->is_ok()) { + puts("PANIC: Unwrapped result with error: "); + puts(str); + puts("\n"); + __builtin_trap(); + } + return &this->ok_data; + } + T& unwrap() { if (!this->is_ok()) { - throw "Unwrapped result with error"; + puts("PANIC: Unwrapped result with error\n"); + __builtin_trap(); } return &this->ok_data; } diff --git a/src/include/std.hpp b/src/include/std.hpp index 66accf4..47ab38e 100644 --- a/src/include/std.hpp +++ b/src/include/std.hpp @@ -2,8 +2,8 @@ #ifndef _H_LIBCP #define _H_LIBCP +#include #include - #include #include #include diff --git a/src/include/stddef.hpp b/src/include/stddef.hpp new file mode 100644 index 0000000..ac691f8 --- /dev/null +++ b/src/include/stddef.hpp @@ -0,0 +1,7 @@ +#ifndef _H_LIBCP_STDDEF + +using size_t = decltype(sizeof(int)); +using ptrdiff_t = decltype(static_cast(nullptr) - static_cast(nullptr)); +using nullptr_t = decltype(nullptr); + +#endif // !_H_LIBCP_STDDEF diff --git a/src/include/stl.hpp b/src/include/stl.hpp index de62858..64faa50 100644 --- a/src/include/stl.hpp +++ b/src/include/stl.hpp @@ -9,6 +9,13 @@ namespace stl { #include #include #include +#include +#include +#include +#include +#include +#include +#include } // namespace stl #endif // _H_LIBCP_STL \ No newline at end of file diff --git a/src/include/stl/add_lvalue_reference.hpp b/src/include/stl/add_lvalue_reference.hpp new file mode 100644 index 0000000..015842b --- /dev/null +++ b/src/include/stl/add_lvalue_reference.hpp @@ -0,0 +1,35 @@ +#ifndef _H_LIBCP_STL_ADD_LVALUE_REF +#define _H_LIBCP_STL_ADD_LVALUE_REF + +#include + +#if __has_builtin(__add_lvalue_reference) + +template +using __add_lvalue_reference_t = __add_lvalue_reference(_Tp); + +#else + +template > +struct __add_lvalue_reference_impl { + using type = _Tp; +}; + +template +struct __add_lvalue_reference_impl<_Tp, true> { + using type = _Tp&; +}; + +template +using __add_lvalue_reference_t = typename __add_lvalue_reference_impl<_Tp>::type; + +#endif // __has_builtin(__add_lvalue_reference) + +template +struct add_lvalue_reference { + using type = __add_lvalue_reference_t<_Tp>; +}; + +template +using add_lvalue_reference_t = __add_lvalue_reference_t<_Tp>; +#endif // _H_LIBCP_STL_ADD_LVALUE_REF diff --git a/src/include/stl/add_rvalue_reference.hpp b/src/include/stl/add_rvalue_reference.hpp new file mode 100644 index 0000000..07dd0d2 --- /dev/null +++ b/src/include/stl/add_rvalue_reference.hpp @@ -0,0 +1,36 @@ +#ifndef _H_LIBCP_STL_ADD_RVALUE_REF +#define _H_LIBCP_STL_ADD_RVALUE_REF + +#include + +#if __has_builtin(__add_rvalue_reference) + +template +using __add_rvalue_reference_t = __add_rvalue_reference(_Tp); + +#else + +template > +struct __add_rvalue_reference_impl { + using type = _Tp; +}; + +template +struct __add_rvalue_reference_impl<_Tp, true> { + using type = _Tp&&; +}; + +template +using __add_rvalue_reference_t = typename __add_rvalue_reference_impl<_Tp>::type; + +#endif // __has_builtin(__add_rvalue_reference) + +template +struct add_rvalue_reference { + using type = __add_rvalue_reference_t<_Tp>; +}; + +template +using add_rvalue_reference_t = __add_rvalue_reference_t<_Tp>; + +#endif // _H_LIBCP_STL_ADD_RVALUE_REF diff --git a/src/include/stl/integer_sequence.hpp b/src/include/stl/integer_sequence.hpp new file mode 100644 index 0000000..20537d2 --- /dev/null +++ b/src/include/stl/integer_sequence.hpp @@ -0,0 +1,67 @@ +#ifndef _H_LIBCP_STL_INTEGER_SEQUENCE +#define _H_LIBCP_STL_INTEGER_SEQUENCE + +#include +#include +template +struct __tuple_indices; + +template +struct __integer_sequence { + template