3cf33ba12105f8c62cb23ed92447e08d8993ca04
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
70 # The hash code of the object.
71 # Assuming that a == b -> a.hash == b.hash
73 # Without redefinition, it is based on the `object_id` of the instance.
74 fun hash
: Int do return object_id
/ 8
77 # The main class of the program.
78 # `Sys` is a singleton class, its only instance is `sys` defined in `Object`.
79 # `sys` is used to invoke methods on the program on the system.
81 # Instructions outside classes implicitly redefine this method.
85 ###############################################################################
87 ###############################################################################
89 # The ancestor of class where objects are in a total order.
90 # In order to work, the method '<' has to be redefined.
92 # What `self` can be compared to?
93 type OTHER: Comparable
95 # Is `self` lesser than `other`?
96 fun <(other
: OTHER): Bool is abstract
98 # not `other` < `self`
99 # Note, the implementation must ensure that: `(x<=y) == (x<y or x==y)`
100 fun <=(other
: OTHER): Bool do return not other
< self
102 # not `self` < `other`
103 # Note, the implementation must ensure that: `(x>=y) == (x>y or x==y)`
104 fun >=(other
: OTHER): Bool do return not self < other
107 fun >(other
: OTHER): Bool do return other
< self
109 # -1 if <, +1 if > and 0 otherwise
110 # Note, the implementation must ensure that: (x<=>y == 0) == (x==y)
111 fun <=>(other
: OTHER): Int
115 else if other
< self then
123 fun is_between
(c
: OTHER, d
: OTHER): Bool
125 return c
<= self and self <= d
128 # The maximum between `self` and `other` (prefers `self` if equals).
129 fun max
(other
: OTHER): OTHER
138 # The minimum between `self` and `c` (prefer `self` if equals)
139 fun min
(c
: OTHER): OTHER
149 # Discrete total orders.
153 redef type OTHER: Discrete
156 fun succ
: OTHER do return self + 1
158 # The previous element.
159 fun prec
: OTHER do return self - 1
161 # The `i`-th successor element.
162 fun +(i
: Int): OTHER is abstract
164 # The `i`-th previous element.
165 fun -(i
: Int): OTHER is abstract
167 # The distance between self and d.
169 # assert 10.distance(15) == 5
170 # assert 'Z'.distance('A') == 25
171 fun distance
(d
: OTHER): Int
178 else if self > d
then
186 while cursor
< stop
do
194 ###############################################################################
196 ###############################################################################
199 # `true` and `false` are the only instances.
200 # Boolean are manipulated trough three special operators:
201 # `and`, `or`, `not`.
202 # Booleans are mainly used by conditional statement and loops.
204 redef fun object_id
is intern
205 redef fun ==(b
) is intern
206 redef fun !=(b
) is intern
207 redef fun output
is intern
218 # Native floating point numbers.
219 # Corresponds to C float.
221 redef fun object_id
is intern
222 redef fun output
is intern
224 fun <=(i
: Float): Bool is intern
225 fun <(i
: Float): Bool is intern
226 fun >=(i
: Float): Bool is intern
227 fun >(i
: Float): Bool is intern
228 fun +(i
: Float): Float is intern
229 fun -: Float is intern
230 fun -(i
: Float): Float is intern
231 fun *(i
: Float): Float is intern
232 fun /(i
: Float): Float is intern
234 # The integer part of `self`.
236 # assert (0.0).to_i == 0
237 # assert (0.9).to_i == 0
238 # assert (-0.9).to_i == 0
239 # assert (9.9).to_i == 9
240 # assert (-9.9).to_i == -9
241 fun to_i
: Int is intern
244 # Native integer numbers.
245 # Correspond to C int.
248 redef type OTHER: Int
250 redef fun object_id
is intern
251 redef fun hash
do return self
252 redef fun ==(i
) is intern
253 redef fun !=(i
) is intern
254 redef fun output
is intern
256 redef fun <=(i
) is intern
257 redef fun <(i
) is intern
258 redef fun >=(i
) is intern
259 redef fun >(i
) is intern
260 redef fun +(i
) is intern
262 redef fun -(i
) is intern
263 fun *(i
: Int): Int is intern
264 fun /(i
: Int): Int is intern
265 fun %(i
: Int): Int is intern
267 # `i` bits shift fo the left (aka <<)
269 # assert 5.lshift(1) == 10
270 fun lshift
(i
: Int): Int is intern
272 # `i` bits shift fo the right (aka >>)
274 # assert 5.rshift(1) == 2
275 fun rshift
(i
: Int): Int is intern
277 # The float equivalent of `self`
279 # assert 5.to_f == 5.0
280 # assert 5.to_f != 5 # Float and Int are not equals
281 fun to_f
: Float is intern
283 redef fun succ
is intern
284 redef fun prec
is intern
285 redef fun distance
(i
)
299 else if other
< self then
306 redef fun is_between
(c
, d
)
308 if self < c
or d
< self then
333 # The character whose ASCII value is `self`.
335 # assert 65.ascii == 'A'
336 # assert 10.ascii == '\n'
337 fun ascii
: Char is intern
339 # Number of digits of an integer in base `b` (plus one if negative)
341 # assert 123.digit_count(10) == 3
342 # assert 123.digit_count(2) == 7 # 1111011 in binary
343 fun digit_count
(b
: Int): Int
345 if b
== 10 then return digit_count_base_10
346 var d
: Int # number of digits
347 var n
: Int # current number
352 else if self == 0 then
361 n
= n
/ b
# euclidian division /
366 # Optimized version for base 10
367 fun digit_count_base_10
: Int
379 if val
< 10 then return result
380 if val
< 100 then return result
+1
381 if val
< 1000 then return result
+2
382 if val
< 10000 then return result
+3
388 # Return the corresponding digit character
389 # If 0 <= `self` <= 9, return the corresponding character.
390 # assert 5.to_c == '5'
391 # If 10 <= `self` <= 36, return the corresponding letter [a..z].
392 # assert 15.to_c == 'f'
395 assert self >= 0 and self <= 36 # TODO plan for this
397 return (self + '0'.ascii
).ascii
399 return (self + ('a'.ascii
- 10)).ascii
403 # Execute 'each' for each integer in [self..last]
404 fun enumerate_to
(last
: Int)
414 # Execute 'each' for each integer in [self..after[
415 fun enumerate_before
(after
: Int)
425 # The absolute value of self
427 # assert (-10).abs == 10
428 # assert 10.abs == 10
442 # Characters are denoted with simple quote.
443 # eg. `'a'` or `'\n'`.
446 redef type OTHER: Char
448 redef fun object_id
is intern
449 redef fun hash
do return ascii
450 redef fun ==(o
) is intern
451 redef fun !=(o
) is intern
452 redef fun output
is intern
454 redef fun <=(i
) is intern
455 redef fun <(i
) is intern
456 redef fun >=(i
) is intern
457 redef fun >(i
) is intern
459 redef fun succ
is intern
460 redef fun prec
is intern
462 redef fun distance
(c
)
464 var d
= self.ascii
- c
.ascii
472 # If `self` is a digit then return this digit else return -1.
474 # assert '5'.to_i == 5
480 else if is_digit
then
481 return self.ascii
- '0'.ascii
483 return self.to_lower
.ascii
- ('a'.ascii
+ 10)
487 # the ascii value of self
489 # assert 'a'.ascii == 97
490 # assert '\n'.ascii == 10
491 fun ascii
: Int is intern
493 redef fun +(i
) is intern
494 redef fun -(i
) is intern
496 # Return the lower case version of self.
497 # If self is not a letter, then return self
499 # assert 'A'.to_lower == 'a'
500 # assert 'a'.to_lower == 'a'
501 # assert '$'.to_lower == '$'
505 return (ascii
+ ('a'.distance
('A'))).ascii
511 # Return the upper case version of self.
512 # If self is not a letter, then return self
514 # assert 'a'.to_upper == 'A'
515 # assert 'A'.to_upper == 'A'
516 # assert '$'.to_upper == '$'
520 return (ascii
- ('a'.distance
('A'))).ascii
526 # Is self a digit? (from '0' to '9')
528 # assert '0'.is_digit == true
529 # assert '9'.is_digit == true
530 # assert 'a'.is_digit == false
533 return self >= '0' and self <= '9'
536 # Is self a lower case letter? (from 'a' to 'z')
538 # assert 'a'.is_lower == true
539 # assert 'z'.is_lower == true
540 # assert 'A'.is_lower == false
541 # assert '$'.is_lower == false
544 return self >= 'a' and self <= 'z'
547 # Is self a upper case letter? (from 'A' to 'Z')
549 # assert 'A'.is_upper == true
550 # assert 'A'.is_upper == true
551 # assert 'z'.is_upper == false
552 # assert '$'.is_upper == false
555 return self >= 'A' and self <= 'Z'
558 # Is self a letter? (from 'A' to 'Z' and 'a' to 'z')
560 # assert 'A'.is_letter == true
561 # assert 'A'.is_letter == true
562 # assert 'z'.is_letter == true
563 # assert '$'.is_letter == false
566 return is_lower
or is_upper
570 # Pointer classes are used to manipulate extern C structures.
572 # Is the address behind this Object at NULL?
573 fun address_is_null
: Bool `{ return recv == NULL; `}