ChaiScript
ChaiScript Language Getting Started

ChaiScript is a simple language that should feel familiar to anyone who knows C++ or ECMAScript (JavaScript).


Loops

Common looping constructs exist in ChaiScript

var i = 0;
while (i < 10)
{
// do something
++i;
}
for (var i = 0; i < 10; ++i)
{
// do something
}
See also
for
while

Conditionals

If statements work as expected

var b = true;
if (b) {
// do something
} else if (c < 10) {
// do something else
} else {
// or do this
}
See also
if

Functions

Functions are defined with the def keyword

def myfun(x) { print(x); }
myfun(10);

Functions may have "guards" which determine if which is called.

eval> def myfun2(x) : x < 10 { print("less than 10"); }
eval> def myfun2(x) : x >= 10 { print("10 or greater"); }
eval> myfun2(5)
less than 10
eval> myfun2(12)
10 or greater
See also
def
attr
ChaiScript Language Object Model Reference

Function Objects

Functions are first class types in ChaiScript and can be used as variables.

eval> var p = print;
eval> p(1);
1

They can also be passed to functions.

eval> def callfunc(f, lhs, rhs) { return f(lhs, rhs); }
eval> def do_something(lhs, rhs) { print("lhs: ${lhs}, rhs: ${rhs}"); }
eval> callfunc(do_something, 1, 2);
lhs: 1, rhs: 2

Operators can also be treated as functions by using the back tick operator. Building on the above example:

eval> callfunc(`+`, 1, 4);
5
eval> callfunc(`*`, 3, 2);
6

See also
ChaiScript Language Keyword Reference
ChaiScript_Language for Built in Functions