A simple example

One of the simpler extension one can wish to do consists to add new primitives procedures to the interpreter. To illustrate this, suppose we want to add two new primitives to the interpreter: posix-time and posix-ctime. The former function correspond to the POSIX.1[#!POSIX.1-90!#] function time: it returns the number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC). The latter is a wrapper around the POSIX.1 function ctime which returns a string containing the current time in an human readable format.

First, we will see how to write the new Scheme primitive posix-time. Implementing a new primitive requires to write a new C function which will do the work. Here, we write the C function posix_time to implement the Scheme primitive posix-time. The code of this function is given below.
\begin{Code}
\begin{listing}[200]{2}
static PRIMITIVE posix_time(void)
{
return STk_makeinteger((long) time(NULL));
}
\end{listing}\end{Code}
This function uses the interpreter STk_makeinteger function which converts a C long integer to a integer. Once the posix_time C function is written, we have to bind this new primitive to the Scheme symbol posix-time. This is achieved by the following C function call.
\begin{Code}
\begin{listing}[200]{2}
STk_add_new_primitive(''posix-time'', tc_subr_0, posix_time);
\end{listing}\end{Code}



Subsections