-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
ape.h
266 lines (215 loc) · 11.6 KB
/
ape.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
/*
SPDX-License-Identifier: MIT
ape
https://github.com/kgabis/ape
Copyright (c) 2023 Krzysztof Gabis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef ape_h
#define ape_h
#ifdef __cplusplus
extern "C"
{
#endif
#if 0
} // unconfuse xcode
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#ifdef _MSC_VER
#define __attribute__(x)
#endif
#define APE_VERSION_MAJOR 0
#define APE_VERSION_MINOR 15
#define APE_VERSION_PATCH 0
#define APE_VERSION_STRING "0.15.0"
typedef struct ape ape_t;
typedef struct ape_object { uint64_t _internal; } ape_object_t;
typedef struct ape_error ape_error_t;
typedef struct ape_program ape_program_t;
typedef struct ape_traceback ape_traceback_t;
typedef enum ape_error_type {
APE_ERROR_NONE = 0,
APE_ERROR_PARSING,
APE_ERROR_COMPILATION,
APE_ERROR_RUNTIME,
APE_ERROR_TIMEOUT,
APE_ERROR_ALLOCATION,
APE_ERROR_USER, // from ape_add_error() or ape_add_errorf()
} ape_error_type_t;
typedef enum ape_object_type {
APE_OBJECT_NONE = 0,
APE_OBJECT_ERROR = 1 << 0,
APE_OBJECT_NUMBER = 1 << 1,
APE_OBJECT_BOOL = 1 << 2,
APE_OBJECT_STRING = 1 << 3,
APE_OBJECT_NULL = 1 << 4,
APE_OBJECT_NATIVE_FUNCTION = 1 << 5,
APE_OBJECT_ARRAY = 1 << 6,
APE_OBJECT_MAP = 1 << 7,
APE_OBJECT_FUNCTION = 1 << 8,
APE_OBJECT_EXTERNAL = 1 << 9,
APE_OBJECT_FREED = 1 << 10,
APE_OBJECT_ANY = 0xffff, // for checking types with &
} ape_object_type_t;
typedef ape_object_t (*ape_native_fn)(ape_t *ape, void *data, int argc, ape_object_t *args);
typedef void* (*ape_malloc_fn)(void *ctx, size_t size);
typedef void (*ape_free_fn)(void *ctx, void *ptr);
typedef void (*ape_data_destroy_fn)(void* data);
typedef void* (*ape_data_copy_fn)(void* data);
typedef size_t (*ape_stdout_write_fn)(void* context, const void *data, size_t data_size);
typedef char* (*ape_read_file_fn)(void* context, const char *path);
typedef size_t (*ape_write_file_fn)(void* context, const char *path, const char *string, size_t string_size);
//-----------------------------------------------------------------------------
// Ape API
//-----------------------------------------------------------------------------
ape_t* ape_make(void);
ape_t* ape_make_ex(ape_malloc_fn malloc_fn, ape_free_fn free_fn, void *ctx);
void ape_destroy(ape_t *ape);
void ape_free_allocated(ape_t *ape, void *ptr);
void ape_set_repl_mode(ape_t *ape, bool enabled);
// -1 to disable, returns false if it can't be set for current platform (otherwise true).
// If execution time exceeds given limit an APE_ERROR_TIMEOUT error is set.
// Precision is not guaranteed because time can't be checked every VM tick
// but expect it to be submilisecond.
bool ape_set_timeout(ape_t *ape, double max_execution_time_ms);
void ape_set_stdout_write_function(ape_t *ape, ape_stdout_write_fn stdout_write, void *context);
void ape_set_file_write_function(ape_t *ape, ape_write_file_fn file_write, void *context);
void ape_set_file_read_function(ape_t *ape, ape_read_file_fn file_read, void *context);
ape_program_t* ape_compile(ape_t *ape, const char *code);
ape_program_t* ape_compile_file(ape_t *ape, const char *path);
ape_object_t ape_execute_program(ape_t *ape, const ape_program_t *program);
void ape_program_destroy(ape_program_t *program);
ape_object_t ape_execute(ape_t *ape, const char *code);
ape_object_t ape_execute_file(ape_t *ape, const char *path);
ape_object_t ape_call(ape_t *ape, const char *function_name, int argc, ape_object_t *args);
#define APE_CALL(ape, function_name, ...) \
ape_call(\
(ape),\
(function_name),\
sizeof((ape_object_t[]){__VA_ARGS__}) / sizeof(ape_object_t),\
(ape_object_t[]){__VA_ARGS__})
void ape_set_runtime_error(ape_t *ape, const char *message);
void ape_set_runtime_errorf(ape_t *ape, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
bool ape_has_errors(const ape_t *ape);
int ape_errors_count(const ape_t *ape);
void ape_clear_errors(ape_t *ape);
const ape_error_t* ape_get_error(const ape_t *ape, int index);
bool ape_set_native_function(ape_t *ape, const char *name, ape_native_fn fn, void *data);
bool ape_set_global_constant(ape_t *ape, const char *name, ape_object_t obj);
ape_object_t ape_get_object(ape_t *ape, const char *name);
bool ape_check_args(ape_t *ape, bool generate_error, int argc, ape_object_t *args, int expected_argc, int *expected_types);
#define APE_CHECK_ARGS(ape, generate_error, argc, args, ...)\
ape_check_args(\
(ape),\
(generate_error),\
(argc),\
(args),\
sizeof((int[]){__VA_ARGS__}) / sizeof(int),\
(int[]){__VA_ARGS__})
//-----------------------------------------------------------------------------
// Ape object
//-----------------------------------------------------------------------------
ape_object_t ape_object_make_number(double val);
ape_object_t ape_object_make_bool(bool val);
ape_object_t ape_object_make_null(void);
ape_object_t ape_object_make_string(ape_t *ape, const char *str);
ape_object_t ape_object_make_stringf(ape_t *ape, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
ape_object_t ape_object_make_array(ape_t *ape);
ape_object_t ape_object_make_map(ape_t *ape);
ape_object_t ape_object_make_native_function(ape_t *ape, ape_native_fn fn, void *data);
ape_object_t ape_object_make_error(ape_t *ape, const char *message);
ape_object_t ape_object_make_errorf(ape_t *ape, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
ape_object_t ape_object_make_external(ape_t *ape, void *data);
char* ape_object_serialize(ape_t *ape, ape_object_t obj);
bool ape_object_disable_gc(ape_object_t obj);
void ape_object_enable_gc(ape_object_t obj);
bool ape_object_equals(ape_object_t a, ape_object_t b);
bool ape_object_is_null(ape_object_t obj);
ape_object_t ape_object_copy(ape_object_t obj);
ape_object_t ape_object_deep_copy(ape_object_t obj);
ape_object_type_t ape_object_get_type(ape_object_t obj);
const char* ape_object_get_type_string(ape_object_t obj);
const char* ape_object_get_type_name(ape_object_type_t type);
double ape_object_get_number(ape_object_t obj);
bool ape_object_get_bool(ape_object_t obj);
const char * ape_object_get_string(ape_object_t obj);
const char* ape_object_get_error_message(ape_object_t obj);
const ape_traceback_t* ape_object_get_error_traceback(ape_object_t obj);
bool ape_object_set_external_destroy_function(ape_object_t object, ape_data_destroy_fn destroy_fn);
bool ape_object_set_external_copy_function(ape_object_t object, ape_data_copy_fn copy_fn);
//-----------------------------------------------------------------------------
// Ape object array
//-----------------------------------------------------------------------------
int ape_object_get_array_length(ape_object_t obj);
ape_object_t ape_object_get_array_value(ape_object_t object, int ix);
const char* ape_object_get_array_string(ape_object_t object, int ix);
double ape_object_get_array_number(ape_object_t object, int ix);
bool ape_object_get_array_bool(ape_object_t object, int ix);
bool ape_object_set_array_value(ape_object_t object, int ix, ape_object_t value);
bool ape_object_set_array_string(ape_object_t object, int ix, const char *string);
bool ape_object_set_array_number(ape_object_t object, int ix, double number);
bool ape_object_set_array_bool(ape_object_t object, int ix, bool value);
bool ape_object_add_array_value(ape_object_t object, ape_object_t value);
bool ape_object_add_array_string(ape_object_t object, const char *string);
bool ape_object_add_array_number(ape_object_t object, double number);
bool ape_object_add_array_bool(ape_object_t object, bool value);
//-----------------------------------------------------------------------------
// Ape object map
//-----------------------------------------------------------------------------
int ape_object_get_map_length(ape_object_t obj);
ape_object_t ape_object_get_map_key_at(ape_object_t object, int ix);
ape_object_t ape_object_get_map_value_at(ape_object_t object, int ix);
bool ape_object_set_map_value_at(ape_object_t object, int ix, ape_object_t val);
bool ape_object_set_map_value_with_value_key(ape_object_t object, ape_object_t key, ape_object_t value);
bool ape_object_set_map_value(ape_object_t object, const char *key, ape_object_t value);
bool ape_object_set_map_string(ape_object_t object, const char *key, const char *string);
bool ape_object_set_map_number(ape_object_t object, const char *key, double number);
bool ape_object_set_map_bool(ape_object_t object, const char *key, bool value);
ape_object_t ape_object_get_map_value_with_value_key(ape_object_t object, ape_object_t key);
ape_object_t ape_object_get_map_value(ape_object_t object, const char *key);
const char* ape_object_get_map_string(ape_object_t object, const char *key);
double ape_object_get_map_number(ape_object_t object, const char *key);
bool ape_object_get_map_bool(ape_object_t object, const char *key);
bool ape_object_map_has_key(ape_object_t object, const char *key);
//-----------------------------------------------------------------------------
// Ape error
//-----------------------------------------------------------------------------
const char* ape_error_get_message(const ape_error_t *error);
const char* ape_error_get_filepath(const ape_error_t *error);
const char* ape_error_get_line(const ape_error_t *error);
int ape_error_get_line_number(const ape_error_t *error);
int ape_error_get_column_number(const ape_error_t *error);
ape_error_type_t ape_error_get_type(const ape_error_t *error);
const char* ape_error_get_type_string(const ape_error_t *error);
const char* ape_error_type_to_string(ape_error_type_t type);
char* ape_error_serialize(ape_t *ape, const ape_error_t *error);
const ape_traceback_t* ape_error_get_traceback(const ape_error_t *error);
//-----------------------------------------------------------------------------
// Ape traceback
//-----------------------------------------------------------------------------
int ape_traceback_get_depth(const ape_traceback_t *traceback);
const char* ape_traceback_get_filepath(const ape_traceback_t *traceback, int depth);
const char* ape_traceback_get_line(const ape_traceback_t *traceback, int depth);
int ape_traceback_get_line_number(const ape_traceback_t *traceback, int depth);
int ape_traceback_get_column_number(const ape_traceback_t *traceback, int depth);
const char* ape_traceback_get_function_name(const ape_traceback_t *traceback, int depth);
#ifdef __cplusplus
}
#endif
#endif /* ape_h */