unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Embedding guile in a C application.
@ 2004-05-30  6:32 Binesh Bannerjee
  2004-05-30 15:45 ` Linas Vepstas
  0 siblings, 1 reply; 7+ messages in thread
From: Binesh Bannerjee @ 2004-05-30  6:32 UTC (permalink / raw)


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi...
 	I'm looking to code a genetic programming system a-la Koza ( 
http://www.geneticprogramming.com/Tutorial/ )...
 	I'd like to embed a Guile interpreter inside the app,
and have the C program generate Guile functions, and then get interpreted
with Guile... I'm not sure how to go about doing this, and I haven't found
any clear directions how to do so... (Although, I _have_ found hints that it
should be possible.) http://www.swig.org/Doc1.3/Guile.html shows me
how to embed guile... But, I don't think that's exactly what I want.
What it does, is create a whole guile interpreter shell and all...

All's I want is (something like) this:
 	int main(int argc,char *argv[]) {
 		guile_interpreter gi = create_guile_interpreter();
/* c = (define f (lambda (x) (+ 1 x))); */
 		cons c = new list_struct();
 		c->add("define");
 		c->add("f");
 		cons c2 = new list_struct();
 		c2->add("lambda");
 		const c3 = new list_struct();
 		c3->add("x");
 		c2->add_list(c3);
 		const c4 = new list_struct();
 		c4->add("+");
 		c4->add("x");
 		c4->add("1");
 		c2->add_list(c4);
 		c->add_list(c2);

 		gi->interpret(c);
 		cons r = new list_struct();
 		r->add("f");
 		r->add("6");
 		cons retVal = gi->interpret(r);
 		printf("%d\n",retVal->value());
 	}

 	and have that print out 7.

How would I do that?

Thanks!
Binesh

- -- 
     PGP  Key: http://www.hex21.com/~binesh/binesh-public.asc
PGP Key fingerprint = 421D B4C2 2E96 B8EE 7190  A0CF B42F E71C 7FC3 AD96

     SSH2 Key: http://www.hex21.com/~binesh/binesh-ssh2.pub
OpenSSH  Key: http://www.hex21.com/~binesh/binesh-openssh.pub
BubbleBabble = xibeb-voges-havez-pabaf-debop-cylil-lelyc-viruv-bygeg-zotoh-dixex
  Fingerprint = 9d:7c:84:5d:80:e3:65:8d:ee:9e:a3:b9:56:0a:e9:ad

     SSH1 Key: http://www.hex21.com/~binesh/binesh-ssh1.pub

CipherKnight Seals:
 	http://www.hex21.com/~binesh/binesh-seal.tar.bz2.cs256
 	http://www.hex21.com/~binesh/binesh-seal.zip.cs256
 	http://www.hex21.com/~binesh/binesh-certificate.gif.cs256
 	Decrypt with CipherSaber2 N=256, Password="WelcomeJedi!" (No quotes)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: pgpenvelope 2.10.2 - http://pgpenvelope.sourceforge.net/

iD8DBQFAuYAetC/nHH/DrZYRAnmJAJ9SlaBm8MqZnhvtATshsI3AYMjvtgCg9OT6
QNypnWlRqHi6cGD34YcA7gk=
=AaF0
-----END PGP SIGNATURE-----


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Embedding guile in a C application.
  2004-05-30  6:32 Embedding guile in a C application Binesh Bannerjee
@ 2004-05-30 15:45 ` Linas Vepstas
  2004-05-31  5:54   ` Binesh Bannerjee
  0 siblings, 1 reply; 7+ messages in thread
From: Linas Vepstas @ 2004-05-30 15:45 UTC (permalink / raw)
  Cc: guile-user

On Sun, May 30, 2004 at 02:32:56AM -0400, Binesh Bannerjee was heard to remark:
> 	I'd like to embed a Guile interpreter inside the app,

Try reading some of teh guile tutorials.

In your C code, you'd want to call scm_c_eval_string () to evaluate a
string e.g. scm_c_eval_string ("(define f (lambda (x) (+ 1 x)))");

Guile also like  to hold the main loop (I don't know why, is this fixed
yet?)

void
guile_inner_main(void *closure, int argc, char **argv)
{ 
  ... your prog ... 
}

int
main (int argc, char **argv)
{
  scm_boot_guile (argc, argv, guile_inner_main, NULL);
}


To do your example, below, you would have to write your own
C++ class that assembled stuff into strings, and then called
scm_c_eval_string() on the resulting string.  Caution: you will
find debugging something like the below for syntax errors to be very
very hard.

> /* c = (define f (lambda (x) (+ 1 x))); */
> 		cons c = new list_struct();
> 		c->add("define");
> 		c->add("f");
> 		cons c2 = new list_struct();
> 		c2->add("lambda");
> 		const c3 = new list_struct();
> 		c3->add("x");
> 		c2->add_list(c3);
> 		const c4 = new list_struct();
> 		c4->add("+");
> 		c4->add("x");
> 		c4->add("1");
> 		c2->add_list(c4);
> 		c->add_list(c2);

--linas

-- 
pub  1024D/01045933 2001-02-01 Linas Vepstas (Labas!) <linas@linas.org>
PGP Key fingerprint = 8305 2521 6000 0B5E 8984  3F54 64A9 9A82 0104 5933


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Embedding guile in a C application.
  2004-05-30 15:45 ` Linas Vepstas
@ 2004-05-31  5:54   ` Binesh Bannerjee
  2004-05-31  6:02     ` Binesh Bannerjee
  0 siblings, 1 reply; 7+ messages in thread
From: Binesh Bannerjee @ 2004-05-31  5:54 UTC (permalink / raw)


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sun, 30 May 2004, Linas Vepstas wrote:

> On Sun, May 30, 2004 at 02:32:56AM -0400, Binesh Bannerjee was heard to remark:
>> 	I'd like to embed a Guile interpreter inside the app,
>
> Try reading some of teh guile tutorials.
>
> In your C code, you'd want to call scm_c_eval_string () to evaluate a
> string e.g. scm_c_eval_string ("(define f (lambda (x) (+ 1 x)))");

Nice, thanks!

I actually couldn't find any of the tutorials, but, searching for 
scm_c_eval_string on google gave me enough direction to move...

I'll attach my code, just in case someone else has the same
question.

Thanks again!
Binesh Bannerjee

 	#include <stdio.h>
 	#include <libguile.h>

 	void inner_main(void *closure,int argc,char **argv) {
 		scm_c_eval_string("(define f (lambda (x) (+ 1 x)))");
 		SCM s = scm_c_eval_string("(f 5)");
 		int q = SCM_INUM(s);
 		fprintf(stderr,"%d\n",q);
 	}

 	int main(int argc,char *argv[]) {
 		scm_boot_guile(argc,argv,inner_main,0);
 		return(0);
 	}

- -- 
"When Webster's dictionary used a sparrow to stand for all birds, emus and
  ostriches and penguins and eagles did not go on the attack."
 	-- "The Blank Slate" -- Steven Pinker

     PGP  Key: http://www.hex21.com/~binesh/binesh-public.asc
PGP Key fingerprint = 421D B4C2 2E96 B8EE 7190  A0CF B42F E71C 7FC3 AD96

     SSH2 Key: http://www.hex21.com/~binesh/binesh-ssh2.pub
OpenSSH  Key: http://www.hex21.com/~binesh/binesh-openssh.pub
BubbleBabble = xibeb-voges-havez-pabaf-debop-cylil-lelyc-viruv-bygeg-zotoh-dixex
  Fingerprint = 9d:7c:84:5d:80:e3:65:8d:ee:9e:a3:b9:56:0a:e9:ad

     SSH1 Key: http://www.hex21.com/~binesh/binesh-ssh1.pub

CipherKnight Seals:
 	http://www.hex21.com/~binesh/binesh-seal.tar.bz2.cs256
 	http://www.hex21.com/~binesh/binesh-seal.zip.cs256
 	http://www.hex21.com/~binesh/binesh-certificate.gif.cs256
 	Decrypt with CipherSaber2 N=256, Password="WelcomeJedi!" (No quotes)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: pgpenvelope 2.10.2 - http://pgpenvelope.sourceforge.net/

iD8DBQFAusi+tC/nHH/DrZYRAu4CAJ9Y+YjgRVPsiirCA/URW03MVcnTvwCgrGJU
eo8UOkzuHO6ooW0mkT+mhlk=
=Skv4
-----END PGP SIGNATURE-----


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Embedding guile in a C application.
  2004-05-31  5:54   ` Binesh Bannerjee
@ 2004-05-31  6:02     ` Binesh Bannerjee
  2004-05-31 19:54       ` Mike Gran
  0 siblings, 1 reply; 7+ messages in thread
From: Binesh Bannerjee @ 2004-05-31  6:02 UTC (permalink / raw)


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Mon, 31 May 2004, Binesh Bannerjee wrote:

>
> I actually couldn't find any of the tutorials, but, searching for
> scm_c_eval_string on google gave me enough direction to move...

Rather, I should say, I couldn't find a clear tutorial that described
how to read scheme variables and call scheme procedures from C and vice versa...

Binesh

>

- -- 
"Prediction is difficult, especially of the future."
 	-- Neils Bohr

     PGP  Key: http://www.hex21.com/~binesh/binesh-public.asc
PGP Key fingerprint = 421D B4C2 2E96 B8EE 7190  A0CF B42F E71C 7FC3 AD96

     SSH2 Key: http://www.hex21.com/~binesh/binesh-ssh2.pub
OpenSSH  Key: http://www.hex21.com/~binesh/binesh-openssh.pub
BubbleBabble = xibeb-voges-havez-pabaf-debop-cylil-lelyc-viruv-bygeg-zotoh-dixex
  Fingerprint = 9d:7c:84:5d:80:e3:65:8d:ee:9e:a3:b9:56:0a:e9:ad

     SSH1 Key: http://www.hex21.com/~binesh/binesh-ssh1.pub

CipherKnight Seals:
 	http://www.hex21.com/~binesh/binesh-seal.tar.bz2.cs256
 	http://www.hex21.com/~binesh/binesh-seal.zip.cs256
 	http://www.hex21.com/~binesh/binesh-certificate.gif.cs256
 	Decrypt with CipherSaber2 N=256, Password="WelcomeJedi!" (No quotes)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: pgpenvelope 2.10.2 - http://pgpenvelope.sourceforge.net/

iD8DBQFAusp8tC/nHH/DrZYRAtrlAKDxFYowwA5cZ9/j9zUd100CWQAmBgCgj1Tk
px8P0IJM5Qbz1j00Mzjxrzw=
=yEFZ
-----END PGP SIGNATURE-----


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Embedding guile in a C application.
  2004-05-31  6:02     ` Binesh Bannerjee
@ 2004-05-31 19:54       ` Mike Gran
  2004-06-01 10:00         ` rm
  0 siblings, 1 reply; 7+ messages in thread
From: Mike Gran @ 2004-05-31 19:54 UTC (permalink / raw)


I have been trying to assemble my notes on that subject into something
useful.  I've only just started, but, you can see what I've done at
http://lonelycactus.com/guilebook/ .  

It is probably premature to advertise this now, since this is really
rough stuff, with a lot of needless text in the beginning and only a
little bit of useful content so far.  But hopefully I can try to fix it
up as time allows.

-Mike

--- Binesh Bannerjee <binesh-guile@hex21.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> On Mon, 31 May 2004, Binesh Bannerjee wrote:
> 
> >
> > I actually couldn't find any of the tutorials, but, searching for
> > scm_c_eval_string on google gave me enough direction to move...
> 
> Rather, I should say, I couldn't find a clear tutorial that described
> how to read scheme variables and call scheme procedures from C and
> vice versa...
> 
> Binesh



	
		
__________________________________
Do you Yahoo!?
Friends.  Fun.  Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/ 


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Embedding guile in a C application.
  2004-05-31 19:54       ` Mike Gran
@ 2004-06-01 10:00         ` rm
  2004-08-09 22:51           ` Marius Vollmer
  0 siblings, 1 reply; 7+ messages in thread
From: rm @ 2004-06-01 10:00 UTC (permalink / raw)
  Cc: guile-user

On Mon, May 31, 2004 at 12:54:20PM -0700, Mike Gran wrote:
> I have been trying to assemble my notes on that subject into something
> useful.  I've only just started, but, you can see what I've done at
> http://lonelycactus.com/guilebook/ .  

Minor nitpick: if you close that last </html tag some browsers will be
much more happy ;-)
Other than that: very nice work! There should be a link to your page
on the guile website.

 
  Ralf Mattes

  
> It is probably premature to advertise this now, since this is really
> rough stuff, with a lot of needless text in the beginning and only a
> little bit of useful content so far.  But hopefully I can try to fix it
> up as time allows.
> 
> -Mike
> 
> --- Binesh Bannerjee <binesh-guile@hex21.com> wrote:
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA1
> > 
> > On Mon, 31 May 2004, Binesh Bannerjee wrote:
> > 
> > >
> > > I actually couldn't find any of the tutorials, but, searching for
> > > scm_c_eval_string on google gave me enough direction to move...
> > 
> > Rather, I should say, I couldn't find a clear tutorial that described
> > how to read scheme variables and call scheme procedures from C and
> > vice versa...
> > 
> > Binesh
> 
> 
> 
> 	
> 		
> __________________________________
> Do you Yahoo!?
> Friends.  Fun.  Try the all-new Yahoo! Messenger.
> http://messenger.yahoo.com/ 
> 
> 
> _______________________________________________
> Guile-user mailing list
> Guile-user@gnu.org
> http://lists.gnu.org/mailman/listinfo/guile-user


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Embedding guile in a C application.
  2004-06-01 10:00         ` rm
@ 2004-08-09 22:51           ` Marius Vollmer
  0 siblings, 0 replies; 7+ messages in thread
From: Marius Vollmer @ 2004-08-09 22:51 UTC (permalink / raw)
  Cc: guile-user, spikegran

rm@fabula.de writes:

> On Mon, May 31, 2004 at 12:54:20PM -0700, Mike Gran wrote:
>
>> I have been trying to assemble my notes on that subject into something
>> useful.  I've only just started, but, you can see what I've done at
>> http://lonelycactus.com/guilebook/ .  
>
> [...] There should be a link to your page on the guile website.

Definitely!  Done!

-- 
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2004-08-09 22:51 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-05-30  6:32 Embedding guile in a C application Binesh Bannerjee
2004-05-30 15:45 ` Linas Vepstas
2004-05-31  5:54   ` Binesh Bannerjee
2004-05-31  6:02     ` Binesh Bannerjee
2004-05-31 19:54       ` Mike Gran
2004-06-01 10:00         ` rm
2004-08-09 22:51           ` Marius Vollmer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).