19da7f8856acaf7d42eb1183a23fed0d5a356bbf
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
212 redef fun hash
do return to_i
214 # 1 if true and 0 if false
225 # Native floating point numbers.
226 # Corresponds to C float.
228 redef fun object_id
is intern
229 redef fun output
is intern
231 fun <=(i
: Float): Bool is intern
232 fun <(i
: Float): Bool is intern
233 fun >=(i
: Float): Bool is intern
234 fun >(i
: Float): Bool is intern
235 fun +(i
: Float): Float is intern
236 fun -: Float is intern
237 fun -(i
: Float): Float is intern
238 fun *(i
: Float): Float is intern
239 fun /(i
: Float): Float is intern
241 # The integer part of `self`.
243 # assert (0.0).to_i == 0
244 # assert (0.9).to_i == 0
245 # assert (-0.9).to_i == 0
246 # assert (9.9).to_i == 9
247 # assert (-9.9).to_i == -9
248 fun to_i
: Int is intern
251 # Native integer numbers.
252 # Correspond to C int.
255 redef type OTHER: Int
257 redef fun object_id
is intern
258 redef fun hash
do return self
259 redef fun ==(i
) is intern
260 redef fun !=(i
) is intern
261 redef fun output
is intern
263 redef fun <=(i
) is intern
264 redef fun <(i
) is intern
265 redef fun >=(i
) is intern
266 redef fun >(i
) is intern
267 redef fun +(i
) is intern
269 redef fun -(i
) is intern
270 fun *(i
: Int): Int is intern
271 fun /(i
: Int): Int is intern
272 fun %(i
: Int): Int is intern
274 # `i` bits shift fo the left (aka <<)
276 # assert 5.lshift(1) == 10
277 fun lshift
(i
: Int): Int is intern
279 # `i` bits shift fo the right (aka >>)
281 # assert 5.rshift(1) == 2
282 fun rshift
(i
: Int): Int is intern
284 # The float equivalent of `self`
286 # assert 5.to_f == 5.0
287 # assert 5.to_f != 5 # Float and Int are not equals
288 fun to_f
: Float is intern
290 redef fun succ
is intern
291 redef fun prec
is intern
292 redef fun distance
(i
)
306 else if other
< self then
313 redef fun is_between
(c
, d
)
315 if self < c
or d
< self then
340 # The character whose ASCII value is `self`.
342 # assert 65.ascii == 'A'
343 # assert 10.ascii == '\n'
344 fun ascii
: Char is intern
346 # Number of digits of an integer in base `b` (plus one if negative)
348 # assert 123.digit_count(10) == 3
349 # assert 123.digit_count(2) == 7 # 1111011 in binary
350 fun digit_count
(b
: Int): Int
352 if b
== 10 then return digit_count_base_10
353 var d
: Int # number of digits
354 var n
: Int # current number
359 else if self == 0 then
368 n
= n
/ b
# euclidian division /
373 # Optimized version for base 10
374 fun digit_count_base_10
: Int
386 if val
< 10 then return result
387 if val
< 100 then return result
+1
388 if val
< 1000 then return result
+2
389 if val
< 10000 then return result
+3
395 # Return the corresponding digit character
396 # If 0 <= `self` <= 9, return the corresponding character.
397 # assert 5.to_c == '5'
398 # If 10 <= `self` <= 36, return the corresponding letter [a..z].
399 # assert 15.to_c == 'f'
402 assert self >= 0 and self <= 36 # TODO plan for this
404 return (self + '0'.ascii
).ascii
406 return (self + ('a'.ascii
- 10)).ascii
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 hash
do return ascii
435 redef fun ==(o
) is intern
436 redef fun !=(o
) is intern
437 redef fun output
is intern
439 redef fun <=(i
) is intern
440 redef fun <(i
) is intern
441 redef fun >=(i
) is intern
442 redef fun >(i
) is intern
444 redef fun succ
is intern
445 redef fun prec
is intern
447 redef fun distance
(c
)
449 var d
= self.ascii
- c
.ascii
457 # If `self` is a digit then return this digit else return -1.
459 # assert '5'.to_i == 5
465 else if is_digit
then
466 return self.ascii
- '0'.ascii
468 return self.to_lower
.ascii
- 'a'.ascii
+ 10
472 # the ascii value of self
474 # assert 'a'.ascii == 97
475 # assert '\n'.ascii == 10
476 fun ascii
: Int is intern
478 redef fun +(i
) is intern
479 redef fun -(i
) is intern
481 # Return the lower case version of self.
482 # If self is not a letter, then return self
484 # assert 'A'.to_lower == 'a'
485 # assert 'a'.to_lower == 'a'
486 # assert '$'.to_lower == '$'
490 return (ascii
+ ('a'.distance
('A'))).ascii
496 # Return the upper case version of self.
497 # If self is not a letter, then return self
499 # assert 'a'.to_upper == 'A'
500 # assert 'A'.to_upper == 'A'
501 # assert '$'.to_upper == '$'
505 return (ascii
- ('a'.distance
('A'))).ascii
511 # Is self a digit? (from '0' to '9')
513 # assert '0'.is_digit == true
514 # assert '9'.is_digit == true
515 # assert 'a'.is_digit == false
518 return self >= '0' and self <= '9'
521 # Is self a lower case letter? (from 'a' to 'z')
523 # assert 'a'.is_lower == true
524 # assert 'z'.is_lower == true
525 # assert 'A'.is_lower == false
526 # assert '$'.is_lower == false
529 return self >= 'a' and self <= 'z'
532 # Is self a upper case letter? (from 'A' to 'Z')
534 # assert 'A'.is_upper == true
535 # assert 'A'.is_upper == true
536 # assert 'z'.is_upper == false
537 # assert '$'.is_upper == false
540 return self >= 'A' and self <= 'Z'
543 # Is self a letter? (from 'A' to 'Z' and 'a' to 'z')
545 # assert 'A'.is_letter == true
546 # assert 'A'.is_letter == true
547 # assert 'z'.is_letter == true
548 # assert '$'.is_letter == false
551 return is_lower
or is_upper
555 # Pointer classes are used to manipulate extern C structures.
557 # Is the address behind this Object at NULL?
558 fun address_is_null
: Bool `{ return recv == NULL; `}
560 # Free the memory pointed by this pointer
561 fun free `{ free(recv); `}