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 # Quit the program with a specific return code
73 protected fun exit
(exit_value
: Int) is intern
75 # Return the global sys object, the only instance of the `Sys` class.
76 protected fun sys
: Sys is intern
78 # The hash code of the object.
79 # Assuming that a == b -> a.hash == b.hash
81 # Without redefinition, it is based on the `object_id` of the instance.
82 fun hash
: Int do return object_id
/ 8
85 # The main class of the program.
86 # `Sys` is a singleton class, its only instance is `sys` defined in `Object`.
87 # `sys` is used to invoke methods on the program on the system.
89 # Instructions outside classes implicitly redefine this method.
92 # Number of the last error
93 fun errno
: Int is extern `{
98 ###############################################################################
100 ###############################################################################
102 # The ancestor of class where objects are in a total order.
103 # In order to work, the method '<' has to be redefined.
105 # What `self` can be compared to?
106 type OTHER: Comparable
108 # Is `self` lesser than `other`?
109 fun <(other
: OTHER): Bool is abstract
111 # not `other` < `self`
112 # Note, the implementation must ensure that: `(x<=y) == (x<y or x==y)`
113 fun <=(other
: OTHER): Bool do return not other
< self
115 # not `self` < `other`
116 # Note, the implementation must ensure that: `(x>=y) == (x>y or x==y)`
117 fun >=(other
: OTHER): Bool do return not self < other
120 fun >(other
: OTHER): Bool do return other
< self
122 # -1 if <, +1 if > and 0 otherwise
123 # Note, the implementation must ensure that: (x<=>y == 0) == (x==y)
124 fun <=>(other
: OTHER): Int
128 else if other
< self then
136 fun is_between
(c
: OTHER, d
: OTHER): Bool
138 return c
<= self and self <= d
141 # The maximum between `self` and `other` (prefers `self` if equals).
142 fun max
(other
: OTHER): OTHER
151 # The minimum between `self` and `c` (prefer `self` if equals)
152 fun min
(c
: OTHER): OTHER
162 # Discrete total orders.
166 redef type OTHER: Discrete
169 fun successor
(i
: Int): OTHER is abstract
171 # The previous element.
172 fun predecessor
(i
: Int): OTHER is abstract
174 # The distance between self and d.
176 # assert 10.distance(15) == 5
177 # assert 'Z'.distance('A') == 25
178 fun distance
(d
: OTHER): Int
185 else if self > d
then
193 while cursor
< stop
do
194 cursor
= cursor
.successor
(1)
201 # A numeric value supporting mathematical operations
205 redef type OTHER: Numeric
207 # Addition of `self` with `i`
208 fun +(i
: OTHER): OTHER is abstract
210 # Substraction of `i` from `self`
211 fun -(i
: OTHER): OTHER is abstract
214 fun -: OTHER is abstract
216 # Multiplication of `self` with `i`
217 fun *(i
: OTHER): OTHER is abstract
219 # Division of `self` with `i`
220 fun /(i
: OTHER): OTHER is abstract
222 # The integer part of `self`.
224 # assert (0.0).to_i == 0
225 # assert (0.9).to_i == 0
226 # assert (-0.9).to_i == 0
227 # assert (9.9).to_i == 9
228 # assert (-9.9).to_i == -9
229 fun to_i
: Int is abstract
231 # The float equivalent of `self`
233 # assert 5.to_f == 5.0
234 # assert 5.to_f != 5 # Float and Int are not equals
235 fun to_f
: Float is abstract
237 # Is this the value of zero in its domain?
238 fun is_zero
: Bool do return self == zero
240 # The value of zero in the domain of `self`
241 fun zero
: OTHER is abstract
243 # The value of `val` in the domain of `self`
245 # assert 1.0.value_of(2) == 2.0
246 # assert 1.0.value_of(2.0) == 2.0
247 # assert 1.value_of(2) == 2
248 # assert 1.value_of(2.0) == 2
249 fun value_of
(val
: Numeric): OTHER is abstract
252 ###############################################################################
254 ###############################################################################
257 # `true` and `false` are the only instances.
258 # Boolean are manipulated trough three special operators:
259 # `and`, `or`, `not`.
260 # Booleans are mainly used by conditional statement and loops.
262 redef fun object_id
is intern
263 redef fun ==(b
) is intern
264 redef fun !=(b
) is intern
265 redef fun output
is intern
266 redef fun hash
do return to_i
268 # 1 if true and 0 if false
279 # Native floating point numbers.
280 # Corresponds to C float.
284 redef type OTHER: Float
286 redef fun object_id
is intern
287 redef fun output
is intern
289 redef fun <=(i
): Bool is intern
290 redef fun <(i
): Bool is intern
291 redef fun >=(i
): Bool is intern
292 redef fun >(i
): Bool is intern
294 redef fun +(i
) is intern
295 redef fun - is intern
296 redef fun -(i
) is intern
297 redef fun *(i
) is intern
298 redef fun /(i
) is intern
300 redef fun to_i
is intern
301 redef fun to_f
do return self
303 redef fun zero
do return 0.0
304 redef fun value_of
(val
) do return val
.to_f
307 # Native integer numbers.
308 # Correspond to C int.
313 redef type OTHER: Int
315 redef fun successor
(i
) do return self + i
316 redef fun predecessor
(i
) do return self - i
318 redef fun object_id
is intern
319 redef fun hash
do return self
320 redef fun ==(i
) is intern
321 redef fun !=(i
) is intern
322 redef fun output
is intern
324 redef fun <=(i
) is intern
325 redef fun <(i
) is intern
326 redef fun >=(i
) is intern
327 redef fun >(i
) is intern
328 redef fun +(i
) is intern
330 redef fun - is intern
331 redef fun -(i
) is intern
332 redef fun *(i
) is intern
333 redef fun /(i
) is intern
334 fun %(i
: Int): Int is intern
336 redef fun zero
do return 0
337 redef fun value_of
(val
) do return val
.to_i
339 # `i` bits shift fo the left (aka <<)
341 # assert 5.lshift(1) == 10
342 fun lshift
(i
: Int): Int is intern
344 # `i` bits shift fo the right (aka >>)
346 # assert 5.rshift(1) == 2
347 fun rshift
(i
: Int): Int is intern
349 redef fun to_i
do return self
350 redef fun to_f
is intern
352 redef fun distance
(i
)
366 else if other
< self then
373 redef fun is_between
(c
, d
)
375 if self < c
or d
< self then
400 # The character whose ASCII value is `self`.
402 # assert 65.ascii == 'A'
403 # assert 10.ascii == '\n'
404 fun ascii
: Char is intern
406 # Number of digits of an integer in base `b` (plus one if negative)
408 # assert 123.digit_count(10) == 3
409 # assert 123.digit_count(2) == 7 # 1111011 in binary
410 fun digit_count
(b
: Int): Int
412 if b
== 10 then return digit_count_base_10
413 var d
: Int # number of digits
414 var n
: Int # current number
419 else if self == 0 then
428 n
= n
/ b
# euclidian division /
433 # Optimized version for base 10
434 fun digit_count_base_10
: Int
446 if val
< 10 then return result
447 if val
< 100 then return result
+1
448 if val
< 1000 then return result
+2
449 if val
< 10000 then return result
+3
455 # Return the corresponding digit character
456 # If 0 <= `self` <= 9, return the corresponding character.
457 # assert 5.to_c == '5'
458 # If 10 <= `self` <= 36, return the corresponding letter [a..z].
459 # assert 15.to_c == 'f'
462 assert self >= 0 and self <= 36 # TODO plan for this
464 return (self + '0'.ascii
).ascii
466 return (self + ('a'.ascii
- 10)).ascii
470 # The absolute value of self
472 # assert (-10).abs == 10
473 # assert 10.abs == 10
487 # Characters are denoted with simple quote.
488 # eg. `'a'` or `'\n'`.
491 redef type OTHER: Char
493 redef fun object_id
is intern
494 redef fun hash
do return ascii
495 redef fun ==(o
) is intern
496 redef fun !=(o
) is intern
497 redef fun output
is intern
499 redef fun <=(i
) is intern
500 redef fun <(i
) is intern
501 redef fun >=(i
) is intern
502 redef fun >(i
) is intern
504 redef fun successor
(i
) is intern
505 redef fun predecessor
(i
) is intern
507 redef fun distance
(c
)
509 var d
= self.ascii
- c
.ascii
517 # If `self` is a digit then return this digit else return -1.
519 # assert '5'.to_i == 5
525 else if is_digit
then
526 return self.ascii
- '0'.ascii
528 return self.to_lower
.ascii
- 'a'.ascii
+ 10
532 # the ascii value of self
534 # assert 'a'.ascii == 97
535 # assert '\n'.ascii == 10
536 fun ascii
: Int is intern
538 # Return the lower case version of self.
539 # If self is not a letter, then return self
541 # assert 'A'.to_lower == 'a'
542 # assert 'a'.to_lower == 'a'
543 # assert '$'.to_lower == '$'
547 return (ascii
+ ('a'.distance
('A'))).ascii
553 # Return the upper case version of self.
554 # If self is not a letter, then return self
556 # assert 'a'.to_upper == 'A'
557 # assert 'A'.to_upper == 'A'
558 # assert '$'.to_upper == '$'
562 return (ascii
- ('a'.distance
('A'))).ascii
568 # Is self a digit? (from '0' to '9')
570 # assert '0'.is_digit == true
571 # assert '9'.is_digit == true
572 # assert 'a'.is_digit == false
575 return self >= '0' and self <= '9'
578 # Is self a lower case letter? (from 'a' to 'z')
580 # assert 'a'.is_lower == true
581 # assert 'z'.is_lower == true
582 # assert 'A'.is_lower == false
583 # assert '$'.is_lower == false
586 return self >= 'a' and self <= 'z'
589 # Is self a upper case letter? (from 'A' to 'Z')
591 # assert 'A'.is_upper == true
592 # assert 'A'.is_upper == true
593 # assert 'z'.is_upper == false
594 # assert '$'.is_upper == false
597 return self >= 'A' and self <= 'Z'
600 # Is self a letter? (from 'A' to 'Z' and 'a' to 'z')
602 # assert 'A'.is_letter == true
603 # assert 'A'.is_letter == true
604 # assert 'z'.is_letter == true
605 # assert '$'.is_letter == false
608 return is_lower
or is_upper
612 # Pointer classes are used to manipulate extern C structures.
614 # Is the address behind this Object at NULL?
615 fun address_is_null
: Bool is extern "address_is_null"
617 # Free the memory pointed by this pointer
618 fun free
`{ free(recv); `}