Go to the first, previous, next, last section, table of contents.

Libguile reference

The Guile interpreter is essentially Aubrey Jaffer's SCM interpreter (see section `Overview' in SCM: a portable scheme interpreter) with some modifications to make it suitable as an embedded interpreter.

Part of the modification has been to provide a restricted interface to limit access to the SCM internals; this is called the gscm_ interface, or libguile interface.

If you are programming with Guile, you should only use the C subroutines described in this manual, which all begin with gscm_.

If instead you are extending Guile, you have the entire SCM source to play with. This manual will not help you at all, but you can consult Aubrey Jaffer's SCM manual (see section `Internals' in SCM: a portable scheme interpreter).

If you are adding a module to Guile, I recommend that you stick to the gscm_ interface: this interface is guaranteed to not change drastically, while the SCM internals might change as Guile is developed.

Libguile preliminaries

To use libguile, you must have the following toward the beginning of your C source:

#include <guile/gscm.h>

When you link, you will have to add at least -lguile to the list of libraries. If you are using more of Guile than the basic scheme interpreter, you will have to add more libraries.

If you use these extra packages, make sure you look at section Starting and controlling the interpreter: it will show you what startup code you need to initialize each of these extra libraries.

Data types defined by libguile

The following C constants and data types are defined in libguile:

Data type: GSCM_status
A data type returned by many gscm_ routines. Its value is meant to be interpreted by gscm_error_msg() if it is not GSCM_OK.

Data type: GSCM_top_level
The data type used to store information about the scheme top levels.

Data type: SCM
This is a C data type used to store all scheme data, no matter what the scheme type. Values are converted between C data types and the SCM type with utility functions described below (see section Converting data between C and scheme).

Constant: GSCM_OK
A constant returned by gscm_ calls when there was no error.

Starting and controlling the interpreter

In almost every case, your first gscm_ call will be

Function: GSCM_status gscm_run_scm (int argc, char **argv, FILE stdin, FILE stdout, FILE stderr, (GSCM_status init_proc)(), int boh, char *boh)
Starts up a scheme interpreter, passing argc and argv, with the given assignment of stdin, stdout, stderr. The routine init_proc() is invoked to initialize particular Guile packages.

This next batch of routines are the ones that can be included in the routine init_proc() passed to gscm_run_scm.

Function: void gscm_threads_init_all ()
Initializes Guile threads.

Function: void scm_init_unix ()
Function: void scm_init_posix ()
Function: void scm_init_ioext ()
These three initialize the posix library for scheme.

Function: void scm_init_gtcl ()
Initializes tcl support for Guile.

Function: void scm_init_gtk ()
Initializes Tk support for Guile.

After initializing the interpreter with gscm_run_scm, you need to create a top level. The top level variable you obtain will be used for most future libguile calls.

Function: GSCM_status gscm_create_top_level (GSCM_top_level *toplev)
This routine creates a top level of the interpreter in which to evaluate scheme expressions.

At the end, when you are done with scheme, you can invoke:

Function: void gscm_destroy_top_level (GSCM_top_level toplev)
This routine releases all the resources associated with that top level, thus allowing the top level to be garbage collected.

Error messages

If a routine returns a value of type GSCM_status, we can get a human-readable representation of what the error condition was by invoking:

Function: char * gscm_error_msg (GSCM_status status)
This routine returns a string which can be printed directly. Note that the string will be trashed and reallocated with the next invocation of gscm_error_msg. Here's the typical example of the use of GSCM_status:
status = gscm_some_function_returning_status(...);
if (status != GSCM_OK) {
  fputs(gscm_error_msg(status), stderr);
  fputc('\n', stderr);
  exit(1);
}

Here is how the various possible error codes are defined in `gscm.h':

#define GSCM_OK                         0
#define GSCM_QUIT                       1
#define GSCM_RESTART                    2
#define GSCM_ILLEGALLY_REENTERED        3
#define GSCM_OUT_OF_MEM                 4
#define GSCM_ERROR_OPENING_FILE         5
#define GSCM_ERROR_OPENING_INIT_FILE    6

Executing scheme code

Once you have an interpreter running, and you have created a top level environment, you can ask the interpreter to evaluate scheme code. There are two calls that implement this:

Function: GSCM_status gscm_eval_str (char **answer, GSCM_top_level toplev, char *scheme_code)
This asks the interpreter to evaluate a single line of scheme code. The result of the evaluation is placed in the string *answer. Note that answer is malloc-ed by gscm_eval_str, so after using the value of *answer, you should free it. If answer is NULL, the evaluation result is not returned to the caller

Also note that the line of code in scheme_code must be a well formed scheme expression. If you have many lines of code you must either concatenate them into one string, or use gscm_eval_file().

Function: GSCM_status gscm_eval_file (char **answer, GSCM_top_level toplev, char *fname)
Completely analogous to gscm_eval_str(), except that a whole file is evaluated instead of a string.

Defining new scheme procedures in C

The real interface between C and scheme comes when you can write new scheme procedures in C. This is done through the routine

Function: void gscm_define_procedure (char *name, SCM (*fn)(), int req, int opt, int varp, char *doc)
This routine makes a scheme procedure out the C procedure (*fn)(). The scheme procedure will require req arguments, and accept opt optional arguments. The procedure will be documented by the documentation string doc. [Note: it is not yet clear how documentation strings will evolve in Guile.]

There are several important considerations to be made when writing the C routine (*fn)().

First of all the C routine has to return type SCM.

Second, all arguments passed to the C funcion will be of type SCM.

Third: the C routine is now subject to scheme flow control, which means that it could be interrupted at any point, and then reentered. This means that you have to be very careful with operations such as allocating memory, modifying static data ...

Fourth: to get around the latter issue, you can use GSCM_DEFER_INTS and GSCM_ALLOW_INTS.

Macro: GSCM_DEFER_INTS
Macro: GSCM_ALLOW_INTS
These macros disable and reenable scheme's flow control. They

Converting data between C and scheme

Guile provides mechanisms to convert data between C and scheme. This allows new builtin procedures to understand their arguments (which are of type SCM) and return values of type SCM.

C to scheme

Function: SCM gscm_bool (int x)
Returns #f if x is zero, #t otherwise.

Function: SCM gscm_ulong (unsigned long x)
Function: SCM gscm_long (long x)
Function: SCM gscm_double (double x)
Function: SCM gscm_char (char x)
Function: SCM gscm_str (char *x, int len)
Function: SCM gscm_str0 (char *x)
Returns a scheme object with the value of the C quantity x.

Scheme to C

Function: int gscm_2_bool (SCM obj)
Function: unsigned long gscm_2_ulong (SCM obj)
Function: long gscm_2_long (SCM obj)
Function: double gscm_2_double (SCM obj)
Function: int gscm_2_char (SCM obj)
Function: void gscm_2_str (char **str_out, int *len_out, SCM *obj)
Function: void gscm_2_str0 (char **str_out, int *len_out, SCM *obj)
These routines convert the scheme object to the given C type.

Note the distinction between the str and str0: the former returns with C null-terminated strings; the latter returns a scheme string.

Also note that the string procedures take a pointer to the scheme object obj, and that they return the string in a volatile location *str_out.

Memory allocation and garbage collection

Function: SCM gscm_mkarray (int size)
Allocate memory for a scheme object in a garbage-collector-friendly manner.

Calling scheme procedures from C

Many of the scheme primitives are available in the gscm_ interface; they take and return objects of type SCM, and one could basically use them to write C code that mimics scheme code.

I will list these routines here without much explanation, since what they do is the same as documented in section `Standard Procedures' in R4RS. But I will point out that when a procedure takes a variable number of arguments (such as gscm_list), you should pass the constant SCM_EOL from C to signify the end of the list.

Function: SCM gscm_define (char *name, SCM val)
Corresponds to the scheme (define name val): it binds a value to the given name (which is a C string).

Function: SCM gscm_cons (SCM a, SCM b)
Function: SCM gscm_list (SCM l0, SCM l1, ... , GSCM_EOL_MARKER)
These correspond to the scheme (cons a b) and (list l0 l1 ...) procedures.

Function: SCM gscm_ilength (SCM ls)
Returns the length of the list.

Function: SCM gscm_set_car (SCM obj, SCM val)
Function: SCM gscm_set_cdr (SCM obj, SCM val)
These correspond to the scheme (set-car! ...) and (set-cdr! ...) procedures.

Function: SCM gscm_car (SCM obj)
Function: SCM gscm_cdr (SCM obj)
...

Function: SCM gscm_c[ad][ad][ad][ad]r (SCM obj)
These correspond to the scheme (caadar ls) procedures etc ...

Function: SCM gscm_symbol (SCM str, SCM len)
Function: SCM gscm_tmp_symbol (SCM str, SCM len)
Takes the given string str of length len and returns a symbol corresponding to that string.

Function: SCM gscm_vector (SCM n, SCM fill)
Function: SCM gscm_vref (SCM v, SCM i)
Function: SCM gscm_vset (SCM v, SCM i, SCM val)
These correspond to the scheme (vector n fill), (vref v i) and (vset v i value) procedures.

Function: SCM gscm_make_subr (SCM (*fn)(), int req, int opt, int varp, char *doc
Function: SCM gscm_curry (SCM proc, SCM first_arg)
These routines create new scheme procedures; the first form corresponds to (lambda (...) (...)); the second curries a procedure by fixing the first argument.

Function: SCM gscm_apply (SCM proc, SCM args)
Corresponds to the scheme (apply proc args ...)

Function: SCM gscm_catch (SCM key, SCM thunk, SCM handler)
Function: SCM gscm_throw (SCM key, SCM args)
Corresponds to the scheme catch and throw procedures, which in Guile are provided as primitives.

Function: SCM gscm_is_eq (SCM a, SCM b)
Function: SCM gscm_is_eqv (SCM a, SCM b)
Function: SCM gscm_is_equal (SCM a, SCM b)
These correspond to the scheme eq?, eqv? and equal? predicates.

Function: int gscm_obj_length (SCM obj)
Returns the raw object length.


Go to the first, previous, next, last section, table of contents.