Re: another problem interfacing prolog and C
Daniel Diaz <[email protected]>
| Newsgroups | gmane.comp.gnu.prolog.general |
|---|---|
| Message-ID | <[email protected]> |
Hello Erick,
the problem comes from the fact you invoke Prolog from your call_c/2
foreign code. This one uses 2 arguments (a and _L) which are stored into
an array (accessible via macros X(0) and X(1)). But since write/1
itself is partly written in Prolog it also uses X(...) variables. So,
the clean way to fix it would be to inform the compiler a foreign code
calls Prolog (and thus the compiler would emit code to save/restore the
X(...) vars used).
For the moment you can do it by hand (okay it is a bit dirty) as follows:
#include "gprolog.h"
Bool
calling_c(PlTerm In,PlTerm* Out)
{
PlTerm arg[2]; /* reserve space to save args */
int i;
for(i=0;i<2;i++) /* save the 2 args */
arg[i]=X(1);
Pl_Query_Begin(TRUE);
Pl_Query_Call(Find_Atom("write"),1,&In);
Pl_Query_End(PL_RECOVER);
for(i=0;i<2;i++) /* restore them */
X(1)=arg[i];
*Out = Mk_Atom(atom_nil);
return TRUE; /* BTW: I prefer return TRUE; rather return PL_SUCCESS*/
}
In fact only X(1) (corresponding to _L) should be save since, at the
exit its result is unified (with _L) while X(0) (a) is no longer used.
Sorry for this limitation.
Erick Alphonse wrote:
> Hello,
>
> Still playing with the interface, I would like to call C from prolog and
> then from this C function call prolog back. Unfortunately, I can't get
> it working.
>
> % examp.pl
> :- initialization(top).
>
> :- foreign(call_c(+term,-term)).
>
> top :-
> call_c(a,_L).
>
> % examp_c.c
> #include <string.h>
> #include "gprolog.h"
>
> Bool
> call_c(PlTerm In,PlTerm* Out)
> {
> Pl_Query_Begin(TRUE);
> Pl_Query_Call(Find_Atom("write"),1,&In);
> Pl_Query_End(PL_RECOVER);
>
> *Out = Mk_Atom(atom_nil);
>
> return PL_SUCCESS;
> }
>
> And I get :
> 874# gplc examp.pl examp_c.c
> 875# ./examp
> warning: /home/alphonse/tmp/bug-prolog/examp.pl:1: user directive failed
> a
> GNU Prolog 1.2.18
> By Daniel Diaz
> Copyright (C) 1999-2004 Daniel Diaz
> | ?-
>
> That is, the variable _L in top/0 cannot be unified at the exit of
> call_c, like if the inner query was messing with the stack. If I comment
> the inner query, it works of course. Could someone help me out and tell
> me if, first, what I try to do is possible with the gprolog interface
> and how to do it properly? I can't figure it out myself as I guess I
> exhausted all the examples in the doc.
>
> Thanks in advance for your help, hopefully I could get back to work.
>
> Erick.
>
>
>
> _______________________________________________
> Users-prolog mailing list
> [email protected]
> http://lists.gnu.org/mailman/listinfo/users-prolog
>
--
===============================================
Daniel Diaz
University of Paris 1 INRIA Rocquencourt
75013 Paris FRANCE 78153 Le Chesnay FRANCE
web: http://pauillac.inria.fr/~diaz
email: [email protected]
--
Ce message a été vérifié par MailScanner
pour des virus ou des polluriels et rien de
suspect n'a été trouvé.