Experiments with Embedded TinyScheme

The point of the work on getting the TinyScheme shared library to build and install was to allow us to embed the TinyScheme interpreter into a C program. So, I experimented to see how that works. Basically, in your C program, you use the tinyscheme library to initialize a scheme environment, then you load any scheme file you want:

christopher@evenstar:~/Devel/embedded-ts$ cat loader.c
#include "tinyscheme/scheme.h"

int main() {
 scheme * env;
 FILE *fptr;
 // initialize the scheme invironment
 env = scheme_init_new();
 // set output to go to STDOUT by default
 scheme_set_output_port_file(env, stdout);
 // Load the init file
 fptr = fopen("/usr/lib/tinyscheme/init.scm", "r");
 scheme_load_file(env, fptr);
 fclose(fptr);
 // Load my scheme program
 fptr = fopen("myscheme.scm", "r");
 scheme_load_file(env, fptr);
 fclose(fptr);
 // de-initialize the scheme environment
 scheme_deinit(env);
 return 0;
}

This C program will load whatever Scheme program is in “myscheme.scm”, which happens to be:

christopher@evenstar:~/Devel/embedded-ts$ cat myscheme.scm 
(display "hello world!")
(newline)

So, we compile the program, and run it:

christopher@evenstar:~/Devel/embedded-ts$ gcc -o loader loader.c -ltinyscheme
christopher@evenstar:~/Devel/embedded-ts$ ./loader 
hello world!

Of course, that isn’t too exciting an example, but the point is that (in conjunction with the ffi) the user can have his own scheme code controlling behavior and calling functions in the C program, without having to recompile the C program. So, you get the power of Scheme with the portability and efficiency of C.

This was the dream of Richard Stallman. People think of the “Linux” operating system (it should be called “Gnu/Linux”) as being so awesome because it is so much like Unix. But Stallman didn’t really like Unix that much: he wanted a Lisp operating system. But Lisp operating systems required a lot of specialized hardware, whereas Unix could run on pretty much anything. So, the next best thing was to have a Unix-like operating system that at the foundation was written in C, but that code in turn was running mostly lisp code that could be very flexible and easily hacked by the user.

That dream wasn’t fully realized, unfortunately, throughout the Gnu operating system. But a great example of it is the Emacs text-editor, the core of which is written in C, but most of the action happens in elisp code. Also, Guile Scheme was designed to be embedded in C programs, and has been in a few cases, such as in the GnuCash software and LilyPond.

For libreCMC I am interested in TinyScheme rather than Guile Scheme, because Guile Scheme is too big for the embedded environment. Perhaps somebody could whittle Guile Scheme down to a much smaller core, but I didn’t see any options for that in the build system.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s