GNU Info

Info Node: (guile.info)Starting and controlling the interpreter

(guile.info)Starting and controlling the interpreter


Next: Error messages Prev: Data types and constants defined by gh Up: Top
Enter node , (file) or (file)node

Starting and controlling the interpreter
****************************************

In almost every case, your first `gh_' call will be:

 - Function: void gh_enter (int ARGC, char *ARGV[], void (*MAIN_PROG)())
     Starts up a Scheme interpreter with all the builtin Scheme
     primitives.  `gh_enter()' never exits, and the user's code should
     all be in the `MAIN_PROG()' function.  `argc' and `argv' will be
     passed to MAIN_PROG.

      - Function: void main_prog (int ARGC, char *ARGV[])
          This is the user's main program.  It will be invoked by
          `gh_enter()' after Guile has been started up.

     Note that you can use `gh_repl' inside `gh_enter' (in other words,
     inside the code for `main-prog') if you want the program to be
     controled by a Scheme read-eval-print loop.

A convenience routine which enters the Guile interpreter with the
standard Guile read-eval-print loop ("REPL") is:

 - Function: void gh_repl (int ARGC, char *ARGV[])
     Enters the Scheme interpreter giving control to the Scheme REPL.
     Arguments are processed as if the Guile program `guile' were being
     invoked.

     Note that `gh_repl' should be used _inside_ `gh_enter', since any
     Guile interpreter calls are meaningless unless they happen in the
     context of the interpreter.

     Also note that when you use `gh_repl', your program will be
     controlled by Guile's REPL (which is written in Scheme and has many
     useful features).  Use straight C code inside `gh_enter' if you
     want to maintain execution control in your C program.

You will typically use `gh_enter' and `gh_repl' when you want a Guile
interpreter enhanced by your own libraries, but otherwise quite normal.
For example, to build a Guile-derived program that includes some
random number routines "GSL" (GNU Scientific Library), you would write
a C program that looks like this:

     #include <guile/gh.h>
     #include <gsl_ran.h>
     
     /* random number suite */
     SCM gw_ran_seed(SCM s)
     {
       gsl_ran_seed(gh_scm2int(s));
       return SCM_UNSPECIFIED;
     }
     
     SCM gw_ran_random()
     {
       SCM x;
     
       x = gh_ulong2scm(gsl_ran_random());
       return x;
     }
     
     SCM gw_ran_uniform()
     {
       SCM x;
     
       x = gh_double2scm(gsl_ran_uniform());
       return x;
     }
     SCM gw_ran_max()
     {
       return gh_double2scm(gsl_ran_max());
     }
     
     void
     init_gsl()
     {
       /* random number suite */
       gh_new_procedure("gsl-ran-seed", gw_ran_seed, 1, 0, 0);
       gh_new_procedure("gsl-ran-random", gw_ran_random, 0, 0, 0);
       gh_new_procedure("gsl-ran-uniform", gw_ran_uniform, 0, 0, 0);
       gh_new_procedure("gsl-ran-max", gw_ran_max, 0, 0, 0);
     }
     
     void
     main_prog (int argc, char *argv[])
     {
       init_gsl();
     
       gh_repl(argc, argv);
     }
     
     int
     main (int argc, char *argv[])
     {
       gh_enter (argc, argv, main_prog);
     }

Then, supposing the C program is in `guile-gsl.c', you could compile it
with `gcc -o guile-gsl guile-gsl.c -lguile -lgsl'.

The resulting program `guile-gsl' would have new primitive procedures
`gsl-ran-random', `gsl-ran-gaussian' and so forth.


automatically generated by info2www version 1.2.2.9