Project

General

Profile

Actions

Hello world in Scol

We write our first package !

A package contains only a source code, written in Scol. This content will be read, loaded, compiled, byte-coded and run by the Scol compiler included in each Scol virtual machine.

In Scol, all is functions (this is a functionnal language) !
A function must always return a result. This result must always be of the same type. Any code outside a function will produce a fatal error !

Several primitives types exist :
  • I : the integer type (like C-int) : 10 (-5) 'A
  • F : the floatting point type (like C-float) : 50.25 (-0.1)
  • S : the string type (like C-char*) : "Bob and Alice are just married"
  • and some others particular types (Env, Chn, Srv and Comm).

Of course, we can build any types which we need.

The variables (they are for Scol like functions) are either globals or locals.
A locale variable is known only in the next instruction. This instruction can be a single instruction or a block of instructions.
A globale variable is known in all code after its declaration.

A variable, locale or globale, can not change its type. If we try this, the compiler stops and the application exits with an error.

Each function and each variable must be known by the compiler before be used. The source code is not read as a procedural manner, be careful !

Scol is always case sensitive.

How to write a function ?

This is always the same way :

fun <name_of_the_function> (list_of_arguments) =
    // instructions
;;

fun is a reserved keyword.
The function name must have only the Ascii characters only, without space ([a-zA-Z0-9).
A comma is between each argument. For example :

fun myFunction (name, nickname)=
    instructions;
    ;;

All functions are terminated with a double semicolon.
Each instruction (a single instruction or a block of instructions) is terminated with a single semicolon. The last instruction of a function has a double semicolon (the end of the function).

fun myOtherFunction (integer, string)=
    single_instruction_1;
    (    // begin of a block
        single_instruction_2;
        single_instruction_3;
        single_instruction_4;
    );    // end of a block
    single_instruction_5
    ;;

How to call a function ?

To call a function from another function, write its name with, if any, the same number of arguments.

fun myFunction (name, nickname)=
    instructions;
    ;;

fun myOtherFunction ()=
    myFunction "William" "Bill";
    instructions
    ;;

Be careful, arguments must have the "good" type ... We will see this later.

How to declare a variable ?

A global variable can be defined by two ways :

defined by its type :

typeof myVariable = S;;
typeof myOtherVariable = I;;

defined by an initial value :

var myVariable = "Bob";;
var myOtherVariable = 10;;

typeof and var are two reserved keywords. The end of the declaration is always terminated with a double semicolon.

In the first case, myVariable is nil. nil is a particular value (similar to C-NULL).
In the second case, myVariable is a S type because the initial value is a string (S).
You can not change this.

A global variable is always declared outside a function :

typeof myVariable = S;;
var myOtherVariable = 10;;

fun myFunction (name, nickname)=
    instructions;
    ;;

A local variable is always declared in a function. It is known until the end of the next intruction.
let <initial_value> -> <variable> in

fun myFunction (address)=
    let "Bob" -> myVariable in
    (
        let 10 -> myOtherVariable in
        instruction_1;
        instruction_2;
    );
    instruction_3
    ;;

In the example above, myVariable is known in instruction_1 and instruction_2 (a block). myOtherVariable is only known in instruction_1.
In the third instruction, myVariable and myOtherVariable can not be used !

Note : you can declare a variable to nil but the compiler must determine its type without ambiguity. we will see this later.

The variable name must have only the Ascii characters only, without space ([a-zA-Z0-9).

How to set a variable ?

set is our friend. set is a particular Scol function. set has a side effect : modify the value of a variable.

typeof myVariable = S;;
var myOtherVariable = 10;;

fun myFunction (name, age)=
    set myVariable = name;
    set myOtherVariable = age
    ;;

fun myFunction (name)=
    let "Bob" -> myVariable in
    (
        instruction_1;
        set myVariable = name;
        instruction_2;
    );
    instruction_3
    ;;

How to comment our source code ?

To comment a single line, use //
To comment a mulpiple lines, use /* ... */

var myOtherVariable = 10;;     // myOtherVariable is an integer (type I)

/* myVariable is a string (type S)
myOtherVariable is an integer (type I) */
typeof myVariable = S;;
var myOtherVariable = 10;;

How to make our Hello World ?

Of course, there is a lot of ways !... Here, it is not the simplest way but we will practice all this chapter.

Create a new document in your favorite text editor. And write these lines :

// We declare a global variable with an initial value, like "World" 
var myString = "World";;

/*
This function displays a message in the console. It takes one argument. Note that the type of an argument is never explicitely defined, a function can be polymorphic. The type will be implicitly determined by the compiler from the context.
Arguments are available in the all body of the function (from the = symbol until the double semicolon).
strcat is a Scol function : it concatenates two strings.
The two arguments of strcat must have a type S, so the compiler considers string as a S.
The return value by this function is the return value of its last instruction. _fooS returns a string (S), the function returns also a S.
*/
fun printHello (string)=
    _fooS strcat "Hello " string
    ;;

/*
print write few message in the console (and the log file). No argument is needed.
In the first instruction, the previous written function is called, the global variable is passed to this one.
Second, a new string is set to our global variable.
Third, we have a new call to printHello. Don't forget, the myString value has changed ...
Next, we define a local variable with "Alice" as initial value.
Finally, we still call printHello with the local variable ...
The return value of print is 0, an integer. This is common to return 0 when the value is no longer used.
We should read in the console :
Hello World
Hello Bob
Hello Alice
*/
fun print ()=
    printHello myString;
    set myString = "Bob";
    printHello myString;
    let "Alice" -> anyString in
    printHello anyString;
    0;;

/*
This is the main function. Its name can be anything. It can take 0 or any number of arguments. We can define several main functions if we want.
The main function is a function called from the launcher script (*.scol).
The first instruction shows the console.
The second call the print function
The third directly calls printHello with a customized argument.
main returns 0, an integer. The return value does not affect the following the application.
*/
fun main ()=
    _showconsole;
    print;
    printHello "Scol Community !";
    0;;

Now, save your document to tutorials/hello_world.pkg.

Create a new document. This will be the script launcher.

# This is a comment
# We load our package. If we have several package, we add several lines ...
_load "tutorials/hello_world.pkg" 
# We call the main function. If we want, we can passe any arguments (types I or S only)
main

Save it (tutorials/hello_world.scol) and launch this file.

License : CC-BY-SA 2.0
Tutorial by iri
Updated by /

Updated by iri about 12 years ago · 2 revisions