-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlikes.cpp
67 lines (50 loc) · 1.36 KB
/
likes.cpp
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
/* Part of SWI-Prolog
This example code is in the public domain
*/
#include <iostream>
#include <SWI-cpp2.h>
#include <SWI-cpp2.cpp>
using namespace std;
/* Usage:
likes prints who likes what
likes x prints what is liked by x
likes x y Test whether x likes y
likes -happy See who is happy
Compile using:
swipl-ld -o likes -ld g++ -goal true likes.cpp likes.pl
*/
int
body(int argc, char **argv)
{ if ( argc == 1 )
{ if ( strcmp(argv[0], "-happy") == 0 )
{ PlTermv av(1); /* likes -happy */
cout << "Happy people:" << endl;
PlQuery q("happy", av);
while( q.next_solution() )
cout << "\t" << av[0].as_string() << endl;
} else
{ PlTerm_var whom;
PlQuery q("likes", PlTermv(PlTerm_atom(argv[0]), whom));
cout << argv[0] << " likes:" << endl;
while( q.next_solution() )
cout << "\t" << whom.as_string() << endl;
}
} else if ( argc == 2 )
{ bool likes = PlCall("likes",
PlTermv(PlTerm_atom(argv[0]), PlTerm_atom(argv[1])));
cout << (likes ? "yes" : "no") << endl;
} else
cout << "Usage: likes x [y] or likes -happy" << endl;
return 0;
}
int
main(int argc, char **argv)
{ PlEngine e(argv[0]);
try
{ return body(argc-1, argv+1);
} catch ( const PlExceptionBase &ex )
{ cerr << "Exception thrown: " << ex.what() << endl;
exit(1);
}
return 0;
}