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 meth object_id
: Int is intern
29 # Return true is `self' and `other' have the same dynamic type
30 meth is_same_type
(other
: Object): Bool is intern
32 # Have `self' and `other' the same value?
34 # Implicitely, the default implementation, is ==
35 meth
==(other
: Object): Bool do return self is other
37 # Have `self' and `other' different values?
39 # != is equivament with "not =".
40 meth
!=(other
: Object): Bool do return not (self == other
)
42 # Display self on stdout (debug only).
50 protected meth exit
(exit_value
: Int) is intern # Quit the program.
51 protected meth 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 meth
<(other
: OTHER): Bool is abstract
72 # not `other' < `self'
73 meth
<=(other
: OTHER): Bool do return not other
< self
75 # not `self' < `other'
76 meth
>=(other
: OTHER): Bool do return not self < other
79 meth
>(other
: OTHER): Bool do return other
< self
81 # -1 if <, +1 if > and 0 otherwise
82 meth
<=>(other
: OTHER): Int
86 else if other
< self then
94 meth 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 meth max
(other
: OTHER): OTHER
109 # The minimum between `self' and `c' (prefer `self' if equals)
110 meth min
(c
: OTHER): OTHER
120 # Discrete total orders.
124 redef type OTHER: Discrete
127 meth succ
: OTHER do return self + 1
129 # The previous element.
130 meth prec
: OTHER do return self - 1
132 # The `i' th successor element.
133 meth
+(i
: Int): OTHER is abstract
135 # The `i' th previous element.
136 meth
-(i
: Int): OTHER is abstract
138 # The distance between self and d.
139 # 10.distance(15) # --> 5
140 # 'Z'.distance('A') # --> 25
141 meth 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 meth object_id
is intern
175 redef meth
==(b
) is intern
176 redef meth
!=(b
) is intern
177 redef meth output
is intern
180 # Native floating point numbers.
181 # Corresponds to C float.
183 redef meth object_id
is intern
184 redef meth output
is intern
186 meth
<=(i
: Float): Bool is intern
187 meth
<(i
: Float): Bool is intern
188 meth
>=(i
: Float): Bool is intern
189 meth
>(i
: Float): Bool is intern
190 meth
+(i
: Float): Float is intern
191 meth
-: Float is intern
192 meth
-(i
: Float): Float is intern
193 meth
*(i
: Float): Float is intern
194 meth
/(i
: Float): Float is intern
196 # The integer part of `self'.
197 meth to_i
: Int is intern
200 # Native integer numbers.
201 # Correspond to C int.
204 redef type OTHER: Int
206 redef meth object_id
is intern
207 redef meth
==(i
) is intern
208 redef meth
!=(i
) is intern
209 redef meth output
is intern
211 redef meth
<=(i
) is intern
212 redef meth
<(i
) is intern
213 redef meth
>=(i
) is intern
214 redef meth
>(i
) is intern
215 redef meth
+(i
) is intern
216 meth
-: Int is intern
217 redef meth
-(i
) is intern
218 meth
*(i
: Int): Int is intern
219 meth
/(i
: Int): Int is intern
220 meth
%(i
: Int): Int is intern
221 meth lshift
(i
: Int): Int is intern
222 meth rshift
(i
: Int): Int is intern
224 # The float equivalent of `self'
225 meth to_f
: Float is intern
227 redef meth succ
is intern
228 redef meth prec
is intern
229 redef meth distance
(i
)
239 redef meth
<=>(other
)
243 else if other
< self then
250 redef meth is_between
(c
, d
)
252 if self < c
or d
< self then
259 redef meth max
(other
)
277 # The character whose ASCII value is `self'.
278 meth ascii
: Char is intern
280 # Number of digits of an integer in base `b' plus one if negative)
281 meth 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
318 # Characters are denoted with simple quote.
322 redef type OTHER: Char
324 redef meth object_id
is intern
325 redef meth
==(o
) is intern
326 redef meth
!=(o
) is intern
327 redef meth output
is intern
329 redef meth
<=(i
) is intern
330 redef meth
<(i
) is intern
331 redef meth
>=(i
) is intern
332 redef meth
>(i
) is intern
334 redef meth succ
is intern
335 redef meth prec
is intern
337 redef meth distance
(c
)
339 var d
= self.ascii
- c
.ascii
347 # If `self' is a digit then return this digit.
353 else if self >= '0' and self <= '9' then
354 return self.ascii
- '0'.ascii
356 return self.to_lower
.ascii
- ('a'.ascii
+ 10)
360 # the ascii value of self
361 meth ascii
: Int is intern
363 redef meth
+(i
) is intern
364 redef meth
-(i
) is intern
369 if self >= 'A' and self <= 'Z' then
370 return (ascii
+ ('a'.distance
('A'))).ascii
379 if self >= 'a' and self <= 'z' then
380 return (ascii
- ('a'.distance
('A'))).ascii
387 # Pointer classes are used to manipulate extern C structures.