From 19eb183b38511998322d99c33515c079d89785e3 Mon Sep 17 00:00:00 2001 From: nodist Date: Sat, 24 Oct 2026 20:06:26 -0400 Subject: add kpl_identifier --- include/kpl.h | 5 ++++ include/kpl/allocate.h | 5 +++- include/kpl/ast.h | 6 +++++ include/kpl/env.h | 7 ++--- include/kpl/identifier.h | 41 +++++++++++++++++++++++++++++ include/kpl/io.h | 4 +-- include/kpl/object.h | 16 ++---------- include/kpl/storage.h | 44 +++++++++++++++++++++++++++++++ include/kpl/tree.h | 1 - include/kpl/type.h | 67 +++++++++++++++++++++++++++++++++++++++++++++++ source/config.c | 9 +++++-- source/identifier.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ test/identifier.c | 11 ++++++++ 13 files changed, 261 insertions(+), 23 deletions(-) create mode 100644 include/kpl/ast.h create mode 100644 include/kpl/identifier.h create mode 100644 include/kpl/storage.h create mode 100644 include/kpl/type.h create mode 100644 source/identifier.c create mode 100644 test/identifier.c diff --git a/include/kpl.h b/include/kpl.h index 838478b..8623488 100644 --- a/include/kpl.h +++ b/include/kpl.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -51,6 +52,10 @@ typedef struct _kpl_env kpl_env; #include "kpl/class.h" #include "kpl/object.h" #include "kpl/tree.h" +#include "kpl/identifier.h" +#include "kpl/type.h" +#include "kpl/storage.h" +#include "kpl/ast.h" #include "kpl/memory.h" #include "kpl/io.h" #include "kpl/env.h" diff --git a/include/kpl/allocate.h b/include/kpl/allocate.h index ee24d72..3da4a10 100644 --- a/include/kpl/allocate.h +++ b/include/kpl/allocate.h @@ -19,6 +19,7 @@ inline void *kpl_object_allocate(kpl_thread *thread, int32_t size) { if (thread->gc_head) thread->gc_head->gc_prev = obj; thread->gc_head = obj; + thread->gc_cycle_change++; return obj; } @@ -31,5 +32,7 @@ inline void kpl_object_return(kpl_env *env, void *object) { obj->gc_prev->gc_next = obj->gc_next; if (obj->gc_next) obj->gc_next->gc_prev = obj->gc_prev; - kpl_memory_allocator_return(&env->threads[obj->thread_id].memory_allocator, object); + kpl_thread *thread = &env->threads[obj->thread_id]; + kpl_memory_allocator_return(&thread->memory_allocator, object); + thread->gc_cycle_change--; } diff --git a/include/kpl/ast.h b/include/kpl/ast.h new file mode 100644 index 0000000..b9ba591 --- /dev/null +++ b/include/kpl/ast.h @@ -0,0 +1,6 @@ +/* + * KPL AST + * An AST is the definition of code and data + */ + +#pragma once diff --git a/include/kpl/env.h b/include/kpl/env.h index 1e3d3d8..5ea3b33 100644 --- a/include/kpl/env.h +++ b/include/kpl/env.h @@ -14,6 +14,7 @@ typedef enum : int32_t { typedef struct _kpl_thread { uint16_t thread_id; // self id kpl_memory_allocator memory_allocator; + _Atomic ssize_t gc_cycle_change; // number of objects added(+)/removed(-) to gc_head after last sweep kpl_object *gc_head, *gc_mark_head; // sweep if not marked or not in io pending kpl_io_uring io_uring; // TODO jit allocator @@ -40,7 +41,6 @@ typedef enum : uint32_t { * kpl_env_config &env, argc, argv * kpl_env_init &env * add to the namespace tree - * add a file load task, string evaluate task or REPL task to main thread task queue * kpl_env_run &env * kpl_env_free &env */ @@ -51,8 +51,9 @@ typedef struct _kpl_env { const char *kpl_entry; // file or string to be ran, not used for repl, pointer used to get kpl args namespace _Atomic ssize_t total_priority; // the sum of the threads priority fields, application stops on zero kpl_thread *threads; // 0 is main thread, >= 1 are spawned pthreads - // TODO namespace tree - // TODO module tree + kpl_tree namespace_tree, module_tree; // Trees of kpl_storage + pthread_mutex_t module_tree_mutex; + // TODO jit } kpl_env; // kpl state, no globals, must be zero initialized /* diff --git a/include/kpl/identifier.h b/include/kpl/identifier.h new file mode 100644 index 0000000..092b0c4 --- /dev/null +++ b/include/kpl/identifier.h @@ -0,0 +1,41 @@ +/* + * Thread safe identifiers, at the cost of space remove the need for hash/tree lookups + * Keep 50 [A-Za-z0-9_] Characters in 40 bytes + */ + +#pragma once + +#define KPL_IDENTIFIER_PARTS 5 + +#define KPL_IDENTIFIER_BITS_PER_PART 64 + +#define KPL_IDENTIFIER_BITS_PER_VALUE 6 + +#define KPL_IDENTIFIER_VALUES_PER_PART (KPL_IDENTIFIER_BITS_PER_PART / KPL_IDENTIFIER_BITS_PER_VALUE) + +#define KPL_IDENTIFIER_VALUES_TOTAL_BITS (KPL_IDENTIFIER_BITS_PER_VALUE * KPL_IDENTIFIER_VALUES_PER_PART) + +#define KPL_IDENTIFIER_MAX_LENGTH (KPL_IDENTIFIER_VALUES_PER_PART * KPL_IDENTIFIER_PARTS) + +#define KPL_IDENTIFIER_C_STRING_SIZE (KPL_IDENTIFIER_MAX_LENGTH + sizeof(char)) + +/* + * Each character takes 6 bits from bit 0 to bit 59 + * The upper 4 bits represent the number of characters in the part + */ +typedef struct { + uint64_t parts[KPL_IDENTIFIER_PARTS]; +} kpl_identifier; + +/* + * Converts a c string into an identifier + * Checks if the c_string_length <= KPL_IDENTIFIER_MAX_LENGTH + */ +bool kpl_identifier_encode(kpl_identifier *identifier, const char *c_string, size_t c_string_length); + +/* + * Converts an identifier into a c string + * Does not clear the c_string buffer + * Does not check the c_string buffer assumes the buffer is at least KPL_IDENTIFIER_C_STRING_SIZE + */ +void kpl_identifier_decode(kpl_identifier *identifier, char c_string[KPL_IDENTIFIER_C_STRING_SIZE]); diff --git a/include/kpl/io.h b/include/kpl/io.h index f5904f4..3838a55 100644 --- a/include/kpl/io.h +++ b/include/kpl/io.h @@ -6,8 +6,8 @@ typedef enum : int32_t { KPL_IO_URING_QUEUE_SIZE_POW2_MIN = 12, - KPL_IO_URING_QUEUE_SIZE_POW2_MAX = 20, - KPL_IO_URING_QUEUE_SIZE_POW2_DEFAULT = 15 + KPL_IO_URING_QUEUE_SIZE_POW2_DEFAULT = 15, + KPL_IO_URING_QUEUE_SIZE_POW2_MAX = 20 } kpl_io_uring_enum; /* diff --git a/include/kpl/object.h b/include/kpl/object.h index 1053348..b7b1a33 100644 --- a/include/kpl/object.h +++ b/include/kpl/object.h @@ -6,7 +6,8 @@ typedef enum : uint8_t { KPL_OBJECT_FLAGS_EMPTY = 0, - KPL_OBJECT_FLAGS_IO_PENDING = 1 << 0 // When io pending tasks will not be reachable by GC + KPL_OBJECT_FLAGS_IO_PENDING = 1 << 0, // When io pending tasks will not be reachable by GC + KPL_OBJECT_FLAGS_GC_PERSIST = 1 << 1 // Object is never swept by GC until program end } kpl_object_flags; // for atomic linked list queue @@ -84,16 +85,3 @@ typedef struct _kpl_object_task { kpl_result result; kpl_class variables[]; // a task has no distinction between an argument or a local, both live here } kpl_object_task; - -/* - * KPL Object Mutex is a queue of tasks on an object - * Adding: Increment counter, then add task to task queue - * Removing: Remove from task queue, run task, then decrement counter - * Requires spin check when removing task due to space between counter adding and task adding - * If the counter value before was zero take control of the mutex - * The thread that controls the mutex runs each task until counter is zero - */ -typedef struct { - _Atomic int32_t lock_counter; - kpl_object_queue task_queue; -} kpl_object_mutex; diff --git a/include/kpl/storage.h b/include/kpl/storage.h new file mode 100644 index 0000000..d78f80b --- /dev/null +++ b/include/kpl/storage.h @@ -0,0 +1,44 @@ +/* + * KPL Object storage + */ + +#pragma once + +/* + * Inner tree storage + * Keys will always be kpl_identifier + */ +typedef struct _kpl_storgae_object { + KPL_TREE_HEADER(_kpl_storgae_object); + kpl_type_base *type; + kpl_class class; + kpl_identifier identifier; +} kpl_stoage_object; + +// c_string_ptr used for tree search comparisons +#define KPL_STORAGE_HEADER(STRUCT_NAME) \ + KPL_TREE_HEADER(STRUCT_NAME); \ + kpl_tree object_tree; \ + const char *c_string_ptr; + +/* + * Uses const c string keys + * The namespace is c_string_ptr + */ +typedef struct _kpl_storage_namespace { + KPL_TREE_HEADER(_kpl_storage_namespace); +} kpl_stoage_namespace; + +/* + * Alias used for string comparison functions + */ +typedef kpl_stoage_namespace kpl_stoage_search; + +/* + * Modules make a copy of absolute file path + * The c_string_ptr is pointed to this file path copy string + */ +typedef struct _kpl_storage_module { + KPL_TREE_HEADER(_kpl_storage_module); + char c_string_array[]; // null term, c_string_ptr points to this +} kpl_storage_module; diff --git a/include/kpl/tree.h b/include/kpl/tree.h index de9492d..f3503d2 100644 --- a/include/kpl/tree.h +++ b/include/kpl/tree.h @@ -6,7 +6,6 @@ // tree_duplicates is a linked stack #define KPL_TREE_HEADER(STRUCT_NAME) \ - KPL_OBJECT_HEADER(STRUCT_NAME); \ struct STRUCT_NAME *tree_left, *tree_right, *tree_duplicates; \ int32_t tree_height diff --git a/include/kpl/type.h b/include/kpl/type.h new file mode 100644 index 0000000..546a1ed --- /dev/null +++ b/include/kpl/type.h @@ -0,0 +1,67 @@ +/* + * KPL Types + * A Type is a declaration of code and data + */ + +#pragma once + +typedef enum : uint8_t { + KPL_TYPE_NAME_UNKOWN, + // VALUES + KPL_TYPE_NAME_BOOLEAN, + KPL_TYPE_NAME_INTEGER, + KPL_TYPE_NAME_U8, + KPL_TYPE_NAME_U16, + KPL_TYPE_NAME_U32, + KPL_TYPE_NAME_U64, + KPL_TYPE_NAME_I8, + KPL_TYPE_NAME_I16, + KPL_TYPE_NAME_I32, + KPL_TYPE_NAME_I64, + KPL_TYPE_NAME_FLOAT, + KPL_TYPE_NAME_F32, + KPL_TYPE_NAME_F64, + KPL_TYPE_NAME_CHAR, + // CONTAINERS + KPL_TYPE_NAME_STRING, + KPL_TYPE_NAME_ARRAY, + KPL_TYPE_NAME_TYPLE, + KPL_TYPE_NAME_TABLE, + KPL_TYPE_NAME_MAP, + KPL_TYPE_NAME_SET, + KPL_TYPE_NAME_UNION, + KPL_TYPE_NAME_ANY, + KPL_TYPE_NAME_NATIVE, + KPL_TYPE_NAME_ERROR, + // COROUTINES + KPL_TYPE_NAME_LAMBDA, + KPL_TYPE_NAME_GENERATOR, + KPL_TYPE_NAME_REGEXP, + // STORAGE + KPL_TYPE_NAME_MODULE, + KPL_TYPE_NAME_NAMESPACE +} kpl_type_name; + +typedef enum : uint8_t { + KPL_TYPE_KIND_BASE +} kpl_type_kind; + +typedef enum : uint32_t { + KPL_TYPE_FLAG_EMPTY = 0 +} kpl_type_flags; + +// type_kind is for struct polymorphism +// type_specific meaning changes depending on type_name +#define KPL_TYPE_HEADER(STRUCT_NAME) \ + KPL_OBJECT_HEADER(STRUCT_NAME); \ + kpl_type_name type_name; \ + kpl_type_kind type_kind; \ + uint16_t type_specific; \ + uint32_t type_flags + +/* + * Type has no additional information + */ +typedef struct _kpl_type_base { + KPL_TYPE_HEADER(_kpl_type_base); +} kpl_type_base; diff --git a/source/config.c b/source/config.c index f1aabf8..6e213cb 100644 --- a/source/config.c +++ b/source/config.c @@ -3,9 +3,14 @@ typedef kpl_exit_code kpl_config_option_parse_fn(kpl_env *env, int32_t arg_index, char *argv[]); +/* + * Per configuration option actions + * Used to print help messages and parse arg strings and set env values + */ typedef struct { - int32_t flags; - char match, *help_option_argument, *help_message; + int32_t flags; // flags to set on env->flags + char match; // must be set, used in printing help message + char *help_option_argument, *help_message; // help_option_argument, string type if the option takes an arg kpl_config_option_parse_fn *parse_fn; } kpl_config_option; diff --git a/source/identifier.c b/source/identifier.c new file mode 100644 index 0000000..f6a7279 --- /dev/null +++ b/source/identifier.c @@ -0,0 +1,68 @@ + +#include "kpl.h" + +#define KPL_IDENTIFIER_NUMBER_RANGE_START 1 + +#define KPL_IDENTIFIER_UPPER_RANGE_START 12 + +#define KPL_IDENTIFIER_LOWER_RANGE_START 38 + +static uint64_t kpl_identifier_char_to_value(char c, uint64_t value_index) { + uint64_t value = 0; + if (c >= '0' && c <= '9') + value = c - '0' + KPL_IDENTIFIER_NUMBER_RANGE_START; + else if (c >= 'A' && c <= 'Z') + value = c - 'A' + KPL_IDENTIFIER_UPPER_RANGE_START; + else if (c >= 'a' && c <= 'z') + value = c - 'a' + KPL_IDENTIFIER_LOWER_RANGE_START; + return value << value_index * KPL_IDENTIFIER_BITS_PER_VALUE; +} + +bool kpl_identifier_encode(kpl_identifier *identifier, const char *c_string, size_t c_string_length) { + if (c_string_length > KPL_IDENTIFIER_MAX_LENGTH) + return false; + for (size_t string_index = 0; string_index < c_string_length; string_index++) + if (!isalnum(c_string[string_index]) && c_string[string_index] != '_') + return false; + for (uint32_t part_index = 0; part_index < KPL_IDENTIFIER_PARTS; part_index++) + identifier->parts[part_index] = 0; + uint32_t string_index = 0, part_index = 0; + while (string_index < c_string_length) { + uint64_t value_index = 0; + while (value_index < KPL_IDENTIFIER_VALUES_PER_PART && string_index < c_string_length) { + identifier->parts[part_index] |= kpl_identifier_char_to_value(c_string[string_index], value_index); + value_index++; + string_index++; + } + identifier->parts[part_index] |= value_index << KPL_IDENTIFIER_VALUES_TOTAL_BITS; + part_index++; + } + return true; +} + +static char kpl_identifier_value_to_char(uint64_t part) { + uint8_t value = part & (1 << 5 | 1 << 4 | 1 << 3 | 1 << 2 | 1 << 1 | 1 << 0); + if (!value) + return '_'; + if (value < KPL_IDENTIFIER_UPPER_RANGE_START) + return value - KPL_IDENTIFIER_NUMBER_RANGE_START + '0'; + if (value < KPL_IDENTIFIER_LOWER_RANGE_START) + return value - KPL_IDENTIFIER_UPPER_RANGE_START + 'A'; + return value - KPL_IDENTIFIER_LOWER_RANGE_START + 'a'; +} + +void kpl_identifier_decode(kpl_identifier *identifier, char c_string[KPL_IDENTIFIER_C_STRING_SIZE]) { + uint32_t string_index = 0; + for (uint32_t part_index = 0; part_index < KPL_IDENTIFIER_PARTS; part_index++) { + uint64_t part = identifier->parts[part_index]; + uint64_t value_length = part >> KPL_IDENTIFIER_VALUES_TOTAL_BITS; + if (!value_length) + break; + while (value_length) { + c_string[string_index] = kpl_identifier_value_to_char(part); + part >>= KPL_IDENTIFIER_BITS_PER_VALUE; + value_length--; + string_index++; + } + } +} diff --git a/test/identifier.c b/test/identifier.c new file mode 100644 index 0000000..94c57bb --- /dev/null +++ b/test/identifier.c @@ -0,0 +1,11 @@ + +#include "kpl_test.h" + +KPL_TEST(c_string_to_identifier) { + const char *string = "aA0_t4_12w0Z"; + kpl_identifier identifier; + KPL_ASSERT(kpl_identifier_encode(&identifier, string, strlen(string)), "should encode"); + char buffer[100] = {}; + kpl_identifier_decode(&identifier, buffer); + KPL_ASSERT(strcmp(string, buffer) == 0, "should decode to same string"); +} -- cgit v1.2.3