[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
4.4 More interesting programming with libguile
The learn0
program shows how you can invoke Scheme commands from
a C program. This is not such a great achievement: the same could have
been done by opening a pipe to SCM or any other Scheme interpreter.
A true extension language must allow callbacks. Callbacks allow you to write C routines that can be invoked as Scheme procedures, thus adding new primitive procedures to Scheme. This also means that a Scheme procedure can modify a C data structure.
Guile allows you to define new Scheme procedures in C, and provides a mechanism to go back and forth between C and Scheme data types.
Here is a second program, learn1
, which demonstrates these
features. It is split into three source files: learn1.c
,
c_builtins.h
and c_builtins.c
. I am including the code
here.
Notice that learn1
uses a Scheme master world, and the C routines
in c_builtins.c
are simply adding new primitives to Scheme.
4.4.1 learn1.c | ||
4.4.2 c_builtins.h | ||
4.4.3 c_builtins.c | ||
4.4.4 What learn1 is doing | ||
4.4.5 Compiling and running learn1 |