lib: adds setenv to environ module
[nit.git] / lib / standard / kernel.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
5 #
6 # This file is free software, which comes along with NIT. This software is
7 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
9 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
10 # is kept unaltered, and a notification of the changes is added.
11 # You are allowed to redistribute it and sell it, alone or is a part of
12 # another product.
13
14 # Most minimal classes and methods.
15 # This module is the root of the standard module hierarchy.
16 package kernel
17
18 import end # Mark this module is a top level one. (must be only one)
19
20 ###############################################################################
21 # System Classes #
22 ###############################################################################
23
24 # The root of the class hierarchy.
25 # Each class implicitly specialize Object.
26 #
27 # Currently, Object is also used to collect all top-level methods.
28 interface Object
29 # The unique object identifier in the class.
30 # Unless specific code, you should not use this method.
31 # The identifier is used internally to provide a hash value.
32 fun object_id: Int is intern
33
34 # Return true if `self' and `other' have the same dynamic type.
35 # Unless specific code, you should not use this method.
36 fun is_same_type(other: Object): Bool is intern
37
38 # Have `self' and `other' the same value?
39 ##
40 # The exact meaning of "same value" is let to the subclasses.
41 # Implicitly, the default implementation, is `is'
42 fun ==(other: nullable Object): Bool do return self is other
43
44 # Have `self' and `other' different values?
45 ##
46 # != is equivalent with "not ==".
47 fun !=(other: nullable Object): Bool do return not (self == other)
48
49 # Display self on stdout (debug only).
50 # This method MUST not be used by programs, it is here for debugging
51 # only and can be removed without any notice
52 fun output
53 do
54 '<'.output
55 object_id.output
56 '>'.output
57 end
58
59 # Display class name on stdout (debug only).
60 # This method MUST not be used by programs, it is here for debugging
61 # only and can be removed without any notice
62 fun output_class_name is intern
63
64 # Quit the program with a specific return code
65 protected fun exit(exit_value: Int) is intern
66
67 # Return the global sys object, the only instance of the `Sys' class.
68 protected fun sys: Sys is intern
69 end
70
71 # The main class of the program.
72 # `Sys' is a singleton class, its only instance is `sys' defined in `Object'.
73 # `sys' is used to invoke methods on the program on the system.
74 class Sys
75 # Instructions outside classes implicitly redefine this method.
76 fun main do end
77 end
78
79 ###############################################################################
80 # Abstract Classes #
81 ###############################################################################
82
83 # The ancestor of class where objects are in a total order.
84 # In order to work, the method '<' has to be redefined.
85 interface Comparable
86 # What `self' can be compared to?
87 type OTHER: Comparable
88
89 # Is `self' lesser than `other'?
90 fun <(other: OTHER): Bool is abstract
91
92 # not `other' < `self'
93 # Note, the implementation must ensure that: (x<=y) == (x<y or x==y)
94 fun <=(other: OTHER): Bool do return not other < self
95
96 # not `self' < `other'
97 # Note, the implementation must ensure that: (x>=y) == (x>y or x==y)
98 fun >=(other: OTHER): Bool do return not self < other
99
100 # `other' < `self'
101 fun >(other: OTHER): Bool do return other < self
102
103 # -1 if <, +1 if > and 0 otherwise
104 # Note, the implementation must ensure that: (x<=>y == 0) == (x==y)
105 fun <=>(other: OTHER): Int
106 do
107 if self < other then
108 return -1
109 else if other < self then
110 return 1
111 else
112 return 0
113 end
114 end
115
116 # c <= self <= d
117 fun is_between(c: OTHER, d: OTHER): Bool
118 do
119 return c <= self and self <= d
120 end
121
122 # The maximum between `self' and `other' (prefers `self' if equals).
123 fun max(other: OTHER): OTHER
124 do
125 if self < other then
126 return other
127 else
128 return self
129 end
130 end
131
132 # The minimum between `self' and `c' (prefer `self' if equals)
133 fun min(c: OTHER): OTHER
134 do
135 if c < self then
136 return c
137 else
138 return self
139 end
140 end
141 end
142
143 # Discrete total orders.
144 interface Discrete
145 super Comparable
146
147 redef type OTHER: Discrete
148
149 # The next element.
150 fun succ: OTHER do return self + 1
151
152 # The previous element.
153 fun prec: OTHER do return self - 1
154
155 # The `i'-th successor element.
156 fun +(i: Int): OTHER is abstract
157
158 # The `i'-th previous element.
159 fun -(i: Int): OTHER is abstract
160
161 # The distance between self and d.
162 # 10.distance(15) # --> 5
163 # 'Z'.distance('A') # --> 25
164 fun distance(d: OTHER): Int
165 do
166 var cursor: OTHER
167 var stop: OTHER
168 if self < d then
169 cursor = self
170 stop = d
171 else if self > d then
172 cursor = d
173 stop = self
174 else
175 return 0
176 end
177
178 var nb = 0
179 while cursor < stop do
180 cursor = cursor.succ
181 nb += 1
182 end
183 return nb
184 end
185 end
186
187 ###############################################################################
188 # Native classes #
189 ###############################################################################
190
191 # Native Booleans.
192 # `true' and `false' are the only instances.
193 # Boolean are manipulated trough three special operators:
194 # `and', `or', `not'.
195 # Booleans are mainly used by conditional statement and loops.
196 universal Bool
197 redef fun object_id is intern
198 redef fun ==(b) is intern
199 redef fun !=(b) is intern
200 redef fun output is intern
201 end
202
203 # Native floating point numbers.
204 # Corresponds to C float.
205 universal Float
206 redef fun object_id is intern
207 redef fun output is intern
208
209 fun <=(i: Float): Bool is intern
210 fun <(i: Float): Bool is intern
211 fun >=(i: Float): Bool is intern
212 fun >(i: Float): Bool is intern
213 fun +(i: Float): Float is intern
214 fun -: Float is intern
215 fun -(i: Float): Float is intern
216 fun *(i: Float): Float is intern
217 fun /(i: Float): Float is intern
218
219 # The integer part of `self'.
220 fun to_i: Int is intern
221 end
222
223 # Native integer numbers.
224 # Correspond to C int.
225 universal Int
226 super Discrete
227 redef type OTHER: Int
228
229 redef fun object_id is intern
230 redef fun ==(i) is intern
231 redef fun !=(i) is intern
232 redef fun output is intern
233
234 redef fun <=(i) is intern
235 redef fun <(i) is intern
236 redef fun >=(i) is intern
237 redef fun >(i) is intern
238 redef fun +(i) is intern
239 fun -: Int is intern
240 redef fun -(i) is intern
241 fun *(i: Int): Int is intern
242 fun /(i: Int): Int is intern
243 fun %(i: Int): Int is intern
244 fun lshift(i: Int): Int is intern
245 fun rshift(i: Int): Int is intern
246
247 # The float equivalent of `self'
248 fun to_f: Float is intern
249
250 redef fun succ is intern
251 redef fun prec is intern
252 redef fun distance(i)
253 do
254 var d = self - i
255 if d >= 0 then
256 return d
257 else
258 return -d
259 end
260 end
261
262 redef fun <=>(other)
263 do
264 if self < other then
265 return -1
266 else if other < self then
267 return 1
268 else
269 return 0
270 end
271 end
272
273 redef fun is_between(c, d)
274 do
275 if self < c or d < self then
276 return false
277 else
278 return true
279 end
280 end
281
282 redef fun max(other)
283 do
284 if self < other then
285 return other
286 else
287 return self
288 end
289 end
290
291 redef fun min(c)
292 do
293 if c < self then
294 return c
295 else
296 return self
297 end
298 end
299
300 # The character whose ASCII value is `self'.
301 fun ascii: Char is intern
302
303 # Number of digits of an integer in base `b' (plus one if negative)
304 fun digit_count(b: Int): Int
305 do
306 var d: Int # number of digits
307 var n: Int # current number
308 # Sign
309 if self < 0 then
310 d = 1
311 n = - self
312 else if self == 0 then
313 return 1
314 else
315 d = 0
316 n = self
317 end
318 # count digits
319 while n > 0 do
320 d += 1
321 n = n / b # euclidian division /
322 end
323 return d
324 end
325
326 # Return the corresponding digit character
327 # If 0 <= `self' <= 9, return the corresponding character.
328 # If 10 <= `self' <= 36, return the corresponding letter [a..z].
329 fun to_c: Char
330 do
331 assert self >= 0 and self <= 36 # TODO plan for this
332 if self < 10 then
333 return (self + '0'.ascii).ascii
334 else
335 return (self + ('a'.ascii - 10)).ascii
336 end
337 end
338
339 # Execute 'each' for each integer in [self..last]
340 fun enumerate_to(last: Int)
341 !each(i: Int)
342 do
343 var cur = self
344 while cur <= last do
345 each(cur)
346 cur += 1
347 end
348 end
349
350 # Execute 'each' for each integer in [self..after[
351 fun enumerate_before(after: Int)
352 !each(i: Int)
353 do
354 var cur = self
355 while cur < after do
356 each(cur)
357 cur += 1
358 end
359 end
360
361 # The absolute value of self
362 fun abs: Int
363 do
364 if self >= 0
365 then
366 return self
367 else
368 return -1 * self
369 end
370 end
371 end
372
373 # Native characters.
374 # Characters are denoted with simple quote.
375 # eg. 'a' or '\n'.
376 universal Char
377 super Discrete
378 redef type OTHER: Char
379
380 redef fun object_id is intern
381 redef fun ==(o) is intern
382 redef fun !=(o) is intern
383 redef fun output is intern
384
385 redef fun <=(i) is intern
386 redef fun <(i) is intern
387 redef fun >=(i) is intern
388 redef fun >(i) is intern
389
390 redef fun succ is intern
391 redef fun prec is intern
392
393 redef fun distance(c)
394 do
395 var d = self.ascii - c.ascii
396 if d >= 0 then
397 return d
398 else
399 return -d
400 end
401 end
402
403 # If `self' is a digit then return this digit else return -1.
404 fun to_i: Int
405 do
406
407 if self == '-' then
408 return -1
409 else if is_digit then
410 return self.ascii - '0'.ascii
411 else
412 return self.to_lower.ascii - ('a'.ascii + 10)
413 end
414 end
415
416 # the ascii value of self
417 fun ascii: Int is intern
418
419 redef fun +(i) is intern
420 redef fun -(i) is intern
421
422 # Return the lower case version of self.
423 # If self is not a letter, then return self
424 fun to_lower: Char
425 do
426 if is_upper then
427 return (ascii + ('a'.distance('A'))).ascii
428 else
429 return self
430 end
431 end
432
433 # Return the upper case version of self.
434 # If self is not a letter, then return self
435 fun to_upper: Char
436 do
437 if is_lower then
438 return (ascii - ('a'.distance('A'))).ascii
439 else
440 return self
441 end
442 end
443
444 # Is self a digit? (from '0' to '9')
445 fun is_digit : Bool
446 do
447 return self >= '0' and self <= '9'
448 end
449
450 # Is self a lower case letter? (from 'a' to 'z')
451 fun is_lower : Bool
452 do
453 return self >= 'a' and self <= 'z'
454 end
455
456 # Is self a upper case letter? (from 'A' to 'Z')
457 fun is_upper : Bool
458 do
459 return self >= 'A' and self <= 'Z'
460 end
461
462 # Is self a letter? (from 'A' to 'Z' and 'a' to 'z')
463 fun is_letter : Bool
464 do
465 return is_lower or is_upper
466 end
467 end
468
469 # Pointer classes are used to manipulate extern C structures.
470 extern Pointer
471 end