It's Sizzling & Delicious

Calc Example using SWI-Prolog and C++

I wrote the calc example and confirm that it works. It can be found here. I performed some changes to it since I am not running from the command line.

First I’ll show you the main.cpp

#include <iostream>
#include <SWI-Prolog.h>

using namespace std;

int main(void){

 static char * av[] = {"calc.pl", NULL};
 if( ! PL_initialise(1,av)){
 cout<<"error initializing"<<endl;
 PL_halt(1);
 }else {
 cout<<"success initializing!"<<endl;
 }

 predicate_t pred = PL_predicate("calc",1,"user");
 term_t h0 = PL_new_term_refs(1);

 int rval;
 char * expression = "pi/2";
 PL_put_atom_chars(h0,expression);
 rval = PL_call_predicate(NULL, PL_Q_NORMAL, pred, h0);
 PL_halt( rval ? 0 : 1 );

 return 0;
}

Secondly I’ll show you calc.pl. It is a prolog file.

calc(Atom) :-
 term_to_atom(Expr, Atom),
 A is Expr,
 write(A),
 nl.

I’ll admit that’s pretty straight forward! :)

Advertisement

Comments are closed.

Follow

Get every new post delivered to your Inbox.