Home > scheme-cpp

scheme-cpp

Scheme-cpp is a project mainly written in C and C++, based on the MIT license.

A lightweight wrapper around TinyScheme

h1. Scheme-Cpp

A lightweight C++ wrapper around TinyScheme (http://tinyscheme.sourceforge.net/home.html)

h2. Building

OS X and Linux is the only supported platforms for the moment, and you will need CMake to generate the required Makefile.

Create a build folder, this step is optional, but it's encouraged. It makes cleaning up a lot easier, especially because CMake creates a lot of files all over the place: @mkdir build@ @cd build@

Generate makefiles for the project: @cmake -DCMAKE_BUILD_TYPE=Debug ..@

Compile: @make@

h2. Example

A simple example on how to use the library.

Start by creating an instance of the Interpreter.

bc. script::Interpreter eval;

Next we can execute some code, here we define a function called add.

bc. eval.loadScript("(define (add a b) (+ a b))");

Then we can call our new function, and get back the result.

bc. script::Cell result = eval.call("add", 1, 2);

And lastly we can access the result.

bc. int value = result.toInteger();

h3. Binding C functions

It is really easy to bind C functions so they can be accessed from scheme.

First we define out function, lets make a print function.

bc. void printFunc(std::string const& str) { std::cout << str << std::endl; }

Then we create an interpreter and bind our function to it.

bc. script::Interpreter eval; eval.function("print", printFunc);

We can then call our C function from scheme.

bc. eval.loadString("(print "Hello World!")");

h2. License

See the LICENSE file for legal information about Scheme-Cpp. For license information about TinyScheme see the file @src/tinyscheme/COPYING@.

Previous:Q4Code