ChaiScript Tutoral (3) – The ChaiScript Language – part 2

The ChaiScript Language

The ChaiScript language itself is easy. Here are some notes about the language. If you are someone who learns as it goes. You can skip to the bottom of the page where we talk about ChaiScript’s type system or skip this entire secession if you want.

We will be running trough some really basic stuff quickly in the first part of this tutorial.

Basic ChaiScript

Console IO

Outputting to console is easy. The puts function has your back. It can print any builtin types to console. Ex

puts("Hello, the meaning of life is")

Here is where ChaiScript’s IO functionality ends. Yes, by default ChaiScript can only output to console, and inputting anything is impossible. You need to implement some functions in the host C++ code to do this.

Why is this? ChaiScript is a minimal language, of course.

Declaring Variables

Declaring variables in CahiScript is done by using the var keyword. Such as:

var foo

You can also assign value to the variable by doing so:

var foo = 42//Assign the meaning of life to variable foo

Condition statement (if)

if in ChaiScript works just like it is in any other language. The syntax is

 //execute if true
 //execute if false

Note that due that ChaiScript is a minimal language. The {} under if and else is required. Without the curly braces after if and else is a syntax error.


for and while loops are supported in ChaiScript. And, again they works just as it is in C.  {} are required after them. Ex:

//Add 1~100
var sum = 0
for(var i=1;i<=100;++i)
 sum += i
var i = 50


As you have seen a lot trough this series of tutotial,  you can comment in ChaiScript by using // for commenting the entire line and /*...*/ to comment a block. Ex:

puts("Hello")//prints "Hello"
A:Hay, ChaiScript is awesome.
B: Sure it is.

ChaiScript’s type system

As we know from above. You declare a variable in ChaiScript using the var keyword. But how is the type of the variable decided? It’s simple. It is decided when the variable’s value is assigned the first time. Therefor the line var bottleNum = 15 is acutely doing 2 things. First, it declares a variable called bottleNum. Then it assigns the value of 15 to it. It is at this step the type of bottleNum is determined. Note that ChaiScript is a strong typed language. The type cannot be changed after it is determined. Let’s have fun with it.

var num = 6

num = 1.6
puts(num)//only prints 1 out due to num is an integer

Save this as a text file then execute it using our interpolator. It should look like this.


The comment should explain why 1 is printed instead of 1.6. But if you can’t understand why. The code above is equivalent to the following C code

//The type int is decided by assigning the value 6, which is an int
int num = 6;

num = 1.6;

Also, be aware that if a variable is declared but no value is assigned. The variable cannot be read/ used in function calls due to it has no type, thus no value. You will get really weird error messages doing so.

But, if that is the case. What if we need to initialize a variable with a type with whatever the default value of the type is? Easy. Call the desired type’s constructor!

var num = int()

We don’t do that in C++ since there is on reason to do so. But this is how you declare a variable with default value of a type in ChaiScript.

This concludes this rather short tutorial. We will go a bit indepth about functions in ChaiScript in the next titorial.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Powered by

Up ↑

%d bloggers like this: