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)
24 ###############################################################################
26 ###############################################################################
28 # The root of the class hierarchy.
29 # Each class implicitly specialize Object.
31 # Currently, Object is also used to collect all top-level methods.
33 # The unique object identifier in the class.
34 # Unless specific code, you should not use this method.
35 # The identifier is used internally to provide a hash value.
36 fun object_id
: Int is intern
38 # Return true if `self` and `other` have the same dynamic type.
39 # Unless specific code, you should not use this method.
40 fun is_same_type
(other
: Object): Bool is intern
42 # Return true if `self` and `other` are the same instance.
43 # Unless specific code, you should use `==` instead.
44 fun is_same_instance
(other
: nullable Object): Bool is intern
46 # Have `self` and `other` the same value?
48 # The exact meaning of "same value" is let to the subclasses.
49 # Implicitly, the default implementation, is `is_same_instance`
50 fun ==(other
: nullable Object): Bool do return self.is_same_instance
(other
)
52 # Have `self` and `other` different values?
54 # != is equivalent with "not ==".
55 fun !=(other
: nullable Object): Bool do return not (self == other
)
57 # Display self on stdout (debug only).
58 # This method MUST not be used by programs, it is here for debugging
59 # only and can be removed without any notice
67 # Display class name on stdout (debug only).
68 # This method MUST not be used by programs, it is here for debugging
69 # only and can be removed without any notice
70 fun output_class_name
is intern
72 # The hash code of the object.
73 # Assuming that a == b -> a.hash == b.hash
75 # Without redefinition, it is based on the `object_id` of the instance.
76 fun hash
: Int do return object_id
/ 8
79 # The main class of the program.
80 # `Sys` is a singleton class, its only instance is `sys` defined in `Object`.
81 # `sys` is used to invoke methods on the program on the system.
83 # Instructions outside classes implicitly redefine this method.
86 # The entry point for the execution of the whole program.
87 # Its job is to call `main` but some modules may want to refine it
88 # and inject specific work before or after the main part.
91 # Number of the last error
92 fun errno
: Int is extern `{
97 # Quit the program with a specific return code
98 fun exit
(exit_value
: Int) is intern
100 # Return the global sys object, the only instance of the `Sys` class.
101 fun sys
: Sys is intern
104 ###############################################################################
106 ###############################################################################
108 # The ancestor of class where objects are in a total order.
109 # In order to work, the method '<' has to be redefined.
111 # What `self` can be compared to?
112 type OTHER: Comparable
114 # Is `self` lesser than `other`?
115 fun <(other
: OTHER): Bool is abstract
117 # not `other` < `self`
118 # Note, the implementation must ensure that: `(x<=y) == (x<y or x==y)`
119 fun <=(other
: OTHER): Bool do return not other
< self
121 # not `self` < `other`
122 # Note, the implementation must ensure that: `(x>=y) == (x>y or x==y)`
123 fun >=(other
: OTHER): Bool do return not self < other
126 fun >(other
: OTHER): Bool do return other
< self
128 # -1 if <, +1 if > and 0 otherwise
129 # Note, the implementation must ensure that: (x<=>y == 0) == (x==y)
130 fun <=>(other
: OTHER): Int
134 else if other
< self then
142 fun is_between
(c
: OTHER, d
: OTHER): Bool
144 return c
<= self and self <= d
147 # The maximum between `self` and `other` (prefers `self` if equals).
148 fun max
(other
: OTHER): OTHER
157 # The minimum between `self` and `c` (prefer `self` if equals)
158 fun min
(c
: OTHER): OTHER
168 # Discrete total orders.
172 redef type OTHER: Discrete
175 fun successor
(i
: Int): OTHER is abstract
177 # The previous element.
178 fun predecessor
(i
: Int): OTHER is abstract
180 # The distance between self and d.
182 # assert 10.distance(15) == 5
183 # assert 'Z'.distance('A') == 25
184 fun distance
(d
: OTHER): Int
191 else if self > d
then
199 while cursor
< stop
do
200 cursor
= cursor
.successor
(1)
207 # A numeric value supporting mathematical operations
211 redef type OTHER: Numeric
213 # Addition of `self` with `i`
214 fun +(i
: OTHER): OTHER is abstract
216 # Substraction of `i` from `self`
217 fun -(i
: OTHER): OTHER is abstract
220 fun -: OTHER is abstract
222 # Multiplication of `self` with `i`
223 fun *(i
: OTHER): OTHER is abstract
225 # Division of `self` with `i`
226 fun /(i
: OTHER): OTHER is abstract
228 # The integer part of `self`.
230 # assert (0.0).to_i == 0
231 # assert (0.9).to_i == 0
232 # assert (-0.9).to_i == 0
233 # assert (9.9).to_i == 9
234 # assert (-9.9).to_i == -9
235 fun to_i
: Int is abstract
237 # The float equivalent of `self`
239 # assert 5.to_f == 5.0
240 # assert 5.to_f != 5 # Float and Int are not equals
241 fun to_f
: Float is abstract
243 # Is this the value of zero in its domain?
244 fun is_zero
: Bool do return self == zero
246 # The value of zero in the domain of `self`
247 fun zero
: OTHER is abstract
249 # The value of `val` in the domain of `self`
251 # assert 1.0.value_of(2) == 2.0
252 # assert 1.0.value_of(2.0) == 2.0
253 # assert 1.value_of(2) == 2
254 # assert 1.value_of(2.0) == 2
255 fun value_of
(val
: Numeric): OTHER is abstract
258 ###############################################################################
260 ###############################################################################
263 # `true` and `false` are the only instances.
264 # Boolean are manipulated trough three special operators:
265 # `and`, `or`, `not`.
266 # Booleans are mainly used by conditional statement and loops.
268 redef fun object_id
is intern
269 redef fun ==(b
) is intern
270 redef fun !=(b
) is intern
271 redef fun output
is intern
272 redef fun hash
do return to_i
274 # 1 if true and 0 if false
285 # Native floating point numbers.
286 # Corresponds to C float.
290 redef type OTHER: Float
292 redef fun object_id
is intern
293 redef fun ==(i
) is intern
294 redef fun !=(i
) is intern
295 redef fun output
is intern
297 redef fun <=(i
): Bool is intern
298 redef fun <(i
): Bool is intern
299 redef fun >=(i
): Bool is intern
300 redef fun >(i
): Bool is intern
302 redef fun +(i
) is intern
303 redef fun - is intern
304 redef fun -(i
) is intern
305 redef fun *(i
) is intern
306 redef fun /(i
) is intern
308 redef fun to_i
is intern
309 redef fun to_f
do return self
311 redef fun zero
do return 0.0
312 redef fun value_of
(val
) do return val
.to_f
318 else if other
< self then
325 redef fun is_between
(c
, d
)
327 if self < c
or d
< self then
353 # Native integer numbers.
354 # Correspond to C int.
359 redef type OTHER: Int
361 redef fun successor
(i
) do return self + i
362 redef fun predecessor
(i
) do return self - i
364 redef fun object_id
is intern
365 redef fun hash
do return self
366 redef fun ==(i
) is intern
367 redef fun !=(i
) is intern
368 redef fun output
is intern
370 redef fun <=(i
) is intern
371 redef fun <(i
) is intern
372 redef fun >=(i
) is intern
373 redef fun >(i
) is intern
374 redef fun +(i
) is intern
376 redef fun - is intern
377 redef fun -(i
) is intern
378 redef fun *(i
) is intern
379 redef fun /(i
) is intern
381 # Modulo of `self` with `i`.
383 # Finds the remainder of division of `self` by `i`.
387 fun %(i
: Int): Int is intern
389 redef fun zero
do return 0
390 redef fun value_of
(val
) do return val
.to_i
392 # `i` bits shift fo the left (aka <<)
394 # assert 5.lshift(1) == 10
395 fun lshift
(i
: Int): Int is intern
397 # `i` bits shift fo the right (aka >>)
399 # assert 5.rshift(1) == 2
400 fun rshift
(i
: Int): Int is intern
402 redef fun to_i
do return self
403 redef fun to_f
is intern
405 redef fun distance
(i
)
419 else if other
< self then
426 redef fun is_between
(c
, d
)
428 if self < c
or d
< self then
453 # The character whose ASCII value is `self`.
455 # assert 65.ascii == 'A'
456 # assert 10.ascii == '\n'
457 fun ascii
: Char is intern
459 # Number of digits of an integer in base `b` (plus one if negative)
461 # assert 123.digit_count(10) == 3
462 # assert 123.digit_count(2) == 7 # 1111011 in binary
463 fun digit_count
(b
: Int): Int
465 if b
== 10 then return digit_count_base_10
466 var d
: Int # number of digits
467 var n
: Int # current number
472 else if self == 0 then
481 n
= n
/ b
# euclidian division /
486 # Optimized version for base 10
487 fun digit_count_base_10
: Int
499 if val
< 10 then return result
500 if val
< 100 then return result
+1
501 if val
< 1000 then return result
+2
502 if val
< 10000 then return result
+3
508 # Return the corresponding digit character
509 # If 0 <= `self` <= 9, return the corresponding character.
510 # assert 5.to_c == '5'
511 # If 10 <= `self` <= 36, return the corresponding letter [a..z].
512 # assert 15.to_c == 'f'
515 assert self >= 0 and self <= 36 # TODO plan for this
517 return (self + '0'.ascii
).ascii
519 return (self + ('a'.ascii
- 10)).ascii
523 # The absolute value of self
525 # assert (-10).abs == 10
526 # assert 10.abs == 10
540 # Characters are denoted with simple quote.
541 # eg. `'a'` or `'\n'`.
544 redef type OTHER: Char
546 redef fun object_id
is intern
547 redef fun hash
do return ascii
548 redef fun ==(o
) is intern
549 redef fun !=(o
) is intern
550 redef fun output
is intern
552 redef fun <=(i
) is intern
553 redef fun <(i
) is intern
554 redef fun >=(i
) is intern
555 redef fun >(i
) is intern
557 redef fun successor
(i
) is intern
558 redef fun predecessor
(i
) is intern
560 redef fun distance
(c
)
562 var d
= self.ascii
- c
.ascii
570 # If `self` is a digit then return this digit else return -1.
572 # assert '5'.to_i == 5
578 else if is_digit
then
579 return self.ascii
- '0'.ascii
581 return self.to_lower
.ascii
- 'a'.ascii
+ 10
585 # the ascii value of self
587 # assert 'a'.ascii == 97
588 # assert '\n'.ascii == 10
589 fun ascii
: Int is intern
591 # Return the lower case version of self.
592 # If self is not a letter, then return self
594 # assert 'A'.to_lower == 'a'
595 # assert 'a'.to_lower == 'a'
596 # assert '$'.to_lower == '$'
600 return (ascii
+ ('a'.distance
('A'))).ascii
606 # Return the upper case version of self.
607 # If self is not a letter, then return self
609 # assert 'a'.to_upper == 'A'
610 # assert 'A'.to_upper == 'A'
611 # assert '$'.to_upper == '$'
615 return (ascii
- ('a'.distance
('A'))).ascii
621 # Is self a digit? (from '0' to '9')
623 # assert '0'.is_digit == true
624 # assert '9'.is_digit == true
625 # assert 'a'.is_digit == false
628 return self >= '0' and self <= '9'
631 # Is self a lower case letter? (from 'a' to 'z')
633 # assert 'a'.is_lower == true
634 # assert 'z'.is_lower == true
635 # assert 'A'.is_lower == false
636 # assert '$'.is_lower == false
639 return self >= 'a' and self <= 'z'
642 # Is self a upper case letter? (from 'A' to 'Z')
644 # assert 'A'.is_upper == true
645 # assert 'A'.is_upper == true
646 # assert 'z'.is_upper == false
647 # assert '$'.is_upper == false
650 return self >= 'A' and self <= 'Z'
653 # Is self a letter? (from 'A' to 'Z' and 'a' to 'z')
655 # assert 'A'.is_letter == true
656 # assert 'A'.is_letter == true
657 # assert 'z'.is_letter == true
658 # assert '$'.is_letter == false
661 return is_lower
or is_upper
665 # Pointer classes are used to manipulate extern C structures.
667 # Is the address behind this Object at NULL?
668 fun address_is_null
: Bool is extern "address_is_null"
670 # Free the memory pointed by this pointer
671 fun free
`{ free(recv); `}