update NOTICE and LICENSE
[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 # This module is the root of the standard module hierarchy.
15 package kernel
16
17 import end # Mark this module is a top level one. (must be only one)
18
19 ###############################################################################
20 # System Classes #
21 ###############################################################################
22
23 # The root of the class hierarchy.
24 # Each class implicitely specialize Object.
25 interface Object
26 # The unique object identifier in the class
27 fun object_id: Int is intern
28
29 # Return true is `self' and `other' have the same dynamic type
30 fun is_same_type(other: Object): Bool is intern
31
32 # Have `self' and `other' the same value?
33 ##
34 # Implicitely, the default implementation, is ==
35 fun ==(other: nullable Object): Bool do return self is other
36
37 # Have `self' and `other' different values?
38 ##
39 # != is equivament with "not =".
40 fun !=(other: nullable Object): Bool do return not (self == other)
41
42 # Display self on stdout (debug only).
43 fun output
44 do
45 '<'.output
46 object_id.output
47 '>'.output
48 end
49
50 # Display class name on stdout (debug only).
51 fun output_class_name is intern
52
53 protected fun exit(exit_value: Int) is intern # Quit the program.
54 protected fun sys: Sys is intern # The global sys object
55 end
56
57 # The main class of the program.
58 class Sys
59 # Instructions outside classes implicetely redefine this method.
60 fun main do end
61 end
62
63 ###############################################################################
64 # Abstract Classes #
65 ###############################################################################
66
67 # The ancestor of class where objects are in a total order.
68 # In order to work, the method '<' has to be redefined.
69 interface Comparable
70 type OTHER: Comparable
71
72 # Is `self' lesser than `other'
73 fun <(other: OTHER): Bool is abstract
74
75 # not `other' < `self'
76 fun <=(other: OTHER): Bool do return not other < self
77
78 # not `self' < `other'
79 fun >=(other: OTHER): Bool do return not self < other
80
81 # `other' < `self'
82 fun >(other: OTHER): Bool do return other < self
83
84 # -1 if <, +1 if > and 0 otherwise
85 fun <=>(other: OTHER): Int
86 do
87 if self < other then
88 return -1
89 else if other < self then
90 return 1
91 else
92 return 0
93 end
94 end
95
96 # c <= self <= d
97 fun is_between(c: OTHER, d: OTHER): Bool
98 do
99 return c <= self and self <= d
100 end
101
102 # The maximum between `self' and `other' (prefers `self' if equals).
103 fun max(other: OTHER): OTHER
104 do
105 if self < other then
106 return other
107 else
108 return self
109 end
110 end
111
112 # The minimum between `self' and `c' (prefer `self' if equals)
113 fun min(c: OTHER): OTHER
114 do
115 if c < self then
116 return c
117 else
118 return self
119 end
120 end
121 end
122
123 # Discrete total orders.
124 interface Discrete
125 super Comparable
126
127 redef type OTHER: Discrete
128
129 # The next element.
130 fun succ: OTHER do return self + 1
131
132 # The previous element.
133 fun prec: OTHER do return self - 1
134
135 # The `i' th successor element.
136 fun +(i: Int): OTHER is abstract
137
138 # The `i' th previous element.
139 fun -(i: Int): OTHER is abstract
140
141 # The distance between self and d.
142 # 10.distance(15) # --> 5
143 # 'Z'.distance('A') # --> 25
144 fun distance(d: OTHER): Int
145 do
146 var cursor: OTHER
147 var stop: OTHER
148 if self < d then
149 cursor = self
150 stop = d
151 else if self > d then
152 cursor = d
153 stop = self
154 else
155 return 0
156 end
157
158 var nb = 0
159 while cursor < stop do
160 cursor = cursor.succ
161 nb += 1
162 end
163 return nb
164 end
165 end
166
167 ###############################################################################
168 # Native classes #
169 ###############################################################################
170
171 # Native booleans.
172 # `true' and `false' are the only instances.
173 # Boolean are manipulated trought three special operators:
174 # `and', `or', `not'.
175 # Booleans are mainly used by conditional statement and loops.
176 universal Bool
177 redef fun object_id is intern
178 redef fun ==(b) is intern
179 redef fun !=(b) is intern
180 redef fun output is intern
181 end
182
183 # Native floating point numbers.
184 # Corresponds to C float.
185 universal Float
186 redef fun object_id is intern
187 redef fun output is intern
188
189 fun <=(i: Float): Bool is intern
190 fun <(i: Float): Bool is intern
191 fun >=(i: Float): Bool is intern
192 fun >(i: Float): Bool is intern
193 fun +(i: Float): Float is intern
194 fun -: Float is intern
195 fun -(i: Float): Float is intern
196 fun *(i: Float): Float is intern
197 fun /(i: Float): Float is intern
198
199 # The integer part of `self'.
200 fun to_i: Int is intern
201 end
202
203 # Native integer numbers.
204 # Correspond to C int.
205 universal Int
206 super Discrete
207 redef type OTHER: Int
208
209 redef fun object_id is intern
210 redef fun ==(i) is intern
211 redef fun !=(i) is intern
212 redef fun output is intern
213
214 redef fun <=(i) is intern
215 redef fun <(i) is intern
216 redef fun >=(i) is intern
217 redef fun >(i) is intern
218 redef fun +(i) is intern
219 fun -: Int is intern
220 redef fun -(i) is intern
221 fun *(i: Int): Int is intern
222 fun /(i: Int): Int is intern
223 fun %(i: Int): Int is intern
224 fun lshift(i: Int): Int is intern
225 fun rshift(i: Int): Int is intern
226
227 # The float equivalent of `self'
228 fun to_f: Float is intern
229
230 redef fun succ is intern
231 redef fun prec is intern
232 redef fun distance(i)
233 do
234 var d = self - i
235 if d >= 0 then
236 return d
237 else
238 return -d
239 end
240 end
241
242 redef fun <=>(other)
243 do
244 if self < other then
245 return -1
246 else if other < self then
247 return 1
248 else
249 return 0
250 end
251 end
252
253 redef fun is_between(c, d)
254 do
255 if self < c or d < self then
256 return false
257 else
258 return true
259 end
260 end
261
262 redef fun max(other)
263 do
264 if self < other then
265 return other
266 else
267 return self
268 end
269 end
270
271 redef fun min(c)
272 do
273 if c < self then
274 return c
275 else
276 return self
277 end
278 end
279
280 # The character whose ASCII value is `self'.
281 fun ascii: Char is intern
282
283 # Number of digits of an integer in base `b' plus one if negative)
284 fun digit_count(b: Int): Int
285 do
286 var d: Int # number of digits
287 var n: Int # current number
288 # Sign
289 if self < 0 then
290 d = 1
291 n = - self
292 else if self == 0 then
293 return 1
294 else
295 d = 0
296 n = self
297 end
298 # count digits
299 while n > 0 do
300 d += 1
301 n = n / b # euclidian division /
302 end
303 return d
304 end
305
306 # Return the corresponding digit character
307 # If 0 <= `self' <= 9, return the corresponding character.
308 # If 10 <= `self' <= 36, return the corresponding letter [a..z].
309 fun to_c: Char
310 do
311 assert self >= 0 and self <= 36 # TODO plan for this
312 if self < 10 then
313 return (self + '0'.ascii).ascii
314 else
315 return (self + ('a'.ascii - 10)).ascii
316 end
317 end
318
319 # Executre 'each' for each integer in [self..last]
320 fun enumerate_to(last: Int)
321 !each(i: Int)
322 do
323 var cur = self
324 while cur <= last do
325 each(cur)
326 cur += 1
327 end
328 end
329
330 # Executre 'each' for each integer in [self..after[
331 fun enumerate_before(after: Int)
332 !each(i: Int)
333 do
334 var cur = self
335 while cur < after do
336 each(cur)
337 cur += 1
338 end
339 end
340 end
341
342 # Native characters.
343 # Characters are denoted with simple quote.
344 # eg. 'a' or '\n'.
345 universal Char
346 super Discrete
347 redef type OTHER: Char
348
349 redef fun object_id is intern
350 redef fun ==(o) is intern
351 redef fun !=(o) is intern
352 redef fun output is intern
353
354 redef fun <=(i) is intern
355 redef fun <(i) is intern
356 redef fun >=(i) is intern
357 redef fun >(i) is intern
358
359 redef fun succ is intern
360 redef fun prec is intern
361
362 redef fun distance(c)
363 do
364 var d = self.ascii - c.ascii
365 if d >= 0 then
366 return d
367 else
368 return -d
369 end
370 end
371
372 # If `self' is a digit then return this digit.
373 fun to_i: Int
374 do
375
376 if self == '-' then
377 return -1
378 else if is_digit then
379 return self.ascii - '0'.ascii
380 else
381 return self.to_lower.ascii - ('a'.ascii + 10)
382 end
383 end
384
385 # the ascii value of self
386 fun ascii: Int is intern
387
388 redef fun +(i) is intern
389 redef fun -(i) is intern
390
391 # Char to lower case
392 fun to_lower : Char
393 do
394 if is_upper then
395 return (ascii + ('a'.distance('A'))).ascii
396 else
397 return self
398 end
399 end
400
401 # Char to upper case
402 fun to_upper : Char
403 do
404 if is_lower then
405 return (ascii - ('a'.distance('A'))).ascii
406 else
407 return self
408 end
409 end
410
411 fun is_digit : Bool
412 do
413 return self >= '0' and self <= '9'
414 end
415
416 fun is_lower : Bool
417 do
418 return self >= 'a' and self <= 'z'
419 end
420
421 fun is_upper : Bool
422 do
423 return self >= 'A' and self <= 'Z'
424 end
425
426 fun is_letter : Bool
427 do
428 return is_lower or is_upper
429 end
430 end
431
432 # Pointer classes are used to manipulate extern C structures.
433 universal Pointer
434 end