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
180
181
182
183
184
185
186
187
188
189
190
191
192
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;
|