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")
puts(42)
puts('\n')

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

if(condition)
{
 //execute if true
 puts("True")
}
else
{
 //execute if false
 puts("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.

Loop

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
}
puts(sum)
puts('\n')
var i = 50
while(--i)
{
 puts(i)
}

Comments

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
puts(num)
puts('\n')

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

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

screenshot-from-2017-02-18-16-06-50

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;
printf("%d\n",num);

num = 1.6;
printf("%d\n",num);

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.

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: