About ChaiScript

ChaiScript is the first and only scripting language designed from the ground up with C++ compatibility in mind. It is an ECMAScript-inspired, embedded functional-like language.

ChaiScript is licensed under the BSD license.

Download

Version: 2.3.3 Released: 5/15/2010

Source
Windows
Linux

Getting Started

Getting the source

The latest source (as well as the latest Subversion) is hosted at Google code. To grab the latest release, go here. For the more adventurous, you can get the latest source code here.

Getting Boost

ChaiScript requires Boost (at least 1.34), which you can get here.

Compiling the evaluator

In GCC:

cd chaiscript
./g++ -Iinclude -I<path to Boost> src/main.cpp -o chaiscript_eval

In Visual Studio:

Open the msvc\chaiscript\chaiscript.sln VC++ solution file. Use the property manager to modify the "C++/General/Additional Include Directories" and "Linker/General/Additional Library Directories" to reflect your local configuration.

Right-click on "chaiscript" and hit "compile" or just hit the "Start Debugging" "play" button to start the evaluator.

Using the evaluator

In Windows:

chaiscript_eval.exe
eval>

In Linux and OS X:

./chaiscript_eval
eval>

The evaluator allows you to evaluate ChaiScript expressions. This is a great way to get a feel for the language. You can also pass ChaiScript source files to the evaluator from the commandline to run them as standalone scripts.

Starting an example project

The minimal ChaiScript project requires only a few lines:

#include <chaiscript/chaiscript.hpp>
int main() {
  using namespace chaiscript;
 
  ChaiScript chai;
  chai.eval("print(\"Hello, world\")");
 
  return 0;
}

You can also grab values returned by executing ChaiScript. For example, if you wanted to compute a value, and return its answer, you can replace the above eval line with one like this:

int answer = chai.eval<int>("5+5");

Notice that we use the templated version so that what comes out of the script is type-safe in C++.

It's also possible to register your own functions so that your script can see them. For example:

#include <chaiscript/chaiscript.hpp>
int add(int x, int y) {
  return x+y;
}
 
int main() {
  using namespace chaiscript;
 
  ChaiScript chai;
  chai.add(fun(&add), "add");  //register our 'add' function under the name "add"
  chai.eval("print(add(3, 4))");
 
  return 0;
}

For more a more involved example project, see example.cpp.

ChaiScript examples

Some simple examples:

//Creating a vector of mixed types:
var v = [1, 4, "Hi", 5.0]
 
//And print them one per line by passing the 'print' function
v.for_each(print);
//Creating a Map
var m = ["a":1, "b":2, "c":"Hello World"]
 
//Print the value referenced with the key "b"
print(m["b"])
 
//Replacing the value with another value
m["b"] = 5
 
//Print the key again
print(m["b"])

For more examples, see the samples directory.

Continue on to the ChaiScript Reference or the C++ API Reference.