Project

General

Profile

Actions

Scol Language particular syntax » History » Revision 1

Revision 1/7 | Next »
arkeon, 04/29/2011 06:57 PM


Scol Language particular syntax

Scol is a functional language, with static types and polymorphic.

Syntax

Scol determine independently the expected types.

The basic types are:

I : integer
S : string
F : float
Chn : SCOL channel
Srv : SCOL server
Env : SCOL environment
Comm : communication
[u0 u1] : Tuples where u0, u1 are Scol types
[u0 r1] : List where u0 is a Scol type

Types can be declared by the addition of Scol plugins (dll) or by the use of "typedef" or structures "struct". "nil" can claim any of these types, for example it will be used to change the value of a variable to null or test if a variable has been initialized.

Data Structures

The data structures are declared as follows:

struct MYDATA [ MD_sName : S, MD_iValue : I] mkMYDATA;;

You can access an element of a structure by ".MD_sName" for example:

struct MYDATA [ MD_sName : S, MD_iValue : I] mkMYDATA;;
fun crData(name, value)=
  mkMYDATA [name value]
;;

fun getDataName(mydatastr)=
  mydatastr.MD_sName
;;

fun main()=
  let crData "name" 1 -> mydatastr in
    _fooS strcat ">>> nom : " (getDataName mydatastr);
 0;;

Operators

In Scol, standard operators are supported, it is also possible to use masks (&, |, ~) and bit shifts (>>, <<). The only truly special case concern the float, it will use the standard operators followed by a "."

fun addfloat(v1, v2)=
  v1 +. v2   
;;

fun addint(v1, v2)=
  v1 + v2   
;;

Global variables

There are different ways to declare global variables, the first by the type of the variable and the second by an initial value.

// Declaring a variable by value, here a Int type
var iMyVar = 0;;

// Declaring a variable by value, here a String type
var sMyVar = "Hello World";;

// Declaring a variable by a type
typeof iMyVar = I;;
typeof sMyVar = S;;

Variables locales

A local variable declared with "let value -> varname in ", has a lifetime limited to the following statement. The Scol language set this limit by ";" or end of a bracket or brace.

fun main()=
  let "Hello World" -> text in
  {
    _fooS ">>> local variable";  //Without the brace the "text" variable would not be known at the next line
    _fooS strcat ">>> value : " text;
  };
  0;;

If logic

When using the "if" test, Scol has the distinction of require to write the complete test. "if" without the "else" or without the "then " will not compile. Another special case, the return value in "then" and "else" should contain the same type.

fun main()=
  let 1 -> i in
  if (i == 1) then
     set i = 0
  else nil;

  let 1 -> i in
  if (i >= 1) then
  {
     if (i == 1) then nil else
       set i = i - 1;
  }
  else nil;
  0;;

While loop

The syntax of the loop is composed of "while" followed by "do":

fun main()=
  let 0 -> i in
  while (i <= 10) do
  {
    _fooS strcat ">>> i value : " (itoa i);
    set i = i + 1;
  };
 0;;

Lists

The use of the lists is very common in Scol, it can, unlike arrays, change its size dynamically. In scol lists are nested tuples, this means that we can treat a list as a tuple of two components, the first will contain the first item in the list and the second then rest of the list without the first element. A list can contain any type and always ends with "nil".

var lMyList = "elem1"::"elem2"::"elem3"::nil;;
typeof lMyList2 = [S r1];;

fun main()=
  // parsing, we copy the list into a local variable to avoid modifying the global list
  let lMyList -> l in
  while (l != nil) do
  {
    // we get the first element
    let hd l -> elem in
    {
      _fooS strcat ">>> elem : " elem;

      // we add "elem" to lMyList2
      set lMyList2 = elem::lMyList2;
    };
    // we remove the first element of l
    set l = tl l;
  };

  // another method for the same result
  let lMyList -> l in
  while (l != nil) do
  {
    // we get the first element
    let l -> [elem next] in
    {
      _fooS strcat ">>> elem : " elem;

      // we add "elem" to lMyList2
      set lMyList2 = elem::lMyList2;

      // we remove the first element of l
      set l = next;
    };
  };

  // another method for the same result
  let sizelist lMyList -> size in
  let 0 -> i in
  while (i < size) do
  {
    // we get the first element at the i position in the list
    let nth_list lMyList i -> elem in
    {
      _fooS strcat ">>> elem : " elem;
      // we add "elem" to lMyList2
      set lMyList2 = elem::lMyList2;
    }
    // increment i
    set i = i + 1;
  };
  0;;

Updated by arkeon about 13 years ago · 1 revisions