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

In the last ChaiScript tutorial. We wrote our first ChaiScript program. In this tutorial we will write some ChaiScript! We will be focusing on the basic syntax and how-to for ChaiScript itself.

Setting up the interpolator.

Yup, we need to setup something again. But this time, we will be setting up our own ChaiScript interpolator to execute ou ChaiScript code. If you have build the chai executable in the last tutorial. This step can be ignored. Just use the chai executable instead.

Here is our simple interpolator.

#include <chaiscript/chaiscript.hpp>
int main(int argc, char** argv)
{
 chaiscript::ChaiScript chai;
 if(argc == 2)
  chai.use(argv[1]);
}

It should be straight forward. We create a ChaiScript interpolator by the line  chaiscript::ChaiScript chai . Than the chai.use(<file path here>) makes ChaiScitpt interpolator loads a file and execute it.

Build this program using the command:

g++ ./chaiexec.cpp -o chaiexec -ldl -pthread -std=c++14

Text editor with ChaiScript syntax highlighting

Before writing any ChaiScript. We need am editor to edit our ChaiScript code. But in most cases. The text editor/IDE of your choice don’t support syntax highlighting for ChaiScript. Unless you can write code without syntax highlighting. We need an text editor that supports it.

As far as I know. There are two text editors that supports ChaiScript. –  VIM and GitHub Atom.

VIM should have ChaiScript support right out of the box. Install VIM and you are good to go. At least that is what happens to me. If you are not a guru then GitHub Atom is your choice. It does not support ChaiScript just after install. But a package called language-chaiscript gives Atom this functionality. (Yes, I wrote the package.) Install Atom on your computer then navigate to the Packages page in Settings. Search for language-chaiscript . Install it. And your Atom is ready for ChaiScript.

I do believe that Sublime Text also has a plugin for ChaiScript. But I have never tried it so I’m not sure about that.

screenshot-from-2017-02-18-00-37-18
On Atom. Find the package. Install it!

An example ChaiScript program

This is 15 bottles of bear in ChaiScript. Why 15? Just so the terminal won’t be owned by beers. 🙂

Note that WordPress does not support syntax highlighting on ChaiScript. Therefor JavaScript syntax highlighting is applied as a workaround.

var beerCount = 15
while(beerCount != 0)
{
 puts(beerCount)
 puts(" bottles of beer on the wall, ")
 puts(beerCount)
 puts(" bottles of beer.\n")
 puts("Take one down and pass it around, ")

 beerCount = beerCount - 1
 if(beerCount == 0)
 {
  puts("no more")
 }
 else
 {
  puts(beerCount)
 }
 puts(" bottles of beer on the wall.\n\n")
}

puts("No more bottles of beer on the wall, no more bottles of beer. \n")
puts("Go to the store and buy some more, 99 bottles of beer on the wall.\n")
puts('\n')

Save this as beer.chai. Then execute it by typing ./chaiexec beer.chai . Then you should see your beers counting down.

screenshot-from-2017-02-18-12-29-03
As expected. Beers all over the place

Anyone reading this blog should be able to follow the code. I’ll explain it belfr now.  If the syntax or anything of it seems weird to you. I’ll be explaining everything in detail in the next tutorial.

ChaiScript is a C-like language with some variance. The first difference is that there are no semicolons!  Just like JavaScript, ChaiScript does not require a semicolon being placed after any expression. Semicolons are used in ChaiScript for putting two or more expressions in the same line. Ex: puts("hello");puts("world").

The second difference that differs ChaiScript from C is that it’s variables are automatically typed. However it is also a strong-type system just like C. A variable cannot change it’s type after initialized. I’ll explain this later. Just remember that its a strong-type system.

A the third difference is that ANY expression after an if, else , for, while , try, etc… have to be surrounded by curly braces (the {}). Unlike C, where if only one expression is putted after that, the curly braces can be ignored. Everything else works just like C (in fact ChaiScript works more like JavaScript)

Walkthrough our example

ChaiScript executes from the first line just as any other scripting language does. At the first line of code. We declare a variable called beerCount and assign the value 15 to it.

var beerCount = 15

Then we arrive at the start of a while loop. Which states that unless beerCount is equal to 0. The loop will continue to be executed. Which is written as

while(beerCount != 0)

Next, we print out some message.

puts(beerCount)
puts(" bottles of beer on the wall, ")
puts(beerCount)
puts(" bottles of beer.\n")
puts("Take one down and pass it around, ")

puts is a function builtin to ChaiScript. As the name suggests, it prints the parameter it taken to screen(stdout). puts can take in differen kinds of parameter. We are passing in integers and strings here. Also, puts won’t put a \n character at the end automatically. You’ll have you put it there yourself.

After printing the message. We see the beerCount = beerCount - 1 statement. Which simply assigns beercount-1 to beerCount. And, according to C common sense. should be equal to beerCount-- or  --beerCount.  That will be correct but, beerCount-- isn’t defined in ChaiScript. Only --beerCount is available.

Then, we found us at the if statement. It states that if  beerCount is 0, execute puts("no more"). Otherwise print how many beer is left. Note that the curly braces have to be there even if we only have one line of code in the if and else statement. It is a bit annoying if that’s not your coding style but we will have to live with it.

Last, after the interpolator executed the loop 15 times. beerCount finally reaches 0. We leave the loop and print some message.

puts("No more bottles of beer on the wall, no more bottles of beer. \n")
puts("Go to the store and buy some more, 99 bottles of beer on the wall.\n")
puts('\n')

Notice that just like C, in ChaiScript "" and '' have different meanings. "" indicates that it is a string and '' indicates that is is a character. Thus we can only put a single character in ''(\n in this case).

Some more fun with ChaiScript

Here is FizzBuzz in ChaiScript

for(var i=0;i&lt;=100;++i)
{
 if(i%3==0 &&& i%5==0)
 {
  puts("FizzBuzz")
 }
 else if(i%3==0)
 {
  puts("Fizz")
 }
 else if(i%5==0)
 {
  puts("Buzz")
 }
 else
 {
  puts(i)
 }
 puts(", ")
}

And this is a program that estimates PI b integrating this \int_{0}^{1} \frac{4}{1-x^2}\text{d}x

var sum = 0.0
var x = 0.0
var STEPS = 10000
var delta = 1.0/STEPS
for(var i=0;i<STEPS;++i)
{
 x = (i+0.5)*delta
 sum += 4.0/(1.0+x*x)
}
var pi = delta * sum
puts("PI = ")
puts(pi)
puts("\n")

Try to follow the code. You will learn a lot form it.

This concludes this tutorial. We will be learning about the ChaiScript language in detail in the next tutorial. Be ready for it! You will be information overloaded. It is available here.

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: