Project

General

Profile

Scol Language particular syntax » History » Version 1

arkeon, 04/29/2011 06:57 PM

1 1 arkeon
h1. Scol Language particular syntax
2
3
Scol is a functional language, with static types and polymorphic.
4
5
h2. Syntax
6
7
Scol determine independently the expected types.
8
9
The basic types are:
10
11
I : integer
12
S : string
13
F : float
14
Chn : SCOL channel
15
Srv : SCOL server
16
Env : SCOL environment
17
Comm : communication
18
[u0 u1] : Tuples where u0, u1 are Scol types
19
[u0 r1] : List where u0 is a Scol type
20
21
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.
22
23
h2. Data Structures
24
25
The data structures are declared as follows:
26
<pre>
27
struct MYDATA [ MD_sName : S, MD_iValue : I] mkMYDATA;;
28
</pre>
29
30
You can access an element of a structure by ".MD_sName" for example:
31
<pre>
32
struct MYDATA [ MD_sName : S, MD_iValue : I] mkMYDATA;;
33
fun crData(name, value)=
34
  mkMYDATA [name value]
35
;;
36
37
fun getDataName(mydatastr)=
38
  mydatastr.MD_sName
39
;;
40
41
fun main()=
42
  let crData "name" 1 -> mydatastr in
43
    _fooS strcat ">>> nom : " (getDataName mydatastr);
44
 0;;
45
</pre>
46
47
h2. Operators
48
49
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 "."
50
<pre>
51
fun addfloat(v1, v2)=
52
  v1 +. v2   
53
;;
54
55
fun addint(v1, v2)=
56
  v1 + v2   
57
;;
58
</pre>
59
60
h2. Global variables
61
62
There are different ways to declare global variables, the first by the type of the variable and the second by an initial value.
63
<pre>
64
// Declaring a variable by value, here a Int type
65
var iMyVar = 0;;
66
67
// Declaring a variable by value, here a String type
68
var sMyVar = "Hello World";;
69
70
// Declaring a variable by a type
71
typeof iMyVar = I;;
72
typeof sMyVar = S;;
73
</pre>
74
75
h2. Variables locales
76
77
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.
78
<pre>
79
fun main()=
80
  let "Hello World" -> text in
81
  {
82
    _fooS ">>> local variable";  //Without the brace the "text" variable would not be known at the next line
83
    _fooS strcat ">>> value : " text;
84
  };
85
  0;;
86
</pre>
87
88
h2. If logic
89
90
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.
91
<pre>
92
fun main()=
93
  let 1 -> i in
94
  if (i == 1) then
95
     set i = 0
96
  else nil;
97
  
98
  let 1 -> i in
99
  if (i >= 1) then
100
  {
101
     if (i == 1) then nil else
102
       set i = i - 1;
103
  }
104
  else nil;
105
  0;;
106
</pre>
107
108
h2. While loop
109
110
The syntax of the loop is composed of "while" followed by "do":
111
<pre>
112
fun main()=
113
  let 0 -> i in
114
  while (i <= 10) do
115
  {
116
    _fooS strcat ">>> i value : " (itoa i);
117
    set i = i + 1;
118
  };
119
 0;;
120
</pre>
121
122
h2. Lists
123
124
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".
125
<pre>
126
var lMyList = "elem1"::"elem2"::"elem3"::nil;;
127
typeof lMyList2 = [S r1];;
128
129
fun main()=
130
  // parsing, we copy the list into a local variable to avoid modifying the global list
131
  let lMyList -> l in
132
  while (l != nil) do
133
  {
134
    // we get the first element
135
    let hd l -> elem in
136
    {
137
      _fooS strcat ">>> elem : " elem;
138
139
      // we add "elem" to lMyList2
140
      set lMyList2 = elem::lMyList2;
141
    };
142
    // we remove the first element of l
143
    set l = tl l;
144
  };
145
146
  // another method for the same result
147
  let lMyList -> l in
148
  while (l != nil) do
149
  {
150
    // we get the first element
151
    let l -> [elem next] in
152
    {
153
      _fooS strcat ">>> elem : " elem;
154
155
      // we add "elem" to lMyList2
156
      set lMyList2 = elem::lMyList2;
157
158
      // we remove the first element of l
159
      set l = next;
160
    };
161
  };
162
163
  // another method for the same result
164
  let sizelist lMyList -> size in
165
  let 0 -> i in
166
  while (i < size) do
167
  {
168
    // we get the first element at the i position in the list
169
    let nth_list lMyList i -> elem in
170
    {
171
      _fooS strcat ">>> elem : " elem;
172
      // we add "elem" to lMyList2
173
      set lMyList2 = elem::lMyList2;
174
    }
175
    // increment i
176
    set i = i + 1;
177
  };
178
  0;;
179
</pre>