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 protected fun exit
(exit_value
: Int) is intern # Quit the program.
51 protected fun sys
: Sys is intern # The global sys object
54 # The main class of the program.
56 # Instructions outside classes implicetely redefine this method.
60 ###############################################################################
62 ###############################################################################
64 # The ancestor of class where objects are in a total order.
65 # In order to work, the method '<' has to be redefined.
67 type OTHER: Comparable
69 # Is `self' lesser than `other'
70 fun <(other
: OTHER): Bool is abstract
72 # not `other' < `self'
73 fun <=(other
: OTHER): Bool do return not other
< self
75 # not `self' < `other'
76 fun >=(other
: OTHER): Bool do return not self < other
79 fun >(other
: OTHER): Bool do return other
< self
81 # -1 if <, +1 if > and 0 otherwise
82 fun <=>(other
: OTHER): Int
86 else if other
< self then
94 fun is_between
(c
: OTHER, d
: OTHER): Bool
96 return c
<= self and self <= d
99 # The maximum between `self' and `other' (prefers `self' if equals).
100 fun max
(other
: OTHER): OTHER
109 # The minimum between `self' and `c' (prefer `self' if equals)
110 fun min
(c
: OTHER): OTHER
120 # Discrete total orders.
124 redef type OTHER: Discrete
127 fun succ
: OTHER do return self + 1
129 # The previous element.
130 fun prec
: OTHER do return self - 1
132 # The `i' th successor element.
133 fun +(i
: Int): OTHER is abstract
135 # The `i' th previous element.
136 fun -(i
: Int): OTHER is abstract
138 # The distance between self and d.
139 # 10.distance(15) # --> 5
140 # 'Z'.distance('A') # --> 25
141 fun distance
(d
: OTHER): Int
148 else if self > d
then
156 while cursor
< stop
do
164 ###############################################################################
166 ###############################################################################
169 # `true' and `false' are the only instances.
170 # Boolean are manipulated trought three special operators:
171 # `and', `or', `not'.
172 # Booleans are mainly used by conditional statement and loops.
174 redef fun object_id
is intern
175 redef fun ==(b
) is intern
176 redef fun !=(b
) is intern
177 redef fun output
is intern
180 # Native floating point numbers.
181 # Corresponds to C float.
183 redef fun object_id
is intern
184 redef fun output
is intern
186 fun <=(i
: Float): Bool is intern
187 fun <(i
: Float): Bool is intern
188 fun >=(i
: Float): Bool is intern
189 fun >(i
: Float): Bool is intern
190 fun +(i
: Float): Float is intern
191 fun -: Float is intern
192 fun -(i
: Float): Float is intern
193 fun *(i
: Float): Float is intern
194 fun /(i
: Float): Float is intern
196 # The integer part of `self'.
197 fun to_i
: Int is intern
200 # Native integer numbers.
201 # Correspond to C int.
204 redef type OTHER: Int
206 redef fun object_id
is intern
207 redef fun ==(i
) is intern
208 redef fun !=(i
) is intern
209 redef fun output
is intern
211 redef fun <=(i
) is intern
212 redef fun <(i
) is intern
213 redef fun >=(i
) is intern
214 redef fun >(i
) is intern
215 redef fun +(i
) is intern
217 redef fun -(i
) is intern
218 fun *(i
: Int): Int is intern
219 fun /(i
: Int): Int is intern
220 fun %(i
: Int): Int is intern
221 fun lshift
(i
: Int): Int is intern
222 fun rshift
(i
: Int): Int is intern
224 # The float equivalent of `self'
225 fun to_f
: Float is intern
227 redef fun succ
is intern
228 redef fun prec
is intern
229 redef fun distance
(i
)
243 else if other
< self then
250 redef fun is_between
(c
, d
)
252 if self < c
or d
< self then
277 # The character whose ASCII value is `self'.
278 fun ascii
: Char is intern
280 # Number of digits of an integer in base `b' plus one if negative)
281 fun digit_count
(b
: Int): Int
283 var d
: Int # number of digits
284 var n
: Int # current number
289 else if self == 0 then
298 n
= n
/ b
# euclidian division /
303 # Return the corresponding digit character
304 # If 0 <= `self' <= 9, return the corresponding character.
305 # If 10 <= `self' <= 36, return the corresponding letter [a..z].
308 assert self >= 0 and self <= 36 # TODO plan for this
310 return (self + '0'.ascii
).ascii
312 return (self + ('a'.ascii
- 10)).ascii
316 # Executre 'each' for each integer in [self..last]
317 fun enumerate_to
(last
: Int)
327 # Executre 'each' for each integer in [self..after[
328 fun enumerate_before
(after
: Int)
340 # Characters are denoted with simple quote.
344 redef type OTHER: Char
346 redef fun object_id
is intern
347 redef fun ==(o
) is intern
348 redef fun !=(o
) is intern
349 redef fun output
is intern
351 redef fun <=(i
) is intern
352 redef fun <(i
) is intern
353 redef fun >=(i
) is intern
354 redef fun >(i
) is intern
356 redef fun succ
is intern
357 redef fun prec
is intern
359 redef fun distance
(c
)
361 var d
= self.ascii
- c
.ascii
369 # If `self' is a digit then return this digit.
375 else if is_digit
then
376 return self.ascii
- '0'.ascii
378 return self.to_lower
.ascii
- ('a'.ascii
+ 10)
382 # the ascii value of self
383 fun ascii
: Int is intern
385 redef fun +(i
) is intern
386 redef fun -(i
) is intern
392 return (ascii
+ ('a'.distance
('A'))).ascii
402 return (ascii
- ('a'.distance
('A'))).ascii
410 return self >= '0' and self <= '9'
415 return self >= 'a' and self <= 'z'
420 return self >= 'A' and self <= 'Z'
425 return is_lower
or is_upper
429 # Pointer classes are used to manipulate extern C structures.