Project

General

Profile

Scol Language particular syntax » History » Version 5

iri, 12/16/2011 12:13 PM

1 5 iri
author : 
2
date :
3
updated : iri, october 2011
4
updated : iri, december 2011
5
6 1 arkeon
h1. Scol Language particular syntax
7
8
Scol is a functional language, with static types and polymorphic.
9
10 3 iri
{{toc}}
11
12 1 arkeon
h2. Syntax
13
14
Scol determine independently the expected types.
15
16
The basic types are:
17 2 arkeon
<pre>
18
I       : integer
19
S       : string
20
F       : float
21
Chn     : SCOL channel
22
Srv     : SCOL server
23
Env     : SCOL environment
24
Comm    : communication
25 1 arkeon
[u0 u1] : Tuples where u0, u1 are Scol types
26
[u0 r1] : List where u0 is a Scol type
27 2 arkeon
</pre>
28 1 arkeon
29
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.
30
31
h2. Data Structures
32
33 3 iri
h3. With _struct_ :
34
35 1 arkeon
The data structures are declared as follows:
36
<pre>
37
struct MYDATA [ MD_sName : S, MD_iValue : I] mkMYDATA;;
38
</pre>
39
40
You can access an element of a structure by ".MD_sName" for example:
41
<pre>
42
struct MYDATA [ MD_sName : S, MD_iValue : I] mkMYDATA;;
43
fun crData(name, value)=
44
  mkMYDATA [name value]
45
;;
46
47
fun getDataName(mydatastr)=
48
  mydatastr.MD_sName
49
;;
50
51
fun main()=
52
  let crData "name" 1 -> mydatastr in
53
    _fooS strcat ">>> nom : " (getDataName mydatastr);
54
 0;;
55
</pre>
56
57 3 iri
Note : you *must initialize* your structure *before* use it !
58
59
h3. With _typedef_ :
60
61
Definition :
62
<pre>
63
typedef NewType =
64
T1 type1
65
| T2 type2
66
| T3 type3
67
| Tn;;
68
</pre>
69
70
To access it, use _match_  :
71
<pre>fun maFonction (X)=
72
	match X with
73
	(T1 e -> instruction1)
74
	| (T2 e -> instruction2)
75
	| (T3 e -> instruction3)
76
	| (TN e -> instructionN)
77
	| (_ -> instructionDefault);;
78
</pre>
79
80
For example :
81
<pre>typedef NewType =	/* New scol type named 'NewType'. It can set by a "subtype" (in fact in function) T1, T2, ... */
82
T1 I
83
| T2 F
84
| T3 S
85
| Tn;;
86
87
typeof X = NewType;;	/* Global variable declaration (see below) */
88
89
fun maFonction ()=
90
	match X with
91
	(T1 e -> (_fooS "X is an integer"; 0)
92
	| (T2 e -> (_fooS "X is a float"; 0)
93
	| (T3 e -> (_fooS "X is a string"; 0)
94
	| (_ -> (_fooS "X is another thing"; 0);;
95
</pre>
96
97 1 arkeon
h2. Operators
98
99
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 "."
100
<pre>
101
fun addfloat(v1, v2)=
102
  v1 +. v2   
103
;;
104
105
fun addint(v1, v2)=
106
  v1 + v2   
107
;;
108
</pre>
109
110 3 iri
For the string, you could use this :
111
<pre>
112
fun addString (s1, s2)=
113
  strcat s1 s2;;
114
</pre>
115
116 1 arkeon
h2. Global variables
117
118
There are different ways to declare global variables, the first by the type of the variable and the second by an initial value.
119
<pre>
120 3 iri
// Declaring a variable by value, here a Int type (because the given value is an integer)
121 1 arkeon
var iMyVar = 0;;
122
123 3 iri
// Declaring a variable by value, here a String type (because the given value is an string)
124 1 arkeon
var sMyVar = "Hello World";;
125
126
// Declaring a variable by a type
127
typeof iMyVar = I;;
128
typeof sMyVar = S;;
129
</pre>
130
131 3 iri
To set a value to these varaible, use *set* function (it's a function even if the return is ignored usually) :
132
<pre>set sMyVar = "Hello World";
133
set iMyVar = 0;</pre>
134
135 4 arkeon
h2. Locale variables
136 1 arkeon
137
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.
138
<pre>
139
fun main()=
140
  let "Hello World" -> text in
141
  {
142
    _fooS ">>> local variable";  //Without the brace the "text" variable would not be known at the next line
143
    _fooS strcat ">>> value : " text;
144
  };
145
  0;;
146
</pre>
147
148
h2. If logic
149
150
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.
151
<pre>
152
fun main()=
153
  let 1 -> i in
154
  if (i == 1) then
155
     set i = 0
156
  else nil;
157
  
158
  let 1 -> i in
159
  if (i >= 1) then
160
  {
161
     if (i == 1) then nil else
162
       set i = i - 1;
163
  }
164
  else nil;
165
  0;;
166
</pre>
167
168
h2. While loop
169
170
The syntax of the loop is composed of "while" followed by "do":
171
<pre>
172
fun main()=
173
  let 0 -> i in
174
  while (i <= 10) do
175
  {
176
    _fooS strcat ">>> i value : " (itoa i);
177
    set i = i + 1;
178
  };
179
 0;;
180
</pre>
181
182
h2. Lists
183
184
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".
185
<pre>
186
var lMyList = "elem1"::"elem2"::"elem3"::nil;;
187
typeof lMyList2 = [S r1];;
188
189
fun main()=
190
  // parsing, we copy the list into a local variable to avoid modifying the global list
191
  let lMyList -> l in
192
  while (l != nil) do
193
  {
194
    // we get the first element
195
    let hd l -> elem in
196
    {
197
      _fooS strcat ">>> elem : " elem;
198
199
      // we add "elem" to lMyList2
200
      set lMyList2 = elem::lMyList2;
201
    };
202
    // we remove the first element of l
203
    set l = tl l;
204
  };
205
206
  // another method for the same result
207
  let lMyList -> l in
208
  while (l != nil) do
209
  {
210
    // we get the first element
211
    let l -> [elem next] in
212
    {
213
      _fooS strcat ">>> elem : " elem;
214
215
      // we add "elem" to lMyList2
216
      set lMyList2 = elem::lMyList2;
217
218
      // we remove the first element of l
219
      set l = next;
220
    };
221
  };
222
223
  // another method for the same result
224
  let sizelist lMyList -> size in
225
  let 0 -> i in
226
  while (i < size) do
227
  {
228
    // we get the first element at the i position in the list
229
    let nth_list lMyList i -> elem in
230
    {
231
      _fooS strcat ">>> elem : " elem;
232
      // we add "elem" to lMyList2
233
      set lMyList2 = elem::lMyList2;
234
    }
235
    // increment i
236
    set i = i + 1;
237 2 arkeon
  };
238
  0;;
239 1 arkeon
</pre>
240
241 3 iri
Note : to test if a list is ended, compare it with nil :
242
<pre>
243
if myList == nil then
244
  // to do something when the list is empty
245
else
246
  // to do another thing when the list is not empty
247
</pre>
248
249 2 arkeon
h2. Recursive functions
250
251 3 iri
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. The recursive function is very often used and this is natural in Scol.
252 2 arkeon
<pre>
253
var lMyList = "elem1"::"elem2"::"elem3"::nil;;
254
typeof lMyList2 = [S r1];;
255
256
fun recursiveParse(l)=
257
  if l == nil then nil else
258
  let l -> [elem next] in
259
  {
260
    _fooS strcat ">>> elem : " elem;
261
262
    // we add "elem" to lMyList2
263
    set lMyList2 = elem::lMyList2;
264
265
    // we recall the function with the following data to be processed
266
    recursiveParse next;
267
  };;
268
269
fun main()=
270
  recursiveParse lMyList;
271
  0;;
272
</pre>
273
274
h2. Callbacks
275
276
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.
277
<pre>
278
typeof cbMyfun = fun [S] I;;
279
280
fun cbTest1(svalue)=
281
  _fooS strcat ">>> value : " svalue;
282
  0;;
283
284
fun main()=
285 1 arkeon
  set cbMyFun = @cbTest1;
286
  exec cbMyFun with ["Hello world !"];
287
  0;;
288 3 iri
</pre>
289
290
h2. Prototype
291
292
A function must be defined before use it. Sometimes, we can not do it. In this case, the function must be prototyped before use it (but it must be defined in the current environment ! Otherwise error "empty prototype" !)
293
294
<pre>
295
proto myFunctionPrototyped = fun [u0] u1;;
296 1 arkeon
</pre>