-
Notifications
You must be signed in to change notification settings - Fork 14
/
yasl_aux.h
224 lines (199 loc) · 8.42 KB
/
yasl_aux.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
#ifndef YASL_YASL_AUX_H_
#define YASL_YASL_AUX_H_
/**
* This file defines common wrappers around many common functions from yasl.h
* It doesn't add any new functionality, just helps reduce boilerplate.
*/
#include "yasl.h"
/**
* Loads all standard libraries into the appropriate state and declares them all with their default names.
* @param S The state onto which to load the libraries.
* @return YASL_SUCESS on success, else error code.
*/
int YASLX_decllibs(struct YASL_State *S);
/**
* [-1, +0]
* Declares a global and initializes it with the top value from the stack.
* @param S the YASL_State.
* @param name name of the global (nul-terminated).
*/
void YASLX_initglobal(struct YASL_State *S, const char *name);
/**
* [-0, +0]
* Prints an error message for bad value.
* @param S The YASL State.
* @param fmt a format string, taking the same parameters as printf.
* @param ... var args for the above format string.
*/
#define YASLX_print_err_value(S, ...) YASL_print_err(S, MSG_VALUE_ERROR __VA_ARGS__)
/**
* [-0, +0]
* Prints an error message for bad argument type.
* @param S The YASL State.
* @param fn_name name of the function in which the error occured.
* @param position which arg had the wrong type.
* @param exp expected type of the arg.
* @param act actual type of the arg.
*/
void YASLX_print_err_bad_arg_type(struct YASL_State *S,
const char *const fn_name,
int position,
const char *const exp,
const char *const act);
/**
* [-0, +0]
* Prints an error message
* @param S The YASL State.
* @param fn_name name of the function in which the error occurred.
* @param position which arg had the wrong type.
* @param exp expected type of the arg.
*/
void YASLX_print_err_bad_arg_type_n(struct YASL_State *S,
const char *const fn_name,
unsigned position,
const char *exp);
/**
* [-0, +0]
* Causes a fatal value error.
* @param S the YASL_State in which the error occurred.
*/
YASL_NORETURN void YASLX_throw_err_value(struct YASL_State *S);
/**
* [-0, +0]
* Causes a fatal value error.
* @param S the YASL_State in which the error occurred.
*/
YASL_NORETURN void YASLX_throw_err_type(struct YASL_State *S);
/**
* [-0, +0]
* Prints an error message, then causes a fatal error.
* @param S the YASL State in which the error occurred.
*/
#define YASLX_print_and_throw_err_value(S, ...) do {\
YASLX_print_err_value(S, __VA_ARGS__);\
YASLX_throw_err_value(S);\
} while (0)
/**
* [-0, +0]
* Prints an error message, then causes a fatal error.
* @param S the YASL State in which the error occurred.
*/
YASL_NORETURN void YASLX_print_and_throw_err_bad_arg_type_n(struct YASL_State *S, const char *fn, unsigned n, const char *exp);
/**
* [-0, +0]
* Returns the nth position of the stack if it is an int. Otherwise, causes a type error,
* along with a printed error message.
* @param S The YASL_State.
* @param name Name of the function in which this is called, used for error message.
* @param pos the position of the argument.
* @return the nth position of the stack if it's an int, otherwise no return.
*/
yasl_int YASLX_checknint(struct YASL_State *S, const char *name, unsigned n);
/**
* [-0, +0]
* Returns the nth position of the stack if it is a float. Otherwise, causes a type error, along with a printed
* error message.
* @param S The YASL_State.
* @param name Name of the function in which this is called, used for error message.
* @param pos the position of the argument.
* @return the nth position of the stack if it's a float, otherwise no return.
*/
yasl_float YASLX_checknfloat(struct YASL_State *S, const char *name, unsigned n);
/**
* [-0, +0]
* Returns the nth position of the stack if it is a bool. Otherwise, causes a type error, along with a printed
* error message.
* @param S The YASL_State.
* @param name Name of the function in which this is called, used for error message.
* @param pos the position of the argument.
* @return the nth position of the stack if it's a bool, otherwise no return.
*/
bool YASLX_checknbool(struct YASL_State *S, const char *name, unsigned n);
/**
* [-0, +0]
* Checks that the nth position of the stack if it is undef. Otherwise, causes a type error, along with a printed
* error message.
* @param S The YASL_State.
* @param name Name of the function in which this is called, used for error message.
* @param pos the position of the argument.
*/
void YASLX_checknundef(struct YASL_State *S, const char *name, unsigned n);
/**
* [-0, +0]
* Returns the nth position of the stack if it is a str. Otherwise, causes a type error, along with a printed
* error message.
* @param S The YASL_State.
* @param name Name of the function in which this is called, used for error message.
* @param pos the position of the argument.
* @outparam len the length of the returned str.
* @return the nth position of the stack if it's a str, otherwise no return.
*/
const char *YASLX_checknstr(struct YASL_State *S, const char *name, unsigned n, size_t *len);
/**
* [-0, +0]
* Returns the nth position of the stack if it is a userdata with a matching tag. Otherwise, causes a type error,
* along with a printed error message.
* @param S The YASL_State.
* @param tag The tag of the userdata.
* @param name Name of the function in which this is called, used for error message.
* @param n the position of the argument, used only for the error message.
* @return the nth position of the stack if it's a userdata, otherwise no return.
*/
void *YASLX_checknuserdata(struct YASL_State *S, const char *tag, const char *name, unsigned n);
/**
* [-0, +0]
* Returns the nth position of the stack if it is an int, or default_val if the nth position is undef.
* Otherwise, causes a type error, along with a printed error message.
* @param S The YASL_State.
* @param name Name of the function in which this is called, used for error message.
* @param n the position of the argument.
* @param default_val the value to return if the nth position of the stack is undef.
* @return the nth position of the stack or a default value if it's an int or undef, otherwise no return.
*/
yasl_int YASLX_checknoptint(struct YASL_State *S, const char *name, unsigned n, yasl_int default_val);
/**
* [-0, +0]
* Returns the nth position of the stack if it is a float, or default_val if the nth position is undef.
* Otherwise, causes a type error, along with a printed error message.
* @param S The YASL_State.
* @param name Name of the function in which this is called, used for error message.
* @param n the position of the argument.
* @param default_val the value to return if the nth position of the stack is undef.
* @return the nth position of the stack or a default value if it's a float or undef, otherwise no return.
*/
yasl_float YASLX_checknoptfloat(struct YASL_State *S, const char *name, unsigned n, yasl_float default_val);
/**
* [-0, +0]
* Returns the nth position of the stack if it is a bool, or default_val if the nth position is undef.
* Otherwise, causes a type error, along with a printed error message.
* @param S The YASL_State.
* @param name Name of the function in which this is called, used for error message.
* @param n the position of the argument.
* @param default_val the value to return if the nth position of the stack is undef.
* @return the nth position of the stack or a default value if it's a bool or undef, otherwise no return.
*/
bool YASLX_checknoptbool(struct YASL_State *S, const char *name, unsigned n, bool default_val);
/**
* [-0, +0]
* Returns the nth position of the stack if it is a str, or default_val if the nth position is undef.
* Otherwise, causes a type errorm along with a printed error message.
* @param S The YASL_State
* @param name Name of the calling function, used for error message.
* @param n the position of the argument.
* @param len the length of the returned str.
* @param default_val the str to return in the case that the nth position of the stack is undef. NUL-terminated.
* @return the nth position of the stack or a default value if it's a str or undef, otherwise no return.
*/
const char *YASLX_checknoptstrz(struct YASL_State *S, const char *name, unsigned n, size_t *len, const char *default_val);
struct YASLX_function {
const char *name;
YASL_cfn fn;
int args;
};
/**
* Inserts all functions in the array into a table on top of the stack.
* @param S The YASL_State
* @param functions array of function names, function pointers, and number of args.
*/
void YASLX_tablesetfunctions(struct YASL_State *S, const struct YASLX_function functions[]);
#endif