summaryrefslogtreecommitdiff
path: root/src/honeysuckle.h
blob: f54a2617c71d88116b55a0533a09149e7d0553bf (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
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
#ifndef HONEYSUCKLE_H
#define HONEYSUCKLE_H

#include <stdbool.h>

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#include "va_nargs.h"

/* type constants */
typedef enum
  { HS_BOOL,
    HS_INT,
    HS_NUM,
    HS_STR,
    HS_TBL,
    HS_FUNC,
    HS_CFUNC,
    HS_USER,
    HS_LIGHT,
    HS_NIL,
    HS_ANY,
  } hs_type;

const char* hs_type_to_string(hs_type type);

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * hs_parse_args and hs_parse_overloaded
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

struct hs_arg {
  hs_type type;
  union {
    bool *boolean;
    lua_Integer *integer;
    lua_Number *number;
    char **string;
    int *stack_index;
    lua_CFunction *function;
    void **userdata;
  } ptr;
};

#define hs_bool(x)  { .type=HS_BOOL,  .ptr.boolean = &(x) }
#define hs_int(x)   { .type=HS_INT,   .ptr.integer = &(x) }
#define hs_num(x)   { .type=HS_NUM,   .ptr.number = &(x) }
#define hs_str(x)   { .type=HS_STR,   .ptr.string = &(x) }
#define hs_tbl(x)   { .type=HS_TBL,   .ptr.stack_index = &(x) }
#define hs_func(x)  { .type=HS_FUNC,  .ptr.stack_index = &(x) }
#define hs_cfunc(x) { .type=HS_CFUNC, .ptr.function = &(x) }
#define hs_user(x)  { .type=HS_USER,  .ptr.userdata = &(x) }
#define hs_light(x) { .type=HS_LIGHT, .ptr.userdata = &(x) }
#define hs_nil(x)   { .type=HS_NIL,   .ptr.stack_index = &(x) }
#define hs_any(x)   { .type=HS_ANY,   .ptr.stack_index = &(x) }

void hs_parse_args_(lua_State *L, int n_args, struct hs_arg *arguments);

#define hs_parse_args(L, ...)				\
   hs_parse_args_(L,					\
		  VA_NARGS(__VA_ARGS__)/2,		\
		 (struct hs_arg[]) { __VA_ARGS__ })


#define hs_overload(...) VA_NARGS(__VA_ARGS__)/2, (struct hs_arg[]) { __VA_ARGS__ }

int hs_parse_overloaded_(lua_State *L, ...);

#define hs_parse_overloaded(L, ...)		\
   hs_parse_overloaded_(L, __VA_ARGS__, -1)

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * hs_create_table
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

struct hs_tbl_entry {
   hs_type key_type;
   union {
      bool boolean;
      lua_Integer integer;
      lua_Number number;
      char *string;
      int stack_index;
      lua_CFunction function;
      void *userdata;
   } key;
   hs_type value_type;
   union {
      bool boolean;
      lua_Integer integer;
      lua_Number number;
      char *string;
      int stack_index;
      lua_CFunction function;
      void *userdata;
   } value;
};

#define hs_bool_bool(key, value)				\
   { HS_BOOL, .key.boolean=key, HS_BOOL, .value.boolean=value }
#define hs_bool_int(key, value)					\
   { HS_BOOL, .key.boolean=key, HS_INT, .value.integer=value }
#define hs_bool_num(key, value)					\
   { HS_BOOL, .key.boolean=key, HS_NUM, .value.number=value }
#define hs_bool_str(key, value)					\
   { HS_BOOL, .key.boolean=key, HS_STR, .value.string=value }
#define hs_bool_tbl(key, value)						\
   { HS_BOOL, .key.boolean=key, HS_TBL, .value.stack_index=value }
#define hs_bool_func(key, value)					\
   { HS_BOOL, .key.boolean=key, HS_FUNC, .value.stack_index=value }
#define hs_bool_cfunc(key, value)					\
   { HS_BOOL, .key.boolean=key, HS_CFUNC, .value.function=value }
#define hs_bool_user(key, value)					\
   { HS_BOOL, .key.boolean=key, HS_USER, .value.stack_index=value }
#define hs_bool_light(key, value)					\
   { HS_BOOL, .key.boolean=key, HS_LIGHT, .value.userdata=value }

#define hs_int_bool(key, value)					\
   { HS_INT, .key.integer=key, HS_BOOL, .value.boolean=value }
#define hs_int_int(key, value)					\
   { HS_INT, .key.integer=key, HS_INT, .value.integer=value }
#define hs_int_num(key, value)					\
   { HS_INT, .key.integer=key, HS_NUM, .value.number=value }
#define hs_int_str(key, value)					\
   { HS_INT, .key.integer=key, HS_STR, .value.string=value }
#define hs_int_tbl(key, value)						\
   { HS_INT, .key.integer=key, HS_TBL, .value.stack_index=value }
#define hs_int_func(key, value)						\
   { HS_INT, .key.integer=key, HS_FUNC, .value.stack_index=value }
#define hs_int_cfunc(key, value)					\
   { HS_INT, .key.integer=key, HS_CFUNC, .value.function=value }
#define hs_int_user(key, value)						\
   { HS_INT, .key.integer=key, HS_USER, .value.stack_index=value }
#define hs_int_light(key, value)					\
   { HS_INT, .key.integer=key, HS_LIGHT, .value.userdata=value }
   
#define hs_num_bool(key, value)					\
   { HS_NUM, .key.number=key, HS_BOOL, .value.boolean=value }
#define hs_num_int(key, value)					\
   { HS_NUM, .key.number=key, HS_INT, .value.integer=value }
#define hs_num_num(key, value)					\
   { HS_NUM, .key.number=key, HS_NUM, .value.number=value }
#define hs_num_str(key, value)					\
   { HS_NUM, .key.number=key, HS_STR, .value.string=value }
#define hs_num_tbl(key, value)						\
   { HS_NUM, .key.number=key, HS_TBL, .value.stack_index=value }
#define hs_num_func(key, value)						\
   { HS_NUM, .key.number=key, HS_FUNC, .value.stack_index=value }
#define hs_num_cfunc(key, value)				\
   { HS_NUM, .key.number=key, HS_CFUNC, .value.function=value }
#define hs_num_user(key, value)						\
   { HS_NUM, .key.number=key, HS_USER, .value.stack_index=value }
#define hs_num_light(key, value)				\
   { HS_NUM, .key.number=key, HS_LIGHT, .value.userdata=value }

#define hs_tbl_bool(key, value)						\
   { HS_TBL, .key.stack_index=key, HS_BOOL, .value.boolean=value }
#define hs_tbl_int(key, value)						\
   { HS_TBL, .key.stack_index=key, HS_INT, .value.integer=value }
#define hs_tbl_num(key, value)						\
   { HS_TBL, .key.stack_index=key, HS_NUM, .value.number=value }
#define hs_tbl_str(key, value)						\
   { HS_TBL, .key.stack_index=key, HS_STR, .value.string=value }
#define hs_tbl_tbl(key, value)						\
   { HS_TBL, .key.stack_index=key, HS_TBL, .value.stack_index=value }
#define hs_tbl_func(key, value)						\
   { HS_TBL, .key.stack_index=key, HS_FUNC, .value.stack_index=value }
#define hs_tbl_cfunc(key, value)					\
   { HS_TBL, .key.stack_index=key, HS_CFUNC, .value.function=value }
#define hs_tbl_user(key, value)						\
   { HS_TBL, .key.stack_index=key, HS_USER, .value.stack_index=value }
#define hs_tbl_light(key, value)					\
   { HS_TBL, .key.stack_index=key, HS_LIGHT, .value.userdata=value }

#define hs_func_bool(key, value)					\
   { HS_FUNC, .key.stack_index=key, HS_BOOL, .value.boolean=value }
#define hs_func_int(key, value)						\
   { HS_FUNC, .key.stack_index=key, HS_INT, .value.integer=value }
#define hs_func_num(key, value)						\
   { HS_FUNC, .key.stack_index=key, HS_NUM, .value.number=value }
#define hs_func_str(key, value)						\
   { HS_FUNC, .key.stack_index=key, HS_STR, .value.string=value }
#define hs_func_tbl(key, value)						\
   { HS_FUNC, .key.stack_index=key, HS_TBL, .value.stack_index=value }
#define hs_func_func(key, value)					\
   { HS_FUNC, .key.stack_index=key, HS_FUNC, .value.stack_index=value }
#define hs_func_cfunc(key, value)					\
   { HS_FUNC, .key.stack_index=key, HS_CFUNC, .value.function=value }
#define hs_func_user(key, value)					\
   { HS_FUNC, .key.stack_index=key, HS_USER, .value.stack_index=value }
#define hs_func_light(key, value)					\
   { HS_FUNC, .key.stack_index=key, HS_LIGHT, .value.userdata=value }

#define hs_cfunc_bool(key, value)					\
   { HS_CFUNC, .key.function=key, HS_BOOL, .value.boolean=value }
#define hs_cfunc_int(key, value)					\
   { HS_CFUNC, .key.function=key, HS_INT, .value.integer=value }
#define hs_cfunc_num(key, value)				\
   { HS_CFUNC, .key.function=key, HS_NUM, .value.number=value }
#define hs_cfunc_str(key, value)				\
   { HS_CFUNC, .key.function=key, HS_STR, .value.string=value }
#define hs_cfunc_tbl(key, value)					\
   { HS_CFUNC, .key.function=key, HS_TBL, .value.stack_index=value }
#define hs_cfunc_func(key, value)					\
   { HS_CFUNC, .key.function=key, HS_FUNC, .value.stack_index=value }
#define hs_cfunc_cfunc(key, value)					\
   { HS_CFUNC, .key.function=key, HS_CFUNC, .value.function=value }
#define hs_cfunc_user(key, value)					\
   { HS_CFUNC, .key.function=key, HS_USER, .value.stack_index=value }
#define hs_cfunc_light(key, value)					\
   { HS_CFUNC, .key.function=key, HS_LIGHT, .value.userdata=value }
   
#define hs_user_bool(key, value)					\
   { HS_USER, .key.stack_index=key, HS_BOOL, .value.boolean=value }
#define hs_user_int(key, value)						\
   { HS_USER, .key.stack_index=key, HS_INT, .value.integer=value }
#define hs_user_num(key, value)						\
   { HS_USER, .key.stack_index=key, HS_NUM, .value.number=value }
#define hs_user_str(key, value)						\
   { HS_USER, .key.stack_index=key, HS_STR, .value.string=value }
#define hs_user_tbl(key, value)						\
   { HS_USER, .key.stack_index=key, HS_TBL, .value.stack_index=value }
#define hs_user_func(key, value)					\
   { HS_USER, .key.stack_index=key, HS_FUNC, .value.stack_index=value }
#define hs_user_cfunc(key, value)					\
   { HS_USER, .key.stack_index=key, HS_CFUNC, .value.function=value }
#define hs_user_user(key, value)					\
   { HS_USER, .key.stack_index=key, HS_USER, .value.stack_index=value }
#define hs_user_light(key, value)					\
   { HS_USER, .key.stack_index=key, HS_LIGHT, .value.userdata=value }

#define hs_light_bool(key, value)					\
   { HS_LIGHT, .key.userdata=key, HS_BOOL, .value.boolean=value }
#define hs_light_int(key, value)					\
   { HS_LIGHT, .key.userdata=key, HS_INT, .value.integer=value }
#define hs_light_num(key, value)				\
   { HS_LIGHT, .key.userdata=key, HS_NUM, .value.number=value }
#define hs_light_str(key, value)				\
   { HS_LIGHT, .key.userdata=key, HS_STR, .value.string=value }
#define hs_light_tbl(key, value)					\
   { HS_LIGHT, .key.userdata=key, HS_TBL, .value.stack_index=value }
#define hs_light_func(key, value)					\
   { HS_LIGHT, .key.userdata=key, HS_FUNC, .value.stack_index=value }
#define hs_light_cfunc(key, value)					\
   { HS_LIGHT, .key.userdata=key, HS_CFUNC, .value.function=value }
#define hs_light_user(key, value)					\
   { HS_LIGHT, .key.userdata=key, HS_USER, .value.stack_index=value }
#define hs_light_light(key, value)					\
   { HS_LIGHT, .key.userdata=key, HS_LIGHT, .value.userdata=value }
   
int hs_create_table_(lua_State *L, int n_elements, struct hs_tbl_entry *elements);

#define hs_create_table(L, ...)					\
   hs_create_table_(L,						\
		    VA_NARGS(__VA_ARGS__)/4,			\
		    (struct hs_tbl_entry[]) { __VA_ARGS__ })


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * hs_process_table
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

void hs_process_table(lua_State *L, int table_index, void *data, ...);
// default processors
void hs_pt_set_boolean(bool value, void *data);
void hs_pt_set_integer(lua_Integer value, void *data);
void hs_pt_set_number(lua_Number value, void *data);
void hs_pt_set_string(const char *value, void *data);


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * hs_pushstring
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

void hs_vpushstring(lua_State *L, const char *format_string, va_list args);
void hs_pushstring(lua_State *L, const char *format_string, ...);


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * error creation and handling
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

void hs_throw_error(lua_State *L, const char *format_string, ...);
int hs_traceback(lua_State *L);
int hs_call(lua_State *L, int nargs, int nret);


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * registry operations
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

#define hs_rstore(L) luaL_ref(L, LUA_REGISTRYINDEX);
#define hs_rload(L, ref) lua_rawgeti(L, LUA_REGISTRYINDEX, ref)
#define hs_rdel(L, ref) luaL_unref(L, LUA_REGISTRYINDEX, ref)

#endif