aboutsummaryrefslogtreecommitdiff
path: root/include/kpl/type.h
blob: 91c3bd41ed35097916a11ac3e5dddf057dc37809 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * KPL Types
 * A Type is a declaration of code and data
 * Types can be shared across threads
 */

#pragma once

typedef enum : uint8_t {
    KPL_TYPE_NAME_INVALID,
    // 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_TUPLE,
    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,
    // DEFINITIONS
    KPL_TYPE_NAME_ALIAS,
    KPL_TYPE_NAME_UNIQUE,
    // ENUM COUNT
    KPL_TYPE_NAME_ENUM_COUNT
} kpl_type_name;

const char *kpl_type_name_c_string(kpl_type_name type_name);

/*
 * The c struct for this type
 */
typedef enum : uint8_t {
    KPL_TYPE_KIND_INVALID,
    KPL_TYPE_KIND_BASE, // has no additional type information
    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;

kpl_type_kind kpl_type_name_get_kind(kpl_type_name type_name);

typedef enum : uint16_t {
    KPL_TYPE_FLAG_EMPTY             = 0,
    KPL_TYPE_FLAG_NULLABLE          = 1 << 0,
    KPL_TYPE_FLAG_TREE_TYPE         = 1 << 1,
    KPL_TYPE_FLAG_TREE_IDENTIFIER   = 1 << 2
} 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; \
    kpl_type_kind type_kind; \
    uint16_t type_flags

/*
 * Type has no additional information
 */
typedef struct _kpl_type_base {
    KPL_TYPE_HEADER(_kpl_type_base);
} kpl_type_base;

/*
 * Keeps interface pointer for its type comparison
 */
typedef struct _kpl_type_native {
    KPL_TYPE_HEADER(_kpl_type_native);
    kpl_interface *interface;
} kpl_type_native;

/*
 * Type wraps an unknown number of types
 * Array does not grow or shrink
 * Minimum array size is 1
 */
typedef struct _kpl_type_array {
    KPL_TYPE_HEADER(_kpl_type_array);
    uint16_t array_size;
    kpl_type_base *array[];
} kpl_type_array;

/*
 * Tree based on matching types
 */
typedef struct _kpl_type_tree_item_type {
    KPL_TREE_HEADER(_kpl_type_tree_item_type);
    uint16_t tag; // unique id
    kpl_type_base *type;
} kpl_type_tree_item_type;

/*
 * Tree based on matching identifiers
 */
typedef struct _kpl_type_tree_item_identifier {
    KPL_TREE_HEADER(_kpl_type_tree_item_identifier);
    uint16_t tag; // unique id for the item
    kpl_type_base *type;
    kpl_identifier identifier;
} kpl_type_tree_item_identifier;

/*
 * Tree of types
 * Tag counter provides a unique tag to each new inserted item
 */
typedef struct _kpl_type_tree {
    KPL_TYPE_HEADER(_kpl_type_tree);
    uint16_t tag_counter;
    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 tag_counter, arguments_size; // unique id generator
    kpl_type_result_union result;
    kpl_type_base *arguments[];
} kpl_type_coroutine;