Hello world in Scol » History » Version 1
iri, 09/24/2012 12:35 AM
1 | 1 | iri | h1. Hello world in Scol |
---|---|---|---|
2 | |||
3 | We write our first package ! |
||
4 | |||
5 | A package contains only a source code, written in Scol. This content will be read, loaded, compiled, byte-coded and run by the Scol compiler included in each Scol virtual machine. |
||
6 | |||
7 | In Scol, all is functions (this is a functionnal language) ! |
||
8 | A function must always return a result. This result must always be of the same type. Any code outside a function will produce a fatal error ! |
||
9 | |||
10 | Several primitives types exist : |
||
11 | * *I* : the integer type (like C-int) : 10 (-5) 'A |
||
12 | * *F* : the floatting point type (like C-float) : 50.25 (-0.1) |
||
13 | * *S* : the string type (like C-char*) : "Bob and Alice are just married" |
||
14 | * and some others particular types (*Env*, *Chn*, *Srv* and *Comm*). |
||
15 | |||
16 | Of course, we can build any types which we need. |
||
17 | |||
18 | The variables (they are for Scol like functions) are either globals or locals. |
||
19 | A locale variable is known only in the next instruction. This instruction can be a single instruction or a block of instructions. |
||
20 | A globale variable is known in all code after its declaration. |
||
21 | |||
22 | A variable, locale or globale, can not change its type. If we try this, the compiler stops and the application exits with an error. |
||
23 | |||
24 | Each function and each variable must be known by the compiler **before** be used. The source code is not read as a procedural manner, be careful ! |
||
25 | |||
26 | h2. How to write a function ? |
||
27 | |||
28 | This is always the same way : |
||
29 | |||
30 | <pre> |
||
31 | fun <name_of_the_function> (list_of_arguments) = |
||
32 | // instructions |
||
33 | ;; |
||
34 | </pre> |
||
35 | |||
36 | *fun* is a reserved keyword. |
||
37 | The function name must have only the Ascii characters only, without space ([a-zA-Z0-9). |
||
38 | A comma is between each argument. For example : |
||
39 | |||
40 | <pre> |
||
41 | fun myFunction (name, nickname)= |
||
42 | instructions; |
||
43 | ;; |
||
44 | </pre> |
||
45 | |||
46 | All functions are terminated with a double semicolon. |
||
47 | Each instruction (a single instruction or a block of instructions) is terminated with a single semicolon. The last instruction of a function has a double semicolon (the end of the function). |
||
48 | |||
49 | <pre> |
||
50 | fun myOtherFunction (integer, string)= |
||
51 | single_instruction_1; |
||
52 | ( // begin of a block |
||
53 | single_instruction_2; |
||
54 | single_instruction_3; |
||
55 | single_instruction_4; |
||
56 | ); // end of a block |
||
57 | single_instruction_5 |
||
58 | ;; |
||
59 | </pre> |
||
60 | |||
61 | h2. How to call a function ? |
||
62 | |||
63 | To call a function from another function, write its name with, if any, the same number of arguments. |
||
64 | |||
65 | <pre> |
||
66 | fun myFunction (name, nickname)= |
||
67 | instructions; |
||
68 | ;; |
||
69 | |||
70 | fun myOtherFunction ()= |
||
71 | myFunction "William" "Bill"; |
||
72 | instructions |
||
73 | ;; |
||
74 | </pre> |
||
75 | |||
76 | Be careful, arguments must have the "good" type ... We will see this later. |
||
77 | |||
78 | h2. How to declare a variable ? |
||
79 | |||
80 | A global variable can be defined by two ways : |
||
81 | |||
82 | h3. defined by its type : |
||
83 | |||
84 | @typeof myVariable = S;;@ |
||
85 | @typeof myOtherVariable = I;;@ |
||
86 | |||
87 | h3. defined by an initial value : |
||
88 | |||
89 | @var myVariable = "Bob";;@ |
||
90 | @var myOtherVariable = 10;;@ |
||
91 | |||
92 | *typeof* and *var* are two reserved keywords. The end of the declaration is always terminated with a double semicolon. |
||
93 | |||
94 | In the first case, myVariable is nil. nil is a particular value (similar to C-NULL). |
||
95 | In the second case, myVariable is a S type because the initial value is a string (S). |
||
96 | You can not change this. |
||
97 | |||
98 | A global variable is always declared outside a function : |
||
99 | |||
100 | <pre> |
||
101 | typeof myVariable = S;; |
||
102 | var myOtherVariable = 10;; |
||
103 | |||
104 | fun myFunction (name, nickname)= |
||
105 | instructions; |
||
106 | ;; |
||
107 | </pre> |
||
108 | |||
109 | A local variable is always declared in a function. It is known until the end of the next intruction. |
||
110 | @let <initial_value> -> <variable> in@ |
||
111 | |||
112 | <pre> |
||
113 | fun myFunction (address)= |
||
114 | let "Bob" -> myVariable in |
||
115 | ( |
||
116 | let 10 -> myOtherVariable in |
||
117 | instruction_1; |
||
118 | instruction_2; |
||
119 | ); |
||
120 | instruction_3 |
||
121 | ;; |
||
122 | </pre> |
||
123 | |||
124 | In the example above, myVariable is known in instruction_1 and instruction_2 (a block). myOtherVariable is only known in instruction_1. |
||
125 | In the third instruction, myVariable and myOtherVariable can not be used ! |
||
126 | |||
127 | Note : you can declare a variable to nil but the compiler must determine its type without ambiguity. we will see this later. |
||
128 | |||
129 | The variable name must have only the Ascii characters only, without space ([a-zA-Z0-9). |
||
130 | |||
131 | h2. How to set a variable ? |
||
132 | |||
133 | *set* is our friend. set is a particular Scol function. _set_ has a side effect : modify the value of a variable. |
||
134 | |||
135 | <pre> |
||
136 | typeof myVariable = S;; |
||
137 | var myOtherVariable = 10;; |
||
138 | |||
139 | fun myFunction (name, age)= |
||
140 | set myVariable = name; |
||
141 | set myOtherVariable = age |
||
142 | ;; |
||
143 | |||
144 | fun myFunction (name)= |
||
145 | let "Bob" -> myVariable in |
||
146 | ( |
||
147 | instruction_1; |
||
148 | set myVariable = name; |
||
149 | instruction_2; |
||
150 | ); |
||
151 | instruction_3 |
||
152 | ;; |
||
153 | </pre> |
||
154 | |||
155 | h2. How to comment our source code ? |
||
156 | |||
157 | To comment a single line, use // |
||
158 | To comment a mulpiple lines, use /* ... */ |
||
159 | |||
160 | <pre> |
||
161 | var myOtherVariable = 10;; // myOtherVariable is an integer (type I) |
||
162 | |||
163 | /* myVariable is a string (type S) |
||
164 | myOtherVariable is an integer (type I) */ |
||
165 | typeof myVariable = S;; |
||
166 | var myOtherVariable = 10;; |
||
167 | </pre> |
||
168 | |||
169 | h2. How to make our Hello World ? |
||
170 | |||
171 | Of course, there is a lot of ways !... Here, it is not the simplest way but we will practice all this chapter. |
||
172 | |||
173 | Create a new document in your favorite text editor. And write these lines : |
||
174 | |||
175 | <pre> |
||
176 | // We declare a global variable with an initial value, like "World" |
||
177 | var myString = "World";; |
||
178 | |||
179 | /* |
||
180 | This function displays a message in the console. It takes one argument. Note that the type of an argument is never explicitely defined, a function can be polymorphic. The type will be implicitly determined by the compiler from the context. |
||
181 | Arguments are available in the all body of the function (from the = symbol until the double semicolon). |
||
182 | strcat is a Scol function : it concatenates two strings. |
||
183 | The two arguments of strcat must have a type S, so the compiler considers string as a S. |
||
184 | The return value by this function is the return value of its last instruction. _fooS returns a string (S), the function returns also a S. |
||
185 | */ |
||
186 | fun printHello (string)= |
||
187 | _fooS strcat "Hello " string |
||
188 | ;; |
||
189 | |||
190 | /* |
||
191 | print write few message in the console (and the log file). No argument is needed. |
||
192 | In the first instruction, the previous written function is called, the global variable is passed to this one. |
||
193 | Second, a new string is set to our global variable. |
||
194 | Third, we have a new call to printHello. Don't forget, the myString value has changed ... |
||
195 | Next, we define a local variable with "Alice" as initial value. |
||
196 | Finally, we still call printHello with the local variable ... |
||
197 | The return value of print is 0, an integer. This is common to return 0 when the value is no longer used. |
||
198 | We should read in the console : |
||
199 | Hello World |
||
200 | Hello Bob |
||
201 | Hello Alice |
||
202 | */ |
||
203 | fun print ()= |
||
204 | printHello myString; |
||
205 | set myString = "Bob"; |
||
206 | printHello myString; |
||
207 | let "Alice" -> anyString in |
||
208 | printHello anyString; |
||
209 | 0;; |
||
210 | |||
211 | /* |
||
212 | This is the main function. Its name can be anything. It can take 0 or any number of arguments. We can define several main functions if we want. |
||
213 | The main function is a function called from the launcher script (*.scol). |
||
214 | The first instruction shows the console. |
||
215 | The second call the print function |
||
216 | The third directly calls printHello with a customized argument. |
||
217 | main returns 0, an integer. The return value does not affect the following the application. |
||
218 | */ |
||
219 | fun main ()= |
||
220 | _showconsole; |
||
221 | print; |
||
222 | printHello "Scol Community !"; |
||
223 | 0;; |
||
224 | </pre> |
||
225 | |||
226 | Now, save your document to _tutorials/hello_world.pkg_. |
||
227 | |||
228 | Create a new document. This will be the script launcher. |
||
229 | |||
230 | <pre> |
||
231 | # This is a comment |
||
232 | # We load our package. If we have several package, we add several lines ... |
||
233 | _load "tutorials/hello_world.pkg" |
||
234 | # We call the main function. If we want, we can passe any arguments (types I or S only) |
||
235 | main |
||
236 | </pre> |
||
237 | |||
238 | Save it (_tutorials/hello_world.scol_) and launch this file. |
||
239 | |||
240 | License : "CC-BY-SA 2.0":https://creativecommons.org/licenses/by-sa/2.0/ |
||
241 | Tutorial by iri |
||
242 | Updated by / |