-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
33 lines (26 loc) · 1.18 KB
/
main.c
File metadata and controls
33 lines (26 loc) · 1.18 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
#include <SWI-Prolog.h>
#include <stdio.h>
int main(int argc, char **argv) {
// Initialize prolog
PL_initialise(1, (char*[]){ argv[0]} );
// Create modules m1 and m2
module_t m1 = PL_new_module(PL_new_atom("module_1"));
module_t m2 = PL_new_module(PL_new_atom("module_2"));
// Fetch and store m2's name inside m2_name_t (re-using the atom created in
// the line above gives the same result, this is just to show how the error
// actually happens in my real code)
atom_t m2_name_a = PL_module_name(m2);
term_t m2_name_t = PL_new_term_ref();
PL_put_atom(m2_name_t, m2_name_a);
// Create a term for "use_module(m2_name_t)"
term_t use_module_f = PL_new_functor(PL_new_atom("use_module"), 1);
term_t use_module_t = PL_new_term_ref();
PL_cons_functor(use_module_t, use_module_f, m2_name_t);
// Call "use_module(m2_name_t)" inside m1
PL_call(use_module_t, m1);
// Fetch exception generated by PL_call
char *exception_msg;
term_t exception = PL_exception(0);
PL_get_chars(exception, &exception_msg, CVT_WRITE | REP_UTF8 | BUF_MALLOC);
puts(exception_msg); // prints "error(existence_error(source_sink,module_2),_)"
}