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.
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.
Assignment
Assignment in ChaiScript occurs by default as a copy operation.
var i = 5; var j = i; // Copy i into j
You can explicitly choose a reference assignment with the := operator.
var i = 5; var j := i; // Share the same value between i and j
Conditional Blocks
"If" blocks are supported, but be sure to note that "else if" is a single keyword instead of two separate words as it is in C++:
var i = 2 if (i < 2) { print("less than 2") } else if (i == 2) { print("equal to 2") } else { print("more than 2") }
Functions
ChaiScript allows two kinds of function definitions. The first, static definitions, use the keyword "def" and are globally visible:
def add_elems(x, y) { x + y } print(add_elems(5, 10))
Function may also have guards, expressions which prevent or allow execution. For example, let's say we want to print the proper pluralization next to the number of items we have in a vector:
def print_count(x) : x.size() == 1 { print("You have " + x.size().to_string() + " item") } def print_count(x) { print("You have " + x.size().to_string() + " items") } print_count(["bob"]) // prints "You have 1 item" print_count(["bob", "fred", "sally"]) //prints "You have 3 items"
Anonymous functions (aka lambdas) create a function as a value in your code, that can be passed in a variable.
var add_elems = fun(x, y) { x + y } print(add_elems(5, 10))
Return values
ChaiScript return values come in two types: explicit and inferred. Explicit return values work identically to C++:
def five() { return 5 } print(five())
When there is no explicit return statement, the last value referenced in a function is used. This code sample produces the same result as the above:
def five() { 5 } print(five())
Looping
ChaiScript allows C++ style loops in two flavors:
For loops:
for (var i = 0; i < 10; ++i) { print("i: " + i.to_string()) }
While loops:
var i = 0 while (i < 10) { print("i: " + i.to_string()) ++i }
Loops can be broken with the 'break' keyword, which works similarly to C++, where flow breaks out of the inner-most loop to the next outer loop.
Methods
ChaiScript uses a "syntactic sugar" technique for methods which allow functions to be called as methods. For example, the following are equivalent:
5.to_string() to_string(5)
The value on the left of the dot is used as the first parameter to the named function.
Containers
ChaiScript supports a handful of container types, allowing shortcuts for creating Vectors and Maps.
Vectors:
var x = [1, 2, 3] print(x[1])
Maps:
var x = ["bob":1, "fred":2] print(x["fred"])
Also available is a shortcut notation for creating a Vector from a range of values:
var x = [1..10] print(x)
Exception Blocks
ChaiScript offers the ability to capture and throw exceptions.
try { //dangerous block of code } catch (e) { //do something with 'e', our exception } finally { //do something after everything else has been done }
Throwing an exception is just like any other function call, but we use 'throw' as the name of the function:
throw(3)
Objects
For more information on how ChaiScript can extend C++ classes and build new types, read here.