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