ChaiScript is a simple language that should feel familiar to anyone who knows C++ or ECMAScript (JavaScript).
Loops
Common looping constructs exist in ChaiScript
for (var i = 0; i < 10; ++i)
{
}
- See also
- for
-
while
Conditionals
If statements work as expected
if (b) {
} else if (c < 10) {
} else {
}
- 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.
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