If you are writing a program that has a technical user base, it is a nice touch to make the program scriptable. In fact, you might want to do the hard work in a programming language and then use your scripting language to build out features. In theory, this should be easy. There are plenty of embedded scripting libraries and they provide some way for your code to access script resources and for script resources to access selected host variables and functions. If you use C++, one of the easier ways to do this is with ChaiScript.

ChaiScript is BSD licensed and — assuming your compiler supports C++ 14 — it is as easy as including a header file and making a few calls. There are no special tools or libraries required. The code is portable between operating systems, including both 32-bit and 64-bit Windows. It is also threadsafe unless you turn that feature off.



How simple is ChaiScript? Here’s their example of exposing a function (HelloWorld, of course). The function takes an argument and returns a value. The main program sets up the link between the function and script and then runs a simple script.

#include <chaiscript/chaiscript.hpp> std::string helloWorld(const std::string &t_name) { return "Hello " + t_name + "!"; } int main() { chaiscript::ChaiScript chai; chai.add(chaiscript::fun(&helloWorld), "helloWorld"); chai.eval(R"( puts(helloWorld("Bob")); )"); }

A real program is probably more likely to read its script from a file or some other user-specified thing, but that’s easy enough to imagine. According to the documentation, your script can call C++ and C++ can call into scripts and all in a type-safe manner. You can also propagate exceptions.

The scripting language itself is straightforward. Instead of the formal documentation, you might appreciate the cheat sheet. There are also quite a few examples.

It is easy to dismiss scripting languages, but they are perfect for some applications. ChaiScript and similar tools let you build the hard parts the hard way and the easy parts the easy way. After all, you probably use bash and it is nothing more than a scripting language.