aboutsummaryrefslogtreecommitdiff
path: root/include/kpl/type.h
blob: 7b93e6ff2030ccc969719743dde376ae99cf284f (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
/*
 * KPL Types
 * A Type is a declaration of code and data
 */

#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_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
#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 tree;
} kpl_type_tree;

/*
 * Coroutine Type
 * Arguments size is known at creation and does not grow
 * Arguments size can be zero
 */
typedef struct _kpl_type_coroutine {
    KPL_TYPE_HEADER(_kpl_type_coroutine);
    uint16_t arguments_size, tag_counter; // unique id generator
    kpl_tree result_tree;
    kpl_type_base *arguments[];
} kpl_type_coroutine;