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
71 # The main class of the program.
72 # `Sys' is a singleton class, its only instance is `sys' defined in `Object'.
73 # `sys' is used to invoke methods on the program on the system.
75 # Instructions outside classes implicitly redefine this method.
79 ###############################################################################
81 ###############################################################################
83 # The ancestor of class where objects are in a total order.
84 # In order to work, the method '<' has to be redefined.
86 # What `self' can be compared to?
87 type OTHER: Comparable
89 # Is `self' lesser than `other'?
90 fun <(other
: OTHER): Bool is abstract
92 # not `other' < `self'
93 # Note, the implementation must ensure that: (x<=y) == (x<y or x==y)
94 fun <=(other
: OTHER): Bool do return not other
< self
96 # not `self' < `other'
97 # Note, the implementation must ensure that: (x>=y) == (x>y or x==y)
98 fun >=(other
: OTHER): Bool do return not self < other
101 fun >(other
: OTHER): Bool do return other
< self
103 # -1 if <, +1 if > and 0 otherwise
104 # Note, the implementation must ensure that: (x<=>y == 0) == (x==y)
105 fun <=>(other
: OTHER): Int
109 else if other
< self then
117 fun is_between
(c
: OTHER, d
: OTHER): Bool
119 return c
<= self and self <= d
122 # The maximum between `self' and `other' (prefers `self' if equals).
123 fun max
(other
: OTHER): OTHER
132 # The minimum between `self' and `c' (prefer `self' if equals)
133 fun min
(c
: OTHER): OTHER
143 # Discrete total orders.
147 redef type OTHER: Discrete
150 fun succ
: OTHER do return self + 1
152 # The previous element.
153 fun prec
: OTHER do return self - 1
155 # The `i'-th successor element.
156 fun +(i
: Int): OTHER is abstract
158 # The `i'-th previous element.
159 fun -(i
: Int): OTHER is abstract
161 # The distance between self and d.
162 # 10.distance(15) # --> 5
163 # 'Z'.distance('A') # --> 25
164 fun distance
(d
: OTHER): Int
171 else if self > d
then
179 while cursor
< stop
do
187 ###############################################################################
189 ###############################################################################
192 # `true' and `false' are the only instances.
193 # Boolean are manipulated trough three special operators:
194 # `and', `or', `not'.
195 # Booleans are mainly used by conditional statement and loops.
197 redef fun object_id
is intern
198 redef fun ==(b
) is intern
199 redef fun !=(b
) is intern
200 redef fun output
is intern
203 # Native floating point numbers.
204 # Corresponds to C float.
206 redef fun object_id
is intern
207 redef fun output
is intern
209 fun <=(i
: Float): Bool is intern
210 fun <(i
: Float): Bool is intern
211 fun >=(i
: Float): Bool is intern
212 fun >(i
: Float): Bool is intern
213 fun +(i
: Float): Float is intern
214 fun -: Float is intern
215 fun -(i
: Float): Float is intern
216 fun *(i
: Float): Float is intern
217 fun /(i
: Float): Float is intern
219 # The integer part of `self'.
220 fun to_i
: Int is intern
223 # Native integer numbers.
224 # Correspond to C int.
227 redef type OTHER: Int
229 redef fun object_id
is intern
230 redef fun ==(i
) is intern
231 redef fun !=(i
) is intern
232 redef fun output
is intern
234 redef fun <=(i
) is intern
235 redef fun <(i
) is intern
236 redef fun >=(i
) is intern
237 redef fun >(i
) is intern
238 redef fun +(i
) is intern
240 redef fun -(i
) is intern
241 fun *(i
: Int): Int is intern
242 fun /(i
: Int): Int is intern
243 fun %(i
: Int): Int is intern
244 fun lshift
(i
: Int): Int is intern
245 fun rshift
(i
: Int): Int is intern
247 # The float equivalent of `self'
248 fun to_f
: Float is intern
250 redef fun succ
is intern
251 redef fun prec
is intern
252 redef fun distance
(i
)
266 else if other
< self then
273 redef fun is_between
(c
, d
)
275 if self < c
or d
< self then
300 # The character whose ASCII value is `self'.
301 fun ascii
: Char is intern
303 # Number of digits of an integer in base `b' (plus one if negative)
304 fun digit_count
(b
: Int): Int
306 var d
: Int # number of digits
307 var n
: Int # current number
312 else if self == 0 then
321 n
= n
/ b
# euclidian division /
326 # Return the corresponding digit character
327 # If 0 <= `self' <= 9, return the corresponding character.
328 # If 10 <= `self' <= 36, return the corresponding letter [a..z].
331 assert self >= 0 and self <= 36 # TODO plan for this
333 return (self + '0'.ascii
).ascii
335 return (self + ('a'.ascii
- 10)).ascii
339 # Execute 'each' for each integer in [self..last]
340 fun enumerate_to
(last
: Int)
350 # Execute 'each' for each integer in [self..after[
351 fun enumerate_before
(after
: Int)
361 # The absolute value of self
374 # Characters are denoted with simple quote.
378 redef type OTHER: Char
380 redef fun object_id
is intern
381 redef fun ==(o
) is intern
382 redef fun !=(o
) is intern
383 redef fun output
is intern
385 redef fun <=(i
) is intern
386 redef fun <(i
) is intern
387 redef fun >=(i
) is intern
388 redef fun >(i
) is intern
390 redef fun succ
is intern
391 redef fun prec
is intern
393 redef fun distance
(c
)
395 var d
= self.ascii
- c
.ascii
403 # If `self' is a digit then return this digit else return -1.
409 else if is_digit
then
410 return self.ascii
- '0'.ascii
412 return self.to_lower
.ascii
- ('a'.ascii
+ 10)
416 # the ascii value of self
417 fun ascii
: Int is intern
419 redef fun +(i
) is intern
420 redef fun -(i
) is intern
422 # Return the lower case version of self.
423 # If self is not a letter, then return self
427 return (ascii
+ ('a'.distance
('A'))).ascii
433 # Return the upper case version of self.
434 # If self is not a letter, then return self
438 return (ascii
- ('a'.distance
('A'))).ascii
444 # Is self a digit? (from '0' to '9')
447 return self >= '0' and self <= '9'
450 # Is self a lower case letter? (from 'a' to 'z')
453 return self >= 'a' and self <= 'z'
456 # Is self a upper case letter? (from 'A' to 'Z')
459 return self >= 'A' and self <= 'Z'
462 # Is self a letter? (from 'A' to 'Z' and 'a' to 'z')
465 return is_lower
or is_upper
469 # Pointer classes are used to manipulate extern C structures.