Project

General

Profile

Scol Language particular syntax » History » Version 4

arkeon, 12/10/2011 07:23 PM

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