1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
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
14 # Most minimal classes and methods.
15 # This module is the root of the standard module hierarchy.
18 import end # Mark this module is a top level one. (must be only one)
20 ###############################################################################
22 ###############################################################################
24 # The root of the class hierarchy.
25 # Each class implicitly specialize Object.
27 # Currently, Object is also used to collect all top-level methods.
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
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
38 # Have `self` and `other` the same value?
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
44 # Have `self` and `other` different values?
46 # != is equivalent with "not ==".
47 fun !=(other
: nullable Object): Bool do return not (self == other
)
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
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
64 # Quit the program with a specific return code
65 protected fun exit
(exit_value
: Int) is intern
67 # Return the global sys object, the only instance of the `Sys` class.
68 protected fun sys
: Sys is intern
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.
75 # Instructions outside classes implicitly redefine this method.
79 ###############################################################################
81 ###############################################################################
83 # The ancestor of class where objects are in a total order.
84 # In order to work, the method '<' has to be redefined.
86 # What `self` can be compared to?
87 type OTHER: Comparable
89 # Is `self` lesser than `other`?
90 fun <(other
: OTHER): Bool is abstract
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
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
101 fun >(other
: OTHER): Bool do return other
< self
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
109 else if other
< self then
117 fun is_between
(c
: OTHER, d
: OTHER): Bool
119 return c
<= self and self <= d
122 # The maximum between `self` and `other` (prefers `self` if equals).
123 fun max
(other
: OTHER): OTHER
132 # The minimum between `self` and `c` (prefer `self` if equals)
133 fun min
(c
: OTHER): OTHER
143 # Discrete total orders.
147 redef type OTHER: Discrete
150 fun succ
: OTHER do return self + 1
152 # The previous element.
153 fun prec
: OTHER do return self - 1
155 # The `i`-th successor element.
156 fun +(i
: Int): OTHER is abstract
158 # The `i`-th previous element.
159 fun -(i
: Int): OTHER is abstract
161 # The distance between self and d.
163 # assert 10.distance(15) == 5
164 # assert 'Z'.distance('A') == 25
165 fun distance
(d
: OTHER): Int
172 else if self > d
then
180 while cursor
< stop
do
188 ###############################################################################
190 ###############################################################################
193 # `true` and `false` are the only instances.
194 # Boolean are manipulated trough three special operators:
195 # `and`, `or`, `not`.
196 # Booleans are mainly used by conditional statement and loops.
198 redef fun object_id
is intern
199 redef fun ==(b
) is intern
200 redef fun !=(b
) is intern
201 redef fun output
is intern
204 # Native floating point numbers.
205 # Corresponds to C float.
207 redef fun object_id
is intern
208 redef fun output
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): Bool is intern
214 fun +(i
: Float): Float is intern
215 fun -: Float is intern
216 fun -(i
: Float): Float is intern
217 fun *(i
: Float): Float is intern
218 fun /(i
: Float): Float is intern
220 # The integer part of `self`.
222 # assert (0.0).to_i == 0
223 # assert (0.9).to_i == 0
224 # assert (-0.9).to_i == 0
225 # assert (9.9).to_i == 9
226 # assert (-9.9).to_i == -9
227 fun to_i
: Int is intern
230 # Native integer numbers.
231 # Correspond to C int.
234 redef type OTHER: Int
236 redef fun object_id
is intern
237 redef fun ==(i
) is intern
238 redef fun !=(i
) is intern
239 redef fun output
is intern
241 redef fun <=(i
) is intern
242 redef fun <(i
) is intern
243 redef fun >=(i
) is intern
244 redef fun >(i
) is intern
245 redef fun +(i
) is intern
247 redef fun -(i
) is intern
248 fun *(i
: Int): Int is intern
249 fun /(i
: Int): Int is intern
250 fun %(i
: Int): Int is intern
252 # `i` bits shift fo the left (aka <<)
254 # assert 5.lshift(1) == 10
255 fun lshift
(i
: Int): Int is intern
257 # `i` bits shift fo the right (aka >>)
259 # assert 5.rshift(1) == 2
260 fun rshift
(i
: Int): Int is intern
262 # The float equivalent of `self`
264 # assert 5.to_f == 5.0
265 # assert 5.to_f != 5 # Float and Int are not equals
266 fun to_f
: Float is intern
268 redef fun succ
is intern
269 redef fun prec
is intern
270 redef fun distance
(i
)
284 else if other
< self then
291 redef fun is_between
(c
, d
)
293 if self < c
or d
< self then
318 # The character whose ASCII value is `self`.
320 # assert 65.ascii == 'A'
321 # assert 10.ascii == '\n'
322 fun ascii
: Char is intern
324 # Number of digits of an integer in base `b` (plus one if negative)
326 # assert 123.digit_count(10) == 3
327 # assert 123.digit_count(2) == 7 # 1111011 in binary
328 fun digit_count
(b
: Int): Int
330 if b
== 10 then return digit_count_base_10
331 var d
: Int # number of digits
332 var n
: Int # current number
337 else if self == 0 then
346 n
= n
/ b
# euclidian division /
351 # Optimized version for base 10
352 fun digit_count_base_10
: Int
364 if val
< 10 then return result
365 if val
< 100 then return result
+1
366 if val
< 1000 then return result
+2
367 if val
< 10000 then return result
+3
373 # Return the corresponding digit character
374 # If 0 <= `self` <= 9, return the corresponding character.
375 # assert 5.to_c == '5'
376 # If 10 <= `self` <= 36, return the corresponding letter [a..z].
377 # assert 15.to_c == 'f'
380 assert self >= 0 and self <= 36 # TODO plan for this
382 return (self + '0'.ascii
).ascii
384 return (self + ('a'.ascii
- 10)).ascii
388 # Execute 'each' for each integer in [self..last]
389 fun enumerate_to
(last
: Int)
399 # Execute 'each' for each integer in [self..after[
400 fun enumerate_before
(after
: Int)
410 # The absolute value of self
412 # assert (-10).abs == 10
413 # assert 10.abs == 10
427 # Characters are denoted with simple quote.
428 # eg. `'a'` or `'\n'`.
431 redef type OTHER: Char
433 redef fun object_id
is intern
434 redef fun ==(o
) is intern
435 redef fun !=(o
) is intern
436 redef fun output
is intern
438 redef fun <=(i
) is intern
439 redef fun <(i
) is intern
440 redef fun >=(i
) is intern
441 redef fun >(i
) is intern
443 redef fun succ
is intern
444 redef fun prec
is intern
446 redef fun distance
(c
)
448 var d
= self.ascii
- c
.ascii
456 # If `self` is a digit then return this digit else return -1.
458 # assert '5'.to_i == 5
464 else if is_digit
then
465 return self.ascii
- '0'.ascii
467 return self.to_lower
.ascii
- ('a'.ascii
+ 10)
471 # the ascii value of self
473 # assert 'a'.ascii == 97
474 # assert '\n'.ascii == 10
475 fun ascii
: Int is intern
477 redef fun +(i
) is intern
478 redef fun -(i
) is intern
480 # Return the lower case version of self.
481 # If self is not a letter, then return self
483 # assert 'A'.to_lower == 'a'
484 # assert 'a'.to_lower == 'a'
485 # assert '$'.to_lower == '$'
489 return (ascii
+ ('a'.distance
('A'))).ascii
495 # Return the upper case version of self.
496 # If self is not a letter, then return self
498 # assert 'a'.to_upper == 'A'
499 # assert 'A'.to_upper == 'A'
500 # assert '$'.to_upper == '$'
504 return (ascii
- ('a'.distance
('A'))).ascii
510 # Is self a digit? (from '0' to '9')
512 # assert '0'.is_digit == true
513 # assert '9'.is_digit == true
514 # assert 'a'.is_digit == false
517 return self >= '0' and self <= '9'
520 # Is self a lower case letter? (from 'a' to 'z')
522 # assert 'a'.is_lower == true
523 # assert 'z'.is_lower == true
524 # assert 'A'.is_lower == false
525 # assert '$'.is_lower == false
528 return self >= 'a' and self <= 'z'
531 # Is self a upper case letter? (from 'A' to 'Z')
533 # assert 'A'.is_upper == true
534 # assert 'A'.is_upper == true
535 # assert 'z'.is_upper == false
536 # assert '$'.is_upper == false
539 return self >= 'A' and self <= 'Z'
542 # Is self a letter? (from 'A' to 'Z' and 'a' to 'z')
544 # assert 'A'.is_letter == true
545 # assert 'A'.is_letter == true
546 # assert 'z'.is_letter == true
547 # assert '$'.is_letter == false
550 return is_lower
or is_upper
554 # Pointer classes are used to manipulate extern C structures.