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 # Return true if `self` and `other` are the same instance.
39 # Unless specific code, you should use `==` instead.
40 fun is_same_instance
(other
: nullable Object): Bool is intern
42 # Have `self` and `other` the same value?
44 # The exact meaning of "same value" is let to the subclasses.
45 # Implicitly, the default implementation, is `is_same_instance`
46 fun ==(other
: nullable Object): Bool do return self.is_same_instance
(other
)
48 # Have `self` and `other` different values?
50 # != is equivalent with "not ==".
51 fun !=(other
: nullable Object): Bool do return not (self == other
)
53 # Display self on stdout (debug only).
54 # This method MUST not be used by programs, it is here for debugging
55 # only and can be removed without any notice
63 # Display class name on stdout (debug only).
64 # This method MUST not be used by programs, it is here for debugging
65 # only and can be removed without any notice
66 fun output_class_name
is intern
68 # Quit the program with a specific return code
69 protected fun exit
(exit_value
: Int) is intern
71 # Return the global sys object, the only instance of the `Sys` class.
72 protected fun sys
: Sys is intern
74 # The hash code of the object.
75 # Assuming that a == b -> a.hash == b.hash
77 # Without redefinition, it is based on the `object_id` of the instance.
78 fun hash
: Int do return object_id
/ 8
81 # The main class of the program.
82 # `Sys` is a singleton class, its only instance is `sys` defined in `Object`.
83 # `sys` is used to invoke methods on the program on the system.
85 # Instructions outside classes implicitly redefine this method.
89 ###############################################################################
91 ###############################################################################
93 # The ancestor of class where objects are in a total order.
94 # In order to work, the method '<' has to be redefined.
96 # What `self` can be compared to?
97 type OTHER: Comparable
99 # Is `self` lesser than `other`?
100 fun <(other
: OTHER): Bool is abstract
102 # not `other` < `self`
103 # Note, the implementation must ensure that: `(x<=y) == (x<y or x==y)`
104 fun <=(other
: OTHER): Bool do return not other
< self
106 # not `self` < `other`
107 # Note, the implementation must ensure that: `(x>=y) == (x>y or x==y)`
108 fun >=(other
: OTHER): Bool do return not self < other
111 fun >(other
: OTHER): Bool do return other
< self
113 # -1 if <, +1 if > and 0 otherwise
114 # Note, the implementation must ensure that: (x<=>y == 0) == (x==y)
115 fun <=>(other
: OTHER): Int
119 else if other
< self then
127 fun is_between
(c
: OTHER, d
: OTHER): Bool
129 return c
<= self and self <= d
132 # The maximum between `self` and `other` (prefers `self` if equals).
133 fun max
(other
: OTHER): OTHER
142 # The minimum between `self` and `c` (prefer `self` if equals)
143 fun min
(c
: OTHER): OTHER
153 # Discrete total orders.
157 redef type OTHER: Discrete
160 fun succ
: OTHER do return self + 1
162 # The previous element.
163 fun prec
: OTHER do return self - 1
165 # The `i`-th successor element.
166 fun +(i
: Int): OTHER is abstract
168 # The `i`-th previous element.
169 fun -(i
: Int): OTHER is abstract
171 # The distance between self and d.
173 # assert 10.distance(15) == 5
174 # assert 'Z'.distance('A') == 25
175 fun distance
(d
: OTHER): Int
182 else if self > d
then
190 while cursor
< stop
do
198 ###############################################################################
200 ###############################################################################
203 # `true` and `false` are the only instances.
204 # Boolean are manipulated trough three special operators:
205 # `and`, `or`, `not`.
206 # Booleans are mainly used by conditional statement and loops.
208 redef fun object_id
is intern
209 redef fun ==(b
) is intern
210 redef fun !=(b
) is intern
211 redef fun output
is intern
222 # Native floating point numbers.
223 # Corresponds to C float.
225 redef fun object_id
is intern
226 redef fun output
is intern
228 fun <=(i
: Float): Bool is intern
229 fun <(i
: Float): Bool is intern
230 fun >=(i
: Float): Bool is intern
231 fun >(i
: Float): Bool is intern
232 fun +(i
: Float): Float is intern
233 fun -: Float is intern
234 fun -(i
: Float): Float is intern
235 fun *(i
: Float): Float is intern
236 fun /(i
: Float): Float is intern
238 # The integer part of `self`.
240 # assert (0.0).to_i == 0
241 # assert (0.9).to_i == 0
242 # assert (-0.9).to_i == 0
243 # assert (9.9).to_i == 9
244 # assert (-9.9).to_i == -9
245 fun to_i
: Int is intern
248 # Native integer numbers.
249 # Correspond to C int.
252 redef type OTHER: Int
254 redef fun object_id
is intern
255 redef fun hash
do return self
256 redef fun ==(i
) is intern
257 redef fun !=(i
) is intern
258 redef fun output
is intern
260 redef fun <=(i
) is intern
261 redef fun <(i
) is intern
262 redef fun >=(i
) is intern
263 redef fun >(i
) is intern
264 redef fun +(i
) is intern
266 redef fun -(i
) is intern
267 fun *(i
: Int): Int is intern
268 fun /(i
: Int): Int is intern
269 fun %(i
: Int): Int is intern
271 # `i` bits shift fo the left (aka <<)
273 # assert 5.lshift(1) == 10
274 fun lshift
(i
: Int): Int is intern
276 # `i` bits shift fo the right (aka >>)
278 # assert 5.rshift(1) == 2
279 fun rshift
(i
: Int): Int is intern
281 # The float equivalent of `self`
283 # assert 5.to_f == 5.0
284 # assert 5.to_f != 5 # Float and Int are not equals
285 fun to_f
: Float is intern
287 redef fun succ
is intern
288 redef fun prec
is intern
289 redef fun distance
(i
)
303 else if other
< self then
310 redef fun is_between
(c
, d
)
312 if self < c
or d
< self then
337 # The character whose ASCII value is `self`.
339 # assert 65.ascii == 'A'
340 # assert 10.ascii == '\n'
341 fun ascii
: Char is intern
343 # Number of digits of an integer in base `b` (plus one if negative)
345 # assert 123.digit_count(10) == 3
346 # assert 123.digit_count(2) == 7 # 1111011 in binary
347 fun digit_count
(b
: Int): Int
349 if b
== 10 then return digit_count_base_10
350 var d
: Int # number of digits
351 var n
: Int # current number
356 else if self == 0 then
365 n
= n
/ b
# euclidian division /
370 # Optimized version for base 10
371 fun digit_count_base_10
: Int
383 if val
< 10 then return result
384 if val
< 100 then return result
+1
385 if val
< 1000 then return result
+2
386 if val
< 10000 then return result
+3
392 # Return the corresponding digit character
393 # If 0 <= `self` <= 9, return the corresponding character.
394 # assert 5.to_c == '5'
395 # If 10 <= `self` <= 36, return the corresponding letter [a..z].
396 # assert 15.to_c == 'f'
399 assert self >= 0 and self <= 36 # TODO plan for this
401 return (self + '0'.ascii
).ascii
403 return (self + ('a'.ascii
- 10)).ascii
407 # The absolute value of self
409 # assert (-10).abs == 10
410 # assert 10.abs == 10
424 # Characters are denoted with simple quote.
425 # eg. `'a'` or `'\n'`.
428 redef type OTHER: Char
430 redef fun object_id
is intern
431 redef fun hash
do return ascii
432 redef fun ==(o
) is intern
433 redef fun !=(o
) is intern
434 redef fun output
is intern
436 redef fun <=(i
) is intern
437 redef fun <(i
) is intern
438 redef fun >=(i
) is intern
439 redef fun >(i
) is intern
441 redef fun succ
is intern
442 redef fun prec
is intern
444 redef fun distance
(c
)
446 var d
= self.ascii
- c
.ascii
454 # If `self` is a digit then return this digit else return -1.
456 # assert '5'.to_i == 5
462 else if is_digit
then
463 return self.ascii
- '0'.ascii
465 return self.to_lower
.ascii
- 'a'.ascii
+ 10
469 # the ascii value of self
471 # assert 'a'.ascii == 97
472 # assert '\n'.ascii == 10
473 fun ascii
: Int is intern
475 redef fun +(i
) is intern
476 redef fun -(i
) is intern
478 # Return the lower case version of self.
479 # If self is not a letter, then return self
481 # assert 'A'.to_lower == 'a'
482 # assert 'a'.to_lower == 'a'
483 # assert '$'.to_lower == '$'
487 return (ascii
+ ('a'.distance
('A'))).ascii
493 # Return the upper case version of self.
494 # If self is not a letter, then return self
496 # assert 'a'.to_upper == 'A'
497 # assert 'A'.to_upper == 'A'
498 # assert '$'.to_upper == '$'
502 return (ascii
- ('a'.distance
('A'))).ascii
508 # Is self a digit? (from '0' to '9')
510 # assert '0'.is_digit == true
511 # assert '9'.is_digit == true
512 # assert 'a'.is_digit == false
515 return self >= '0' and self <= '9'
518 # Is self a lower case letter? (from 'a' to 'z')
520 # assert 'a'.is_lower == true
521 # assert 'z'.is_lower == true
522 # assert 'A'.is_lower == false
523 # assert '$'.is_lower == false
526 return self >= 'a' and self <= 'z'
529 # Is self a upper case letter? (from 'A' to 'Z')
531 # assert 'A'.is_upper == true
532 # assert 'A'.is_upper == true
533 # assert 'z'.is_upper == false
534 # assert '$'.is_upper == false
537 return self >= 'A' and self <= 'Z'
540 # Is self a letter? (from 'A' to 'Z' and 'a' to 'z')
542 # assert 'A'.is_letter == true
543 # assert 'A'.is_letter == true
544 # assert 'z'.is_letter == true
545 # assert '$'.is_letter == false
548 return is_lower
or is_upper
552 # Pointer classes are used to manipulate extern C structures.
554 # Is the address behind this Object at NULL?
555 fun address_is_null
: Bool `{ return recv == NULL; `}
557 # Free the memory pointed by this pointer
558 fun free `{ free(recv); `}