Connecting Scheme and C variables

When building a specialized interpreter, it could be useful to have a variable you can access both in Scheme an in C. Modifying such a variable in C must modify the Scheme associated variable and, symmetrically, modifying it in Scheme must modify the corresponding C variable. One way to do this connection consists to create a special Scheme variable whose content is read/written by a special getter/setter. Definition of such a variable, is done by calling the function STk_define_C_variable. The C prototype for this function is
\begin{Code}
\begin{listing}[200]{2}
void STk_define_C_variable(char *var,
SCM...
...er)(char *var),
void (*setter)(char *var, SCM value));
\end{listing}\end{Code}

The following piece of code shows how we can connect the Scheme variable *errno* to the C variable errno:


\begin{Code}
\begin{listing}[200]{2}
static SCM get_errno(char *s)
{
return STk_makeinteger((long) errno);
}
\end{listing}\end{Code}

\begin{Code}
\begin{listing}[200]{2}
static void set_errno(char *s, SCM value)
{...
...(''setting *errno*: bad integer'', value);
errno = n;
}
\end{listing}\end{Code}

\begin{Code}
\begin{listing}[200]{2}
{
...
STk_define_C_variable(''*errno*'', get_errno, set_errno);
...
}
\end{listing}\end{Code}

After this call to STk_define_C_variable, reading (resp. writing) the value of the *errno* Scheme variable calls the get_errno (resp. set_errno) C function.