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 # This module is the root of the standard module hierarchy.
17 import end # Mark this module is a top level one. (must be only one)
19 ###############################################################################
21 ###############################################################################
23 # The root of the class hierarchy.
24 # Each class implicitely specialize Object.
26 # The unique object identifier in the class
27 fun object_id
: Int is intern
29 # Return true is `self' and `other' have the same dynamic type
30 fun is_same_type
(other
: Object): Bool is intern
32 # Have `self' and `other' the same value?
34 # Implicitely, the default implementation, is ==
35 fun ==(other
: nullable Object): Bool do return self is other
37 # Have `self' and `other' different values?
39 # != is equivament with "not =".
40 fun !=(other
: nullable Object): Bool do return not (self == other
)
42 # Display self on stdout (debug only).
50 # Display class name on stdout (debug only).
51 fun output_class_name
is intern
53 protected fun exit
(exit_value
: Int) is intern # Quit the program.
54 protected fun sys
: Sys is intern # The global sys object
57 # The main class of the program.
59 # Instructions outside classes implicetely redefine this method.
63 ###############################################################################
65 ###############################################################################
67 # The ancestor of class where objects are in a total order.
68 # In order to work, the method '<' has to be redefined.
70 type OTHER: Comparable
72 # Is `self' lesser than `other'
73 fun <(other
: OTHER): Bool is abstract
75 # not `other' < `self'
76 fun <=(other
: OTHER): Bool do return not other
< self
78 # not `self' < `other'
79 fun >=(other
: OTHER): Bool do return not self < other
82 fun >(other
: OTHER): Bool do return other
< self
84 # -1 if <, +1 if > and 0 otherwise
85 fun <=>(other
: OTHER): Int
89 else if other
< self then
97 fun is_between
(c
: OTHER, d
: OTHER): Bool
99 return c
<= self and self <= d
102 # The maximum between `self' and `other' (prefers `self' if equals).
103 fun max
(other
: OTHER): OTHER
112 # The minimum between `self' and `c' (prefer `self' if equals)
113 fun min
(c
: OTHER): OTHER
123 # Discrete total orders.
127 redef type OTHER: Discrete
130 fun succ
: OTHER do return self + 1
132 # The previous element.
133 fun prec
: OTHER do return self - 1
135 # The `i' th successor element.
136 fun +(i
: Int): OTHER is abstract
138 # The `i' th previous element.
139 fun -(i
: Int): OTHER is abstract
141 # The distance between self and d.
142 # 10.distance(15) # --> 5
143 # 'Z'.distance('A') # --> 25
144 fun distance
(d
: OTHER): Int
151 else if self > d
then
159 while cursor
< stop
do
167 ###############################################################################
169 ###############################################################################
172 # `true' and `false' are the only instances.
173 # Boolean are manipulated trought three special operators:
174 # `and', `or', `not'.
175 # Booleans are mainly used by conditional statement and loops.
177 redef fun object_id
is intern
178 redef fun ==(b
) is intern
179 redef fun !=(b
) is intern
180 redef fun output
is intern
183 # Native floating point numbers.
184 # Corresponds to C float.
186 redef fun object_id
is intern
187 redef fun output
is intern
189 fun <=(i
: Float): Bool is intern
190 fun <(i
: Float): Bool is intern
191 fun >=(i
: Float): Bool is intern
192 fun >(i
: Float): Bool is intern
193 fun +(i
: Float): Float is intern
194 fun -: Float is intern
195 fun -(i
: Float): Float is intern
196 fun *(i
: Float): Float is intern
197 fun /(i
: Float): Float is intern
199 # The integer part of `self'.
200 fun to_i
: Int is intern
203 # Native integer numbers.
204 # Correspond to C int.
207 redef type OTHER: Int
209 redef fun object_id
is intern
210 redef fun ==(i
) is intern
211 redef fun !=(i
) is intern
212 redef fun output
is intern
214 redef fun <=(i
) is intern
215 redef fun <(i
) is intern
216 redef fun >=(i
) is intern
217 redef fun >(i
) is intern
218 redef fun +(i
) is intern
220 redef fun -(i
) is intern
221 fun *(i
: Int): Int is intern
222 fun /(i
: Int): Int is intern
223 fun %(i
: Int): Int is intern
224 fun lshift
(i
: Int): Int is intern
225 fun rshift
(i
: Int): Int is intern
227 # The float equivalent of `self'
228 fun to_f
: Float is intern
230 redef fun succ
is intern
231 redef fun prec
is intern
232 redef fun distance
(i
)
246 else if other
< self then
253 redef fun is_between
(c
, d
)
255 if self < c
or d
< self then
280 # The character whose ASCII value is `self'.
281 fun ascii
: Char is intern
283 # Number of digits of an integer in base `b' plus one if negative)
284 fun digit_count
(b
: Int): Int
286 var d
: Int # number of digits
287 var n
: Int # current number
292 else if self == 0 then
301 n
= n
/ b
# euclidian division /
306 # Return the corresponding digit character
307 # If 0 <= `self' <= 9, return the corresponding character.
308 # If 10 <= `self' <= 36, return the corresponding letter [a..z].
311 assert self >= 0 and self <= 36 # TODO plan for this
313 return (self + '0'.ascii
).ascii
315 return (self + ('a'.ascii
- 10)).ascii
319 # Executre 'each' for each integer in [self..last]
320 fun enumerate_to
(last
: Int)
330 # Executre 'each' for each integer in [self..after[
331 fun enumerate_before
(after
: Int)
343 # Characters are denoted with simple quote.
347 redef type OTHER: Char
349 redef fun object_id
is intern
350 redef fun ==(o
) is intern
351 redef fun !=(o
) is intern
352 redef fun output
is intern
354 redef fun <=(i
) is intern
355 redef fun <(i
) is intern
356 redef fun >=(i
) is intern
357 redef fun >(i
) is intern
359 redef fun succ
is intern
360 redef fun prec
is intern
362 redef fun distance
(c
)
364 var d
= self.ascii
- c
.ascii
372 # If `self' is a digit then return this digit.
378 else if is_digit
then
379 return self.ascii
- '0'.ascii
381 return self.to_lower
.ascii
- ('a'.ascii
+ 10)
385 # the ascii value of self
386 fun ascii
: Int is intern
388 redef fun +(i
) is intern
389 redef fun -(i
) is intern
395 return (ascii
+ ('a'.distance
('A'))).ascii
405 return (ascii
- ('a'.distance
('A'))).ascii
413 return self >= '0' and self <= '9'
418 return self >= 'a' and self <= 'z'
423 return self >= 'A' and self <= 'Z'
428 return is_lower
or is_upper
432 # Pointer classes are used to manipulate extern C structures.