aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/kpl.h3
-rw-r--r--include/kpl/ast.h187
-rw-r--r--include/kpl/class.h6
-rw-r--r--include/kpl/env.h19
-rw-r--r--include/kpl/memory.h2
-rw-r--r--include/kpl/object.h8
-rw-r--r--include/kpl/operator.h13
-rw-r--r--include/kpl/storage.h6
-rw-r--r--include/kpl/task.h5
-rw-r--r--include/kpl/type.h40
10 files changed, 270 insertions, 19 deletions
diff --git a/include/kpl.h b/include/kpl.h
index 467196e..ede4fdd 100644
--- a/include/kpl.h
+++ b/include/kpl.h
@@ -67,4 +67,7 @@ typedef struct _kpl_env kpl_env;
#include "kpl/array.h"
#include "kpl/error.h"
#include "kpl/task.h"
+#include "kpl/type.h"
+#include "kpl/operator.h"
+#include "kpl/ast.h"
#include "kpl/env.h"
diff --git a/include/kpl/ast.h b/include/kpl/ast.h
index b9ba591..427ce1b 100644
--- a/include/kpl/ast.h
+++ b/include/kpl/ast.h
@@ -1,6 +1,193 @@
/*
* KPL AST
* An AST is the definition of code and data
+ * AST nodes cannot be shared across threads
*/
#pragma once
+
+/*
+ * Must fit within 4 bits, 8 kinds total, 0 invalid
+ */
+typedef enum : uint8_t {
+ KPL_AST_KIND_LIST = 1,
+ KPL_AST_KIND_FUNCTION = 2,
+ KPL_AST_KIND_COROUTINE = 3,
+ KPL_AST_KIND_DATA = 4,
+ KPL_AST_KIND_IDENTIFIER = 5,
+ KPL_AST_KIND_TYPE = 6,
+ KPL_AST_KIND_OP = 7,
+ KPL_AST_KIND_ACTION = 8
+} kpl_ast_kind;
+
+// Ast nodes can be placed in a linked list
+#define KPL_AST_HEADER(STRUCT_NAME) \
+ KPL_OBJECT_ALLOCATOR_HEADER(STRUCT_NAME); \
+ struct STRUCT_NAME *ast_prev, *ast_next
+
+/*
+ * All ast nodes inherit this, use meta to cast to node type
+ */
+typedef struct _kpl_ast_base {
+ KPL_AST_HEADER(_kpl_ast_base);
+} kpl_ast_base;
+
+/*
+ * Use object_flags field because the ast does not use the garbage collector
+ */
+typedef struct {
+ kpl_ast_kind kind : 4;
+ uint16_t info : 12; // meaning of this changes based on kind
+} kpl_ast_meta;
+
+#define KPL_AST_META_KIND_BIT_WIDTH 4
+
+#define KPL_AST_META_KIND_BIT_MASK 15
+
+/*
+ * Load metadata stored in the header object_flags
+ */
+inline kpl_ast_meta kpl_ast_meta_get(void *ast) {
+ kpl_ast_base *ast_base = ast;
+ uint16_t object_flags = ast_base->object_flags;
+ kpl_ast_meta ast_meta = {};
+ ast_meta.kind = object_flags & KPL_AST_META_KIND_BIT_MASK;
+ ast_meta.info = object_flags >> KPL_AST_META_KIND_BIT_WIDTH;
+ return ast_meta;
+}
+
+/*
+ * Store metadata in the header object_flags
+ */
+inline void kpl_ast_meta_set(void *ast, kpl_ast_meta ast_meta) {
+ kpl_ast_base *ast_base = ast;
+ ast_base->object_flags = 0;
+ ast_base->object_flags = ast_meta.info << KPL_AST_META_KIND_BIT_WIDTH;
+ ast_base->object_flags |= ast_meta.kind;
+}
+
+/*
+ * An identifier without its specification (var, arg, ref) in a lists scope
+ */
+typedef struct _kpl_ast_scope {
+ KPL_TREE_HEADER(_kpl_ast_scope);
+ uint32_t id; // unique id
+ kpl_type_base *type;
+ kpl_class class;
+ kpl_identifier identifier;
+} kpl_ast_scope;
+
+/*
+ * Stored on ast_meta.info
+ */
+typedef enum : uint8_t {
+ KPL_AST_LIST_KIND_BLOCK = 1,
+ KPL_AST_LIST_KIND_COLLECTION = 2,
+ KPL_AST_LIST_KIND_MODIFIERS = 3
+} kpl_ast_list_kind;
+
+typedef struct _kpl_ast_function kpl_ast_function;
+
+/*
+ * A "list" of statements
+ * Only block lists have their own scope
+ */
+typedef struct _kpl_ast_list {
+ KPL_AST_HEADER(_kpl_ast_list);
+ struct _kpl_ast_list *parent_list;
+ kpl_ast_function *function_parent; // owns this list
+ kpl_ast_base *list_head, *list_tail;
+ kpl_tree scope_tree;
+} kpl_ast_list;
+
+typedef struct _kpl_ast_coroutine kpl_ast_coroutine;
+
+/*
+ * Synchronous code
+ */
+typedef struct _kpl_ast_function {
+ KPL_AST_HEADER(_kpl_ast_function);
+ kpl_ast_coroutine *coroutine_parent; // owns this function
+ kpl_ast_list *statements;
+} kpl_ast_function;
+
+/*
+ * A "list" of functions
+ * Coroutine is synchronous if function_head == function_tail
+ * Synchronous coroutines can be called asynchronously, but not vice versa
+ */
+typedef struct _kpl_ast_coroutine {
+ KPL_AST_HEADER(_kpl_ast_coroutine);
+ kpl_ast_coroutine *parent_coroutine;
+ kpl_type_coroutine *type;
+ kpl_ast_function *function_head, *function_tail;
+} kpl_ast_coroutine;
+
+/*
+ * Value or collection
+ */
+typedef struct _kpl_ast_data {
+ KPL_AST_HEADER(_kpl_ast_data);
+ kpl_type_base *type;
+ kpl_class class;
+} kpl_ast_data;
+
+/*
+ * Stored on ast_meta.info
+ */
+typedef enum : uint16_t { // 12 bit limit
+ KPL_AST_IDENTIFIER_KIND_FLAG_EMPTY = 0,
+ KPL_AST_IDENTIFIER_KIND_VARIABLE = 1 << 0,
+ KPL_AST_IDENTIFIER_KIND_ARGUMENT = 1 << 1,
+ KPL_AST_IDENTIFIER_KIND_REFERENCE = 1 << 2,
+ KPL_AST_IDENTIFIER_FLAG_NOT_NULL = 1 << 3 // Flow typing nullable type has been checked
+} kpl_ast_identifier_kind_flags;
+
+/*
+ * Represents a variable, argument or ref
+ */
+typedef struct _kpl_ast_identifier {
+ KPL_AST_HEADER(_kpl_ast_identifier);
+ kpl_ast_scope *scope;
+} kpl_ast_identifier;
+
+/*
+ * Represents a type
+ * Types are left associative operators, they cast data and define arguments
+ * Cannot define variables
+ */
+typedef struct _kpl_ast_type {
+ KPL_AST_HEADER(_kpl_ast_type);
+ kpl_type_base *type;
+ kpl_ast_base *ast_right; // the ast node this type is casting/defining
+} kpl_ast_type;
+
+/*
+ * Represents an operator
+ * All operators return a Result
+ * The operator name is stored on ast_meta.info
+ */
+typedef struct _kpl_ast_operator {
+ KPL_AST_HEADER(_kpl_ast_operator);
+ kpl_type_result *type;
+ kpl_ast_base *ast_children[2]; // left 0, right 1
+} kpl_ast_operator;
+
+/*
+ * Stored on ast_meta.info
+ */
+typedef enum : uint8_t {
+ KPL_AST_ACTION_KIND_IF = 1,
+ KPL_AST_ACTION_KIND_LOOP = 2,
+ KPL_AST_ACTION_KIND_MATCH = 3
+} kpl_ast_action_kind;
+
+/*
+ * Action, if, loop, match
+ * All actions return a Result
+ */
+typedef struct _kpl_ast_action {
+ KPL_AST_HEADER(_kpl_ast_action);
+ kpl_type_result *type;
+ kpl_ast_list *condition_list, *body_list;
+} kpl_ast_action;
diff --git a/include/kpl/class.h b/include/kpl/class.h
index d1c836e..8cb05da 100644
--- a/include/kpl/class.h
+++ b/include/kpl/class.h
@@ -19,11 +19,17 @@ typedef void kpl_interface_native_free_fn(kpl_object_native *object_native);
*/
typedef void kpl_interface_log_fn(kpl_thread *thread, void *object);
+/*
+ * `copy operator
+ */
+typedef kpl_value64 kpl_interface_copy_fn(kpl_thread *thread_from, kpl_thread *thread_to, kpl_value64 object);
+
typedef struct {
// TODO
kpl_interface_gc_mark_object_fn *gc_mark_object_fn;
kpl_interface_native_free_fn *native_free_fn;
kpl_interface_log_fn *log_fn;
+ kpl_interface_copy_fn *copy_fn;
} kpl_interface;
typedef struct {
diff --git a/include/kpl/env.h b/include/kpl/env.h
index 11f90b3..c4237fa 100644
--- a/include/kpl/env.h
+++ b/include/kpl/env.h
@@ -34,7 +34,8 @@ typedef enum : uint32_t {
KPL_ENV_FLAG_HAS_ERROR = 1 << 1,
KPL_ENV_FLAG_RUN_MODULE = 1 << 2,
KPL_ENV_FLAG_RUN_EVAL = 1 << 3,
- KPL_ENV_FLAG_RUN_REPL = 1 << 4
+ KPL_ENV_FLAG_RUN_REPL = 1 << 4,
+ KPL_ENV_FLAG_RUN_FUNCTION = 1 << 5
} kpl_env_flags;
/*
@@ -50,7 +51,10 @@ typedef struct _kpl_env {
_Atomic uint32_t flags; // see kpl_env_flags for values, any thread can modify or check needs atomic
int32_t io_uring_queue_size_pow2; // the power of two to calculate the entries in the io_uring queue
int32_t thread_count; // -1 updates this value to max possible, 0 invalid, >= 1 for specific number
- const char *kpl_entry; // file or string to be ran, not used for repl, pointer used to get kpl args namespace
+ union {
+ const char *kpl_c_string_entry; // file or string, not used for repl, pointer used in kpl args namespace
+ kpl_task_fn *kpl_fn_entry; // a task function, for a task with no variables, placed on main thread
+ }; // only one entry point can be defined
_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
kpl_tree namespace_tree, module_tree; // Trees of kpl_storage
@@ -71,11 +75,6 @@ kpl_exit_code kpl_env_config(kpl_env *env, int32_t argc, char *argv[]);
*/
kpl_exit_code kpl_env_init(kpl_env *env);
-typedef enum : uint8_t {
- KPL_ENV_QUEUE_MODE_ASYNC = 0,
- KPL_ENV_QUEUE_MODE_SYNC = 1
-} kpl_env_queue_mode;
-
/*
* Increase priority by one
*/
@@ -100,13 +99,17 @@ inline void kpl_env_priority_decrement(kpl_thread *thread) {
thread->env->total_priority--;
}
+typedef enum : uint8_t {
+ KPL_ENV_QUEUE_MODE_ASYNC = 0,
+ KPL_ENV_QUEUE_MODE_SYNC = 1
+} kpl_env_queue_mode;
+
/*
* queue_mode ASYNC queues thread with lowest priority
* queue_mode SYNC queues thread on threads[task->thread_id]
*/
void kpl_env_queue_task(kpl_env *env, kpl_object_task *task, kpl_env_queue_mode queue_mode);
-
/*
* Run once, not re-enetrant
* TODO RUN TASK AND IO ORDERING
diff --git a/include/kpl/memory.h b/include/kpl/memory.h
index 3bb4871..5056483 100644
--- a/include/kpl/memory.h
+++ b/include/kpl/memory.h
@@ -60,7 +60,7 @@ void *kpl_memory_allocator_allocate(kpl_memory_allocator *memory_allocator, int3
/*
* Only called though gc_new
- * All allocations must extend KPL_OBJECT_GC_HEADER
+ * All allocations must extend KPL_OBJECT_ALLOCATOR_HEADER
*/
void *kpl_memory_allocator_allocate_gc(kpl_memory_allocator *memory_allocator, uint16_t thread_id);
diff --git a/include/kpl/object.h b/include/kpl/object.h
index 933b459..5566b00 100644
--- a/include/kpl/object.h
+++ b/include/kpl/object.h
@@ -13,14 +13,14 @@ typedef enum : uint16_t {
// for atomic linked list queue
// size in bytes, index on the kpl_threads array
// Flags should only be set by owner thread
-#define KPL_OBJECT_GC_HEADER(STRUCT_NAME) \
+#define KPL_OBJECT_ALLOCATOR_HEADER(STRUCT_NAME) \
struct STRUCT_NAME *_Atomic queue_next; \
int32_t object_size; \
uint16_t thread_id, object_flags;
// Each object can access the object_gc it is a child of
#define KPL_OBJECT_HEADER(STRUCT_NAME) \
- KPL_OBJECT_GC_HEADER(STRUCT_NAME); \
+ KPL_OBJECT_ALLOCATOR_HEADER(STRUCT_NAME); \
kpl_object_gc *object_gc_parent;
/*
@@ -71,7 +71,7 @@ void *kpl_object_queue_dequeue(kpl_object_queue *object_queue);
* Any thread can mark
*/
typedef struct _kpl_object_gc {
- KPL_OBJECT_GC_HEADER(_kpl_object_gc);
+ KPL_OBJECT_ALLOCATOR_HEADER(_kpl_object_gc);
struct _kpl_object_gc *gc_prev, *gc_next;
_Atomic bool gc_mark __attribute((aligned(8)));
kpl_class class;
@@ -160,7 +160,7 @@ typedef struct _kpl_object_error {
int32_t error_line;
} kpl_object_error;
-typedef kpl_object_error *kpl_task_fn(kpl_thread *thread, kpl_object_task *task);
+typedef void kpl_task_fn(kpl_thread *thread, kpl_object_task *object_task);
/*
* Max variables per task, a bit mask used to check if the variable can be garbage collected
diff --git a/include/kpl/operator.h b/include/kpl/operator.h
new file mode 100644
index 0000000..7912ff7
--- /dev/null
+++ b/include/kpl/operator.h
@@ -0,0 +1,13 @@
+/*
+ * KPL Operator names
+ * AST and IR use same enum
+ */
+
+#pragma once
+
+/*
+ * Limited to 12 bits for AST, 0 is invalid
+ */
+typedef enum : uint16_t {
+ KPL_OPERATOR_NAME_INVALID,
+} kpl_operator_name;
diff --git a/include/kpl/storage.h b/include/kpl/storage.h
index 8758624..5b0e13a 100644
--- a/include/kpl/storage.h
+++ b/include/kpl/storage.h
@@ -23,14 +23,14 @@ typedef struct _kpl_storgae_object {
KPL_TREE_HEADER(STRUCT_NAME); \
bool import_ready; \
kpl_tree object_tree; \
- const char *c_string_ptr;
+ 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_STORAGE_HEADER(_kpl_storage_namespace);
} kpl_stoage_namespace;
/*
@@ -44,6 +44,6 @@ typedef kpl_stoage_namespace kpl_stoage_search;
* This tree can only be accessed through a mutex
*/
typedef struct _kpl_storage_module {
- KPL_TREE_HEADER(_kpl_storage_module);
+ KPL_STORAGE_HEADER(_kpl_storage_module);
char c_string_array[]; // null term, c_string_ptr points to this
} kpl_storage_module;
diff --git a/include/kpl/task.h b/include/kpl/task.h
index bfdbb4a..ce183da 100644
--- a/include/kpl/task.h
+++ b/include/kpl/task.h
@@ -32,3 +32,8 @@ inline void kpl_object_task_join(kpl_thread *thread, kpl_object_task *restrict o
object_task_complete->join_task = object_task_join;
kpl_object_task_done(thread, object_task_complete);
}
+
+/*
+ * An error has occurred in the task
+ */
+void kpl_object_task_error(kpl_thread *thread, kpl_object_error *object_error);
diff --git a/include/kpl/type.h b/include/kpl/type.h
index 7b93e6f..91c3bd4 100644
--- a/include/kpl/type.h
+++ b/include/kpl/type.h
@@ -1,6 +1,7 @@
/*
* KPL Types
* A Type is a declaration of code and data
+ * Types can be shared across threads
*/
#pragma once
@@ -58,6 +59,7 @@ typedef enum : uint8_t {
KPL_TYPE_KIND_NATIVE, // keeps interface pointer
KPL_TYPE_KIND_ARRAY, // type wraps an unknown number of types
KPL_TYPE_KIND_TREE, // tree of type/type or identifier/type
+ KPL_TYPE_KIND_RESULT, // union of array and tree for tags
KPL_TYPE_KIND_COROUTINE // coroutine type, tree + array
} kpl_type_kind;
@@ -71,6 +73,7 @@ typedef enum : uint16_t {
} kpl_type_flags;
// type_kind is for struct polymorphism
+// types are in the gc cannot use object_flags for meta
#define KPL_TYPE_HEADER(STRUCT_NAME) \
KPL_OBJECT_HEADER(STRUCT_NAME); \
kpl_type_name type_name; \
@@ -129,17 +132,48 @@ typedef struct _kpl_type_tree_item_identifier {
typedef struct _kpl_type_tree {
KPL_TYPE_HEADER(_kpl_type_tree);
uint16_t tag_counter;
- kpl_tree tree;
+ kpl_tree tag_tree;
} kpl_type_tree;
/*
+ * Reserved ids for result types
+ * For result_types tag_counter has to be initialized to 3
+ * New tags cannot use these ids
+ */
+typedef enum : uint8_t {
+ KPL_TYPE_RESULT_ID_DEFAULT = 0, // .default can be any type
+ KPL_TYPE_RESULT_ID_ERROR = 1, // .error must be error type
+ KPL_TYPE_RESULT_ID_DONE = 2 // .done must be void
+} kpL_type_result_ids;
+
+/*
+ * Most of the time we can use default_error and map all .default and .error tags to this array
+ * .done does not need a type it is always void
+ * If more tags are added move .default, .error (.done if applicable) to the tree and add additional tags to tree
+ */
+typedef union {
+ kpl_type_base *default_error[2]; // used if tag counter == 3
+ kpl_tree tree; // tag counter > 3
+} kpl_type_result_union;
+
+/*
+ * KPL result type
+ */
+typedef struct _kpl_type_result {
+ KPL_TYPE_HEADER(_kpl_type_result);
+ uint16_t tag_counter;
+ kpl_type_result_union result;
+} kpl_type_result;
+
+/*
* Coroutine Type
* Arguments size is known at creation and does not grow
* Arguments size can be zero
+ * Can be cased to kpl_type_result when working with results
*/
typedef struct _kpl_type_coroutine {
KPL_TYPE_HEADER(_kpl_type_coroutine);
- uint16_t arguments_size, tag_counter; // unique id generator
- kpl_tree result_tree;
+ uint16_t tag_counter, arguments_size; // unique id generator
+ kpl_type_result_union result;
kpl_type_base *arguments[];
} kpl_type_coroutine;