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)
{
++i;
}
for (var i = 0; i < 10; ++i)
{
}
- See also
- for
-
while
Conditionals
If statements work as expected
var b = true;
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);
void print(const VoxFrame &f)
Definition GameApi.cc:22580
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); }
lhs: 1, rhs: 2
int do_something(int i)
Definition c_linkage_test.cpp:8
double f(const std::string &, double, bool) noexcept
Definition profile_cpp_calls_2.cpp:4
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