-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscryer_prolog.h
More file actions
456 lines (419 loc) · 12.7 KB
/
scryer_prolog.h
File metadata and controls
456 lines (419 loc) · 12.7 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef enum scryer_Error {
SCRYER_ERROR_SUCCESS,
SCRYER_ERROR_ERROR,
} scryer_Error;
typedef enum scryer_LeafAnswerKind {
SCRYER_LEAF_ANSWER_KIND_TRUE,
SCRYER_LEAF_ANSWER_KIND_FALSE,
SCRYER_LEAF_ANSWER_KIND_LEAF_ANSWER,
SCRYER_LEAF_ANSWER_KIND_EXCEPTION,
} scryer_LeafAnswerKind;
typedef enum scryer_TermKind {
SCRYER_TERM_KIND_INTEGER,
SCRYER_TERM_KIND_RATIONAL,
SCRYER_TERM_KIND_FLOAT,
SCRYER_TERM_KIND_ATOM,
SCRYER_TERM_KIND_STRING,
SCRYER_TERM_KIND_LIST,
SCRYER_TERM_KIND_COMPOUND,
SCRYER_TERM_KIND_VARIABLE,
} scryer_TermKind;
/**
* A dictionary of bindings in a leaf answer.
*/
typedef struct scryer_Bindings scryer_Bindings;
/**
* A leaf answer.
*/
typedef struct scryer_LeafAnswer scryer_LeafAnswer;
/**
* A Scryer Prolog instance.
*/
typedef struct scryer_Machine scryer_Machine;
/**
* A builder for a [`Machine`].
*/
typedef struct scryer_MachineBuilder scryer_MachineBuilder;
/**
* A handler for an in-progress query.
*
* It's parent [`Machine`] shouldn't be accessed while this isn't dropped.
*/
typedef struct scryer_QueryState scryer_QueryState;
/**
* A Prolog Term.
*/
typedef struct scryer_Term scryer_Term;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
/**
* Creates a [`MachineBuilder`] with the default options.
*/
struct scryer_MachineBuilder *scryer_machine_builder_new(void);
/**
* Drops a [`MachineBuilder`].
*
* Notice that this shouldn't be called if [`scryer_machine_builder_build`] is
* called, because the [`MachineBuilder`] gets consumed in that case.
*
* # Safety
*
* `machine_builder` should point to a [`MachineBuilder`] previously created
* with [`scryer_machine_builder_new`].
*/
void scryer_machine_builder_drop(struct scryer_MachineBuilder *machine_builder);
/**
* Creates a [`Machine`] from a [`MachineBuilder`].
*
* This consumes the [`MachineBuilder`], so you shouldn't call
* [`scryer_machine_builder_drop`] after.
*
* # Safety
*
* `machine_builder` should point to a [`MachineBuilder`] previously created
* with [`scryer_machine_builder_new`].
*/
struct scryer_Machine *scryer_machine_builder_build(struct scryer_MachineBuilder *machine_builder);
/**
* Drops a [`Machine`].
*
* # Safety
*
* `machine` should point to a [`Machine`] previously created with
* [`scryer_machine_builder_build`].
*/
void scryer_machine_drop(struct scryer_Machine *machine);
/**
* Run a query from a string.
*
* If no error occurs, `query_state` will be updated with a pointer to a
* [`QueryState`]. This [`Machine`] shoudn't be accessed again until that
* [`QueryState`] is dropped with `scryer_query_state_drop`.
*
* # Errors
*
* Currently this function can't error, but this will probably change.
*
* # Safety
*
* - `machine` should point to a [`Machine`] previously created with
* [`scryer_machine_builder_build`]. - `query` should be a null-terminated
* UTF-8 encoded string.
*/
enum scryer_Error scryer_machine_run_query(struct scryer_Machine *machine,
const char *query,
struct scryer_QueryState **query_state);
/**
* Consults a module from a string.
*
* # Errors
*
* Currently this function can't error, but this will probably change.
*
* # Safety
*
* - `machine` should point to a [`Machine`] previously created with
* [`scryer_machine_builder_build`]. - `module` and `program` should both be
* null-terminated UTF-8 encoded strings.
*/
enum scryer_Error scryer_machine_consult_module_string(struct scryer_Machine *machine,
const char *module,
const char *program);
/**
* Drops a [`QueryState`].
*
* # Safety
*
* `query_state` should point to a [`QueryState`] previously created with
* [`scryer_machine_run_query`].
*/
void scryer_query_state_drop(struct scryer_QueryState *query_state);
/**
* Get the next leaf answer from the query.
*
* If no error occurs, `leaf_answer` will be updated with a pointer to a
* [`LeafAnswer`].
*
* # Errors
*
* If an error occurs, then `leaf_answer` will be updated with a pointer to
* a [`LeafAnswer`] that contains the error term. It can be unwrapped with
* [`scryer_leaf_answer_unwrap_exception`].
*
* # Safety
*
* `query_state` should point to a [`QueryState`] previously created with
* [`scryer_machine_run_query`].
*/
enum scryer_Error scryer_query_state_next_answer(struct scryer_QueryState *query_state,
struct scryer_LeafAnswer **leaf_answer);
/**
* Drops a [`LeafAnswer`].
*
* # Safety
*
* `leaf_answer` should point to a [`LeafAnswer`] previously created with
* [`scryer_query_state_next_answer`].
*/
void scryer_leaf_answer_drop(struct scryer_LeafAnswer *leaf_answer);
/**
* Gets the kind of the [`LeafAnswer`].
*
* # Safety
*
* `leaf_answer` should point to a [`LeafAnswer`] previously created with
* [`scryer_query_state_next_answer`].
*/
enum scryer_LeafAnswerKind scryer_leaf_answer_kind(const struct scryer_LeafAnswer *leaf_answer);
/**
* Unwraps an exception term from a [`LeafAnswer`].
*
* On success updates `term` with a pointer to a [`Term`].
*
* # Errors
*
* If the `LeafAnswer` is not an exception, this returns [`Error::Error`] and
* updates `term` to a null pointer.
*
* # Safety
*
* `leaf_answer` should point to a [`LeafAnswer`] previously created with
* [`scryer_query_state_next_answer`].
*/
enum scryer_Error scryer_leaf_answer_unwrap_exception(const struct scryer_LeafAnswer *leaf_answer,
struct scryer_Term **term);
/**
* Unwraps the bindings from a [`LeafAnswer`].
*
* On success updates `bindings` with a pointer to a [`Bindings`].
*
* # Errors
*
* If the `LeafAnswer` is not a leaf answer (aka, it's an exception, true
* or false), this returns [`Error::Error`] and updates `bindings` to a null
* pointer.
*
* # Safety
*
* `leaf_answer` should point to a [`LeafAnswer`] previously created with
* [`scryer_query_state_next_answer`].
*/
enum scryer_Error scryer_leaf_answer_unwrap_bindings(const struct scryer_LeafAnswer *leaf_answer,
struct scryer_Bindings **bindings);
/**
* Drops a [`Bindings`].
*
* # Safety
*
* `bindings` should point to a [`bindings`] previously created with
* [`scryer_leaf_answer_unwrap_bindings`].
*/
void scryer_bindings_drop(struct scryer_Bindings *bindings);
/**
* Get the term bound to a variable in [`Bindings`].
*
* If the variable specified by `variable` exists in the bindings, succeeds and
* updates `term` with a pointer to a [`Term`].
*
* # Errors
*
* If the variable specified doesn't exist in the bindings, this returns
* [`Error::Error`] and updates `term` to a null pointer.
*
* # Safety
*
* - `variable` should be a null-terminated UTF-8 encoded string. -
* `bindings` should point to a [`bindings`] previously created with
* [`scryer_leaf_answer_unwrap_bindings`].
*/
enum scryer_Error scryer_bindings_get(const struct scryer_Bindings *bindings,
const char *variable,
struct scryer_Term **term);
/**
* Drops a [`Term`].
*
* # Safety
*
* `term` should point to a [`Term`] previously created by Scryer Prolog.
*/
void scryer_term_drop(struct scryer_Term *term);
/**
* Gets the kind of a [`Term`].
*
* # Safety
*
* `term` should point to a [`Term`] previously created by Scryer Prolog.
*/
enum scryer_TermKind scryer_term_kind(const struct scryer_Term *term);
/**
* Unwraps a big integer from a [`Term`].
*
* If `term` is an integer, succeeds and updates `big_integer` with a
* null-terminated string representing that integer. This is so that arbitrary
* precision can be supported. If you need an actual integer you should parse
* this string.
*
* # Errors
*
* If `term` is not an integer, returns [`Error::Error`] and updates
* `big_integer` to a null pointer.
*
* # Safety
*
* `term` should point to a [`Term`] previously created by Scryer Prolog.
*/
enum scryer_Error scryer_term_unwrap_integer(const struct scryer_Term *term, char **big_integer);
/**
* Unwraps a float from a [`Term`].
*
* If `term` is a float, succeeds and updates `scryer_float` with it's value.
*
* # Errors
*
* If `term` is not a float, returns [`Error::Error`] and updates
* `scryer_float` to `0.0`.
*
* # Safety
*
* `term` should point to a [`Term`] previously created by Scryer Prolog.
*/
enum scryer_Error scryer_term_unwrap_float(const struct scryer_Term *term, double *scryer_float);
/**
* Unwraps a rational from a [`Term`].
*
* If `term` is a rational, succeeds and updates `numerator` and
* `denominator` with a strings representing their values, like in
* [`scryer_term_unwrap_integer`].
*
* # Errors
*
* If `term` is not a rational, returns [`Error::Error`] and updates
* `numerator` and `denominator` to a null pointers.
*
* # Safety
*
* `term` should point to a [`Term`] previously created by Scryer Prolog.
*/
enum scryer_Error scryer_term_unwrap_rational(const struct scryer_Term *term,
char **numerator,
char **denominator);
/**
* Unwraps an atom from a [`Term`].
*
* If `term` is an atom, succeeds and updates `atom` with a null terminated
* string with it's contents.
*
* # Errors
*
* If `term` is not an atom, returns [`Error::Error`] and updates `atom` to a
* null pointer.
*
* # Safety
*
* `term` should point to a [`Term`] previously created by Scryer Prolog.
*/
enum scryer_Error scryer_term_unwrap_atom(const struct scryer_Term *term, char **atom);
/**
* Unwraps a string from a [`Term`].
*
* If `term` is a string, succeeds and updates `string` with a null terminated
* string with it's contents.
*
* # Errors
*
* If `term` is not a string, returns [`Error::Error`] and updates `string` to
* a null pointer.
*
* # Safety
*
* `term` should point to a [`Term`] previously created by Scryer Prolog.
*/
enum scryer_Error scryer_term_unwrap_string(const struct scryer_Term *term, char **string);
/**
* Unwraps a list from a [`Term`].
*
* If `term` is a list, succeeds and updates `term_list` with a pointer to a
* buffer containing pointers to terms, and `len` to the number of terms in
* that buffer.
*
* This buffer needs to be dropped with `scryer_list_drop`.
*
* # Errors
*
* If `term` is not a list, returns [`Error::Error`], updates `term_list` to a
* null pointer and `len` to 0.
*
* # Safety
*
* `term` should point to a [`Term`] previously created by Scryer Prolog.
*/
enum scryer_Error scryer_term_unwrap_list(const struct scryer_Term *term,
struct scryer_Term ***term_list,
uintptr_t *len);
/**
* Unwraps a compound from a [`Term`].
*
* If `term` is a compound, succeeds and updates `functor` with a null
* terminated string with the contents of the functor, `args` with a buffer
* with containing pointers to terms, and `len` to the number of terms in that
* buffer.
*
* `args` needs to be dropped with `scryer_list_drop`.
*
* # Errors
*
* If `term` is not a compound, returns [`Error::Error`], updates `functor` and
* `args` to a null pointers, and `len` to 0.
*
* # Safety
*
* `term` should point to a [`Term`] previously created by Scryer Prolog.
*/
enum scryer_Error scryer_term_unwrap_compound(const struct scryer_Term *term,
char **functor,
struct scryer_Term ***args,
uintptr_t *len);
/**
* Unwraps a variable from a [`Term`].
*
* If `term` is a variable, succeeds and updates `variable` with a null
* terminated variable with it's name.
*
* # Errors
*
* If `term` is not a variable, returns [`Error::Error`] and updates `variable`
* to a null pointer.
*
* # Safety
*
* `term` should point to a [`Term`] previously created by Scryer Prolog.
*/
enum scryer_Error scryer_term_unwrap_variable(const struct scryer_Term *term, char **variable);
/**
* Drop a previously allocated string.
*
* # Safety
*
* `string` should be a string previously allocated by Scryer Prolog.
*/
void scryer_string_drop(char *string);
/**
* Drop a previously allocated list.
*
* This only frees the memory for the list itself. The elements it contains
* should be dropped first separatelly.
*
* # Safety
*
* `list` should be a list previously created with [`scryer_term_unwrap_list`],
* and `len` should be it's length.
*/
void scryer_list_drop(struct scryer_Term **list, uintptr_t len);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus