Project

General

Profile

Scol Language particular syntax » History » Version 2

arkeon, 04/29/2011 07:01 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 2 arkeon
<pre>
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 1 arkeon
[u0 u1] : Tuples where u0, u1 are Scol types
19
[u0 r1] : List where u0 is a Scol type
20 2 arkeon
</pre>
21 1 arkeon
22
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.
23
24
h2. Data Structures
25
26
The data structures are declared as follows:
27
<pre>
28
struct MYDATA [ MD_sName : S, MD_iValue : I] mkMYDATA;;
29
</pre>
30
31
You can access an element of a structure by ".MD_sName" for example:
32
<pre>
33
struct MYDATA [ MD_sName : S, MD_iValue : I] mkMYDATA;;
34
fun crData(name, value)=
35
  mkMYDATA [name value]
36
;;
37
38
fun getDataName(mydatastr)=
39
  mydatastr.MD_sName
40
;;
41
42
fun main()=
43
  let crData "name" 1 -> mydatastr in
44
    _fooS strcat ">>> nom : " (getDataName mydatastr);
45
 0;;
46
</pre>
47
48
h2. Operators
49
50
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 "."
51
<pre>
52
fun addfloat(v1, v2)=
53
  v1 +. v2   
54
;;
55
56
fun addint(v1, v2)=
57
  v1 + v2   
58
;;
59
</pre>
60
61
h2. Global variables
62
63
There are different ways to declare global variables, the first by the type of the variable and the second by an initial value.
64
<pre>
65
// Declaring a variable by value, here a Int type
66
var iMyVar = 0;;
67
68
// Declaring a variable by value, here a String type
69
var sMyVar = "Hello World";;
70
71
// Declaring a variable by a type
72
typeof iMyVar = I;;
73
typeof sMyVar = S;;
74
</pre>
75
76
h2. Variables locales
77
78
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.
79
<pre>
80
fun main()=
81
  let "Hello World" -> text in
82
  {
83
    _fooS ">>> local variable";  //Without the brace the "text" variable would not be known at the next line
84
    _fooS strcat ">>> value : " text;
85
  };
86
  0;;
87
</pre>
88
89
h2. If logic
90
91
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.
92
<pre>
93
fun main()=
94
  let 1 -> i in
95
  if (i == 1) then
96
     set i = 0
97
  else nil;
98
  
99
  let 1 -> i in
100
  if (i >= 1) then
101
  {
102
     if (i == 1) then nil else
103
       set i = i - 1;
104
  }
105
  else nil;
106
  0;;
107
</pre>
108
109
h2. While loop
110
111
The syntax of the loop is composed of "while" followed by "do":
112
<pre>
113
fun main()=
114
  let 0 -> i in
115
  while (i <= 10) do
116
  {
117
    _fooS strcat ">>> i value : " (itoa i);
118
    set i = i + 1;
119
  };
120
 0;;
121
</pre>
122
123
h2. Lists
124
125
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".
126
<pre>
127
var lMyList = "elem1"::"elem2"::"elem3"::nil;;
128
typeof lMyList2 = [S r1];;
129
130
fun main()=
131
  // parsing, we copy the list into a local variable to avoid modifying the global list
132
  let lMyList -> l in
133
  while (l != nil) do
134
  {
135
    // we get the first element
136
    let hd l -> elem in
137
    {
138
      _fooS strcat ">>> elem : " elem;
139
140
      // we add "elem" to lMyList2
141
      set lMyList2 = elem::lMyList2;
142
    };
143
    // we remove the first element of l
144
    set l = tl l;
145
  };
146
147
  // another method for the same result
148
  let lMyList -> l in
149
  while (l != nil) do
150
  {
151
    // we get the first element
152
    let l -> [elem next] in
153
    {
154
      _fooS strcat ">>> elem : " elem;
155
156
      // we add "elem" to lMyList2
157
      set lMyList2 = elem::lMyList2;
158
159
      // we remove the first element of l
160
      set l = next;
161
    };
162
  };
163
164
  // another method for the same result
165
  let sizelist lMyList -> size in
166
  let 0 -> i in
167
  while (i < size) do
168
  {
169
    // we get the first element at the i position in the list
170
    let nth_list lMyList i -> elem in
171
    {
172
      _fooS strcat ">>> elem : " elem;
173
      // we add "elem" to lMyList2
174
      set lMyList2 = elem::lMyList2;
175
    }
176
    // increment i
177
    set i = i + 1;
178
  };
179 2 arkeon
  0;;
180
</pre>
181
182
h2. Recursive functions
183
184
Recursive functions can avoid the use of intensive loops execution time. Here is an example of a recursive function to process the items in a list based on the previous example.
185
<pre>
186
var lMyList = "elem1"::"elem2"::"elem3"::nil;;
187
typeof lMyList2 = [S r1];;
188
189
fun recursiveParse(l)=
190
  if l == nil then nil else
191
  let l -> [elem next] in
192
  {
193
    _fooS strcat ">>> elem : " elem;
194
195
    // we add "elem" to lMyList2
196
    set lMyList2 = elem::lMyList2;
197
198
    // we recall the function with the following data to be processed
199
    recursiveParse next;
200
  };;
201
202
fun main()=
203
  recursiveParse lMyList;
204
  0;;
205
</pre>
206
207
h2. Callbacks
208
209
Callbacks allow a call to a function defined in a variable. A window object (ObjWin) for example can call a function when it is closed or being moved. It is also possible to define a variable of function type.
210
<pre>
211
typeof cbMyfun = fun [S] I;;
212
213
fun cbTest1(svalue)=
214
  _fooS strcat ">>> value : " svalue;
215
  0;;
216
217
fun main()=
218
  set cbMyFun = @cbTest1;
219
  exec cbMyFun with ["Hello world !"];
220 1 arkeon
  0;;
221
</pre>