aboutsummaryrefslogtreecommitdiff
path: root/include/kpl/ast.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/kpl/ast.h')
-rw-r--r--include/kpl/ast.h187
1 files changed, 187 insertions, 0 deletions
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;