-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathothm_symbols.h
317 lines (283 loc) · 11.7 KB
/
othm_symbols.h
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* This file is part of Othm_symbols.
*
* Othm_symbols is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Othm_symbols is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with Ruspma. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OTHM_SYMBOLS_H
#define OTHM_SYMBOLS_H
#include <othm_hashmap.h>
#define OTHMSYMBOLSTRUCT(SYMBOL) ((struct othm_symbol_struct *) (SYMBOL))
#define OTHM_SYMBOL_INIT(NAME) \
struct othm_symbol_struct OTHM_SYMBOL_SYMBOL ## NAME = { \
.request.data = #NAME, \
.request.data_size = sizeof(#NAME), \
.request.key_type = othm_symbol_symbol_key_type, \
.request.check_key = othm_symbol_pointer_compare \
}; \
struct othm_symbol_struct * \
OTHM_SYMBOL_SYMBOL ## NAME ## REF = \
&OTHM_SYMBOL_SYMBOL ## NAME
#define OTHM_SYMBOL_INIT_TAGGED_LEFT(NAME, LTAG_TYPE, ...) \
struct { \
LTAG_TYPE ltag; \
struct othm_symbol_struct symbol; \
} OTHM_SYMBOL_SYMBOL ## NAME ## LTAGGED = { \
.symbol.request.data = #NAME, \
.symbol.request.data_size = sizeof(#NAME), \
.symbol.request.key_type = othm_symbol_symbol_key_type, \
.symbol.request.check_key = othm_symbol_pointer_compare, \
__VA_ARGS__ \
}; \
struct othm_symbol_struct * \
OTHM_SYMBOL_SYMBOL ## NAME ## REF = \
&OTHM_SYMBOL_SYMBOL ## NAME ## LTAGGED.symbol
#define OTHM_SYMBOL_INIT_TAGGED_RIGHT(NAME, RTAG_TYPE, ...) \
struct { \
struct othm_symbol_struct symbol; \
RTAG_TYPE rtag; \
} OTHM_SYMBOL_SYMBOL ## NAME ## RTAGGED = { \
.symbol.request.data = #NAME, \
.symbol.request.data_size = sizeof(#NAME), \
.symbol.request.key_type = othm_symbol_symbol_key_type, \
.symbol.request.check_key = othm_symbol_pointer_compare, \
__VA_ARGS__ \
}; \
struct othm_symbol_struct * \
OTHM_SYMBOL_SYMBOL ## NAME ## REF = \
&OTHM_SYMBOL_SYMBOL ## NAME ## RTAGGED.symbol
#define OTHM_SYMBOL_INIT_TAGGED_BOTH(NAME, LTAG_TYPE, RTAG_TYPE, ...) \
struct { \
LTAG_TYPE ltag; \
struct othm_symbol_struct symbol; \
RTAG_TYPE rtag; \
} OTHM_SYMBOL_SYMBOL ## NAME ## BTAGGED = { \
.symbol.request.data = #NAME, \
.symbol.request.data_size = sizeof(#NAME), \
.symbol.request.key_type = othm_symbol_symbol_key_type, \
.symbol.request.check_key = othm_symbol_pointer_compare, \
__VA_ARGS__ \
}; \
struct othm_symbol_struct * \
OTHM_SYMBOL_SYMBOL ## NAME ## REF = \
&OTHM_SYMBOL_SYMBOL ## NAME ## BTAGGED.symbol
#define OTHM_KEYWORD_INIT(NAME) \
struct othm_symbol_struct OTHM_SYMBOL_KEYWORD ## NAME = { \
.request.data = #NAME, \
.request.data_size = sizeof(#NAME), \
.request.key_type = othm_symbol_keyword_key_type, \
.request.check_key = othm_symbol_pointer_compare \
}; \
struct othm_symbol_struct * \
OTHM_SYMBOL_KEYWORD ## NAME ## REF = \
&OTHM_SYMBOL_KEYWORD ## NAME
#define OTHM_KEYWORD_INIT_TAGGED_LEFT(NAME, LTAG_TYPE, ...) \
struct { \
LTAG_TYPE ltag; \
struct othm_symbol_struct symbol; \
} OTHM_SYMBOL_KEYWORD ## NAME ## LTAGGED = { \
.symbol.request.data = #NAME, \
.symbol.request.data_size = sizeof(#NAME), \
.symbol.request.key_type = othm_symbol_keyword_key_type, \
.symbol.request.check_key = othm_symbol_pointer_compare, \
__VA_ARGS__ \
}; \
struct othm_symbol_struct * \
OTHM_SYMBOL_KEYWORD ## NAME ## REF = \
&OTHM_SYMBOL_KEYWORD ## NAME ## LTAGGED.symbol
#define OTHM_KEYWORD_INIT_TAGGED_RIGHT(NAME, RTAG_TYPE, ...) \
struct { \
struct othm_symbol_struct symbol; \
RTAG_TYPE rtag; \
} OTHM_SYMBOL_KEYWORD ## NAME ## RTAGGED = { \
.symbol.request.data = #NAME, \
.symbol.request.data_size = sizeof(#NAME), \
.symbol.request.key_type = othm_symbol_keyword_key_type, \
.symbol.request.check_key = othm_symbol_pointer_compare, \
__VA_ARGS__ \
}; \
struct othm_symbol_struct * \
OTHM_SYMBOL_KEYWORD ## NAME ## REF = \
&OTHM_SYMBOL_KEYWORD ## NAME ## RTAGGED.symbol
#define OTHM_KEYWORD_INIT_TAGGED_BOTH(NAME, LTAG_TYPE, RTAG_TYPE, ...) \
struct { \
LTAG_TYPE ltag; \
struct othm_symbol_struct symbol; \
RTAG_TYPE rtag; \
} OTHM_SYMBOL_KEYWORD ## NAME ## BTAGGED = { \
.symbol.request.data = #NAME, \
.symbol.request.data_size = sizeof(#NAME), \
.symbol.request.key_type = othm_symbol_keyword_key_type, \
.symbol.request.check_key = othm_symbol_pointer_compare, \
__VA_ARGS__ \
}; \
struct othm_symbol_struct * \
OTHM_SYMBOL_KEYWORD ## NAME ## REF = \
&OTHM_SYMBOL_KEYWORD ## NAME ## BTAGGED.symbol
/* NOTE, this uses function declarations that are not prototypes, it
will work with any standard C compiler, but might return warnings! */
#define OTHM_PRIM_FUNCT_INIT(NAME, PRIM_FUNCT, RETURN_TYPE) \
RETURN_TYPE PRIM_FUNCT (); \
struct othm_funct OTHM_SYMBOL_PRIM_OTHM_FUNCT ## NAME = { \
.function = (void (*) (void)) PRIM_FUNCT, \
.name = #NAME \
}; \
struct othm_symbol_struct OTHM_SYMBOL_PRIM_FUNCT ## NAME = { \
.request.data = &OTHM_SYMBOL_PRIM_OTHM_FUNCT ## NAME, \
.request.data_size = sizeof(struct othm_funct), \
.request.key_type = othm_symbol_funct_key_type, \
.request.check_key = othm_symbol_pointer_compare \
}; \
struct othm_symbol_struct * \
OTHM_SYMBOL_PRIM_FUNCT ## NAME ## REF = \
&OTHM_SYMBOL_PRIM_FUNCT ## NAME
#define OTHM_PRIM_FUNCT_INIT_TAGGED_LEFT(NAME, PRIM_FUNCT, RETURN_TYPE, \
LTAG_TYPE, ...) \
RETURN_TYPE PRIM_FUNCT (); \
struct othm_funct OTHM_SYMBOL_PRIM_OTHM_FUNCT ## NAME = { \
.function = (void (*) (void)) PRIM_FUNCT, \
.name = #NAME \
}; \
struct { \
LTAG_TYPE ltag; \
struct othm_symbol_struct symbol; \
} OTHM_SYMBOL_PRIM_FUNCT ## NAME ## LTAGGED = { \
.symbol.request.data = \
&OTHM_SYMBOL_PRIM_OTHM_FUNCT ## NAME, \
.symbol.request.data_size = sizeof(struct othm_funct), \
.symbol.request.key_type = othm_symbol_funct_key_type, \
.symbol.request.check_key = othm_symbol_pointer_compare, \
__VA_ARGS__ \
}; \
struct othm_symbol_struct * \
OTHM_SYMBOL_PRIM_FUNCT ## NAME ## REF = \
&OTHM_SYMBOL_PRIM_FUNCT ## NAME ##LTAGGED.symbol
#define OTHM_PRIM_FUNCT_INIT_TAGGED_RIGHT(NAME, PRIM_FUNCT, RETURN_TYPE, \
RTAG_TYPE, ...) \
RETURN_TYPE PRIM_FUNCT (); \
struct othm_funct OTHM_SYMBOL_PRIM_OTHM_FUNCT ## NAME = { \
.function = (void (*) (void)) PRIM_FUNCT, \
.name = #NAME \
}; \
struct { \
struct othm_symbol_struct symbol; \
RTAG_TYPE rtag; \
} OTHM_SYMBOL_PRIM_FUNCT ## NAME ## RTAGGED = { \
.symbol.request.data = \
&OTHM_SYMBOL_PRIM_OTHM_FUNCT ## NAME, \
.symbol.request.data_size = sizeof(struct othm_funct), \
.symbol.request.key_type = othm_symbol_funct_key_type, \
.symbol.request.check_key = othm_symbol_pointer_compare, \
__VA_ARGS__ \
}; \
struct othm_symbol_struct * \
OTHM_SYMBOL_PRIM_FUNCT ## NAME ## REF = \
&OTHM_SYMBOL_PRIM_FUNCT ## NAME ##RTAGGED.symbol
#define OTHM_PRIM_FUNCT_INIT_TAGGED_BOTH(NAME, PRIM_FUNCT, RETURN_TYPE, \
LTAG_TYPE, RTAG_TYPE, ...) \
RETURN_TYPE PRIM_FUNCT (); \
struct othm_funct OTHM_SYMBOL_PRIM_OTHM_FUNCT ## NAME = { \
.function = (void (*) (void)) PRIM_FUNCT, \
.name = #NAME \
}; \
struct { \
LTAG_TYPE ltag; \
struct othm_symbol_struct symbol; \
RTAG_TYPE rtag; \
} OTHM_SYMBOL_PRIM_FUNCT ## NAME ## BTAGGED = { \
.symbol.request.data = \
&OTHM_SYMBOL_PRIM_OTHM_FUNCT ## NAME, \
.symbol.request.data_size = sizeof(struct othm_funct), \
.symbol.request.key_type = othm_symbol_funct_key_type, \
.symbol.request.check_key = othm_symbol_pointer_compare, \
__VA_ARGS__ \
}; \
struct othm_symbol_struct * \
OTHM_SYMBOL_PRIM_FUNCT ## NAME ## REF = \
&OTHM_SYMBOL_PRIM_FUNCT ## NAME ##BTAGGED.symbol
#define OTHM_SYMBOL_EXPORT(NAME) \
extern struct othm_symbol_struct * \
OTHM_SYMBOL_SYMBOL ## NAME ## REF
#define OTHM_KEYWORD_EXPORT(NAME) \
extern struct othm_symbol_struct * \
OTHM_SYMBOL_KEYWORD ## NAME ## REF
#define OTHM_PRIM_FUNCT_EXPORT(NAME) \
extern struct othm_symbol_struct * \
OTHM_SYMBOL_PRIM_FUNCT ## NAME ## REF
#define OTHM_SYMBOL(NAME) (OTHM_SYMBOL_SYMBOL ## NAME ## REF)
#define OTHM_KEYWORD(NAME) (OTHM_SYMBOL_KEYWORD ## NAME ## REF)
#define OTHM_PRIM_FUNCT(NAME) \
(OTHM_SYMBOL_PRIM_FUNCT ## NAME ## REF)
#define OTHM_SYMBOL_STR_NAME_L(NAME) \
((char *)(OTHM_SYMBOL(NAME))->request.data)
#define OTHM_KEYWORD_STR_NAME_L(NAME) \
((char *)(OTHM_KEYWORD(NAME))->request.data)
#define OTHM_PRIM_FUNCT_STR_NAME_L(NAME) \
((char *) \
((struct othm_funct *) (OTHM_PRIM_FUNCT(NAME))->request.data) \
->name)
#define OTHM_SYMBOL_STR_NAME(SYMBOL) \
((char *)(SYMBOL)->request.data)
#define OTHM_KEYWORD_STR_NAME(KEYWORD) \
((char *)(KEYWORD)->request.data)
#define OTHM_PRIM_FUNCT_STR_NAME(PRIM_FUNCT) \
((char *) \
((struct othm_funct *) (PRIM_FUNCT)->request.data) \
->name)
#define OTHM_SYMBOL_ALLOW_AT_RUNTIME(HASHMAP, NAME) \
othm_symbol_allow_at_runtime(HASHMAP, OTHM_SYMBOL(NAME))
#define OTHM_KEYWORD_ALLOW_AT_RUNTIME(HASHMAP, NAME) \
othm_symbol_allow_at_runtime(HASHMAP, OTHM_KEYWORD(NAME))
#define OTHM_PRIM_FUNCT_ALLOW_AT_RUNTIME(HASHMAP, NAME) \
othm_symbol_allow_at_runtime(HASHMAP, OTHM_PRIM_FUNCT(NAME))
/* This is a macro that takes a othm function symbol, a type for
the symbol, and some values to apply the symbol on. It generates
the an identifier for the symbol OTHM_SYMBOL_PRIM_FUNCT ## NAME
then it gets the data in the request (which is an othm_funct), this
requires converting the .data from void* to struct othm_funct*
next it casts the function in the stuct othm_funct* to the given
type before calling it on the params. It is not too dificult to
figure out, but I dout many C programmers understand the preprocessor
as well as they should. Seriously, learn the preprocessor!
*/
#define OTHM_PRIM_FUNCT_APPLY_L(NAME, FUNCTION_TYPE_CAST, PARAMS) \
((FUNCTION_TYPE_CAST) \
(((struct othm_funct *) \
OTHM_SYMBOL_PRIM_FUNCT ## NAME ## REF->request.data) \
->function))PARAMS
#define OTHM_PRIM_FUNCT_APPLY(PRIM_FUNCT, FUNCTION_TYPE_CAST, PARAMS) \
((FUNCTION_TYPE_CAST) \
(((struct othm_funct *) \
(PRIM_FUNCT).request.data) \
->function))PARAMS
#define OTHM_PRIM_FUNCT_GET(PRIM_FUNCT, FUNCTION_TYPE_CAST) \
((FUNCTION_TYPE_CAST) \
(((struct othm_funct *) \
(PRIM_FUNCT)->request.data) \
->function))
struct othm_symbol_struct {
struct othm_request request;
};
extern char othm_symbol_symbol_key_type[];
extern char othm_symbol_keyword_key_type[];
extern char othm_symbol_funct_key_type[];
/* extern struct othm_hashmap *othm_global_symbol_map; */
void othm_symbol_allow_at_runtime(struct othm_hashmap *hashmap,
struct othm_symbol_struct *symbol);
int othm_symbol_pointer_compare(void *storage, void *data);
int othm_symbol_string_compare(void *storage, void *data);
struct othm_symbol_struct *othm_symbol_get_from_string(struct othm_hashmap *hashmap,
char *name);
struct othm_symbol_struct *othm_keyword_get_from_string(struct othm_hashmap *hashmap,
char *name);
void othm_symbol_print(struct othm_symbol_struct *symbol);
#endif