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 # Type of this instance, automatically specialized in every class
35 # A common use case of the virtual type `SELF` is to type an attribute and
36 # store another instance of the same type as `self`. It can also be used as as
37 # return type to a method producing a copy of `self` or returning an instance
38 # expected to be the exact same type as self.
40 # This virtual type must be used with caution as it can hinder specialization.
41 # In fact, it imposes strict restrictions on all sub-classes and their usage.
42 # For example, using `SELF` as a return type of a method `foo`
43 # forces all subclasses to ensure that `foo` returns the correct and updated
45 # A dangerous usage take the form of a method typed by `SELF` which creates
46 # and returns a new instance.
47 # If not correctly specialized, this method would break when invoked on a
50 # A general rule for safe usage of `SELF` is to ensure that inputs typed
51 # `SELF` are stored in attributes typed `SELF` and returned by methods typed
52 # `SELF`, pretty much the same things as you would do with parameter types.
55 # The unique object identifier in the class.
56 # Unless specific code, you should not use this method.
57 # The identifier is used internally to provide a hash value.
58 fun object_id
: Int is intern
60 # Return true if `self` and `other` have the same dynamic type.
61 # Unless specific code, you should not use this method.
62 fun is_same_type
(other
: Object): Bool is intern
64 # Return true if `self` and `other` are the same instance.
65 # Unless specific code, you should use `==` instead.
66 fun is_same_instance
(other
: nullable Object): Bool is intern
68 # Have `self` and `other` the same value?
70 # The exact meaning of "same value" is let to the subclasses.
71 # Implicitly, the default implementation, is `is_same_instance`
72 fun ==(other
: nullable Object): Bool do return self.is_same_instance
(other
)
74 # Have `self` and `other` different values?
76 # != is equivalent with "not ==".
77 fun !=(other
: nullable Object): Bool do return not (self == other
)
79 # Display self on stdout (debug only).
80 # This method MUST not be used by programs, it is here for debugging
81 # only and can be removed without any notice
89 # Display class name on stdout (debug only).
90 # This method MUST not be used by programs, it is here for debugging
91 # only and can be removed without any notice
92 fun output_class_name
is intern
94 # The hash code of the object.
95 # Assuming that a == b -> a.hash == b.hash
97 # Without redefinition, it is based on the `object_id` of the instance.
98 fun hash
: Int do return object_id
/ 8
101 # The main class of the program.
102 # `Sys` is a singleton class, its only instance is `sys` defined in `Object`.
103 # `sys` is used to invoke methods on the program on the system.
105 # Instructions outside classes implicitly redefine this method.
108 # The entry point for the execution of the whole program.
109 # Its job is to call `main` but some modules may want to refine it
110 # and inject specific work before or after the main part.
113 # Number of the last error
114 fun errno
: Int is extern `{
119 # Quit the program with a specific return code
120 fun exit
(exit_value
: Int) is intern
122 # Return the global sys object, the only instance of the `Sys` class.
123 fun sys
: Sys is intern
126 ###############################################################################
128 ###############################################################################
130 # The ancestor of class where objects are in a total order.
131 # In order to work, the method '<' has to be redefined.
133 # What `self` can be compared to?
134 type OTHER: Comparable
136 # Is `self` lesser than `other`?
137 fun <(other
: OTHER): Bool is abstract
139 # not `other` < `self`
140 # Note, the implementation must ensure that: `(x<=y) == (x<y or x==y)`
141 fun <=(other
: OTHER): Bool do return not other
< self
143 # not `self` < `other`
144 # Note, the implementation must ensure that: `(x>=y) == (x>y or x==y)`
145 fun >=(other
: OTHER): Bool do return not self < other
148 fun >(other
: OTHER): Bool do return other
< self
150 # -1 if <, +1 if > and 0 otherwise
151 # Note, the implementation must ensure that: (x<=>y == 0) == (x==y)
152 fun <=>(other
: OTHER): Int
156 else if other
< self then
164 fun is_between
(c
: OTHER, d
: OTHER): Bool
166 return c
<= self and self <= d
169 # The maximum between `self` and `other` (prefers `self` if equals).
170 fun max
(other
: OTHER): OTHER
179 # The minimum between `self` and `c` (prefer `self` if equals)
180 fun min
(c
: OTHER): OTHER
190 # Discrete total orders.
194 redef type OTHER: Discrete
197 fun successor
(i
: Int): OTHER is abstract
199 # The previous element.
200 fun predecessor
(i
: Int): OTHER is abstract
202 # The distance between self and d.
204 # assert 10.distance(15) == 5
205 # assert 'Z'.distance('A') == 25
206 fun distance
(d
: OTHER): Int
213 else if self > d
then
221 while cursor
< stop
do
222 cursor
= cursor
.successor
(1)
229 # A numeric value supporting mathematical operations
233 redef type OTHER: Numeric
235 # Addition of `self` with `i`
236 fun +(i
: OTHER): OTHER is abstract
238 # Substraction of `i` from `self`
239 fun -(i
: OTHER): OTHER is abstract
242 fun -: OTHER is abstract
244 # Multiplication of `self` with `i`
245 fun *(i
: OTHER): OTHER is abstract
247 # Division of `self` with `i`
248 fun /(i
: OTHER): OTHER is abstract
250 # The integer part of `self`.
252 # assert (0.0).to_i == 0
253 # assert (0.9).to_i == 0
254 # assert (-0.9).to_i == 0
255 # assert (9.9).to_i == 9
256 # assert (-9.9).to_i == -9
257 fun to_i
: Int is abstract
259 # The float equivalent of `self`
261 # assert 5.to_f == 5.0
262 # assert 5.to_f != 5 # Float and Int are not equals
263 fun to_f
: Float is abstract
265 # Is this the value of zero in its domain?
266 fun is_zero
: Bool do return self == zero
268 # The value of zero in the domain of `self`
269 fun zero
: OTHER is abstract
271 # The value of `val` in the domain of `self`
273 # assert 1.0.value_of(2) == 2.0
274 # assert 1.0.value_of(2.0) == 2.0
275 # assert 1.value_of(2) == 2
276 # assert 1.value_of(2.0) == 2
277 fun value_of
(val
: Numeric): OTHER is abstract
280 ###############################################################################
282 ###############################################################################
285 # `true` and `false` are the only instances.
286 # Boolean are manipulated trough three special operators:
287 # `and`, `or`, `not`.
288 # Booleans are mainly used by conditional statement and loops.
290 redef fun object_id
is intern
291 redef fun ==(b
) is intern
292 redef fun !=(b
) is intern
293 redef fun output
is intern
294 redef fun hash
do return to_i
296 # 1 if true and 0 if false
307 # Native floating point numbers.
308 # Corresponds to C float.
312 redef type OTHER: Float
314 redef fun object_id
is intern
315 redef fun ==(i
) is intern
316 redef fun !=(i
) is intern
317 redef fun output
is intern
319 redef fun <=(i
): Bool is intern
320 redef fun <(i
): Bool is intern
321 redef fun >=(i
): Bool is intern
322 redef fun >(i
): Bool is intern
324 redef fun +(i
) is intern
325 redef fun - is intern
326 redef fun -(i
) is intern
327 redef fun *(i
) is intern
328 redef fun /(i
) is intern
330 redef fun to_i
is intern
331 redef fun to_f
do return self
333 redef fun zero
do return 0.0
334 redef fun value_of
(val
) do return val
.to_f
340 else if other
< self then
347 redef fun is_between
(c
, d
)
349 if self < c
or d
< self then
356 # Compare float numbers with a given precision.
358 # Because of the loss of precision in floating numbers,
359 # the `==` method is often not the best way to compare them.
362 # assert 0.01.is_approx(0.02, 0.1) == true
363 # assert 0.01.is_approx(0.02, 0.001) == false
365 fun is_approx
(other
, precision
: Float): Bool
367 assert precision
>= 0.0
368 return self <= other
+ precision
and self >= other
- precision
390 # Native integer numbers.
391 # Correspond to C int.
396 redef type OTHER: Int
398 redef fun successor
(i
) do return self + i
399 redef fun predecessor
(i
) do return self - i
401 redef fun object_id
is intern
402 redef fun hash
do return self
403 redef fun ==(i
) is intern
404 redef fun !=(i
) is intern
405 redef fun output
is intern
407 redef fun <=(i
) is intern
408 redef fun <(i
) is intern
409 redef fun >=(i
) is intern
410 redef fun >(i
) is intern
411 redef fun +(i
) is intern
413 redef fun - is intern
414 redef fun -(i
) is intern
415 redef fun *(i
) is intern
416 redef fun /(i
) is intern
417 fun %(i
: Int): Int is intern
419 redef fun zero
do return 0
420 redef fun value_of
(val
) do return val
.to_i
422 # `i` bits shift fo the left (aka <<)
424 # assert 5.lshift(1) == 10
425 fun lshift
(i
: Int): Int is intern
427 # `i` bits shift fo the right (aka >>)
429 # assert 5.rshift(1) == 2
430 fun rshift
(i
: Int): Int is intern
432 redef fun to_i
do return self
433 redef fun to_f
is intern
435 redef fun distance
(i
)
449 else if other
< self then
456 redef fun is_between
(c
, d
)
458 if self < c
or d
< self then
483 # The character whose ASCII value is `self`.
485 # assert 65.ascii == 'A'
486 # assert 10.ascii == '\n'
487 fun ascii
: Char is intern
489 # Number of digits of an integer in base `b` (plus one if negative)
491 # assert 123.digit_count(10) == 3
492 # assert 123.digit_count(2) == 7 # 1111011 in binary
493 fun digit_count
(b
: Int): Int
495 if b
== 10 then return digit_count_base_10
496 var d
: Int # number of digits
497 var n
: Int # current number
502 else if self == 0 then
511 n
= n
/ b
# euclidian division /
516 # Optimized version for base 10
517 fun digit_count_base_10
: Int
529 if val
< 10 then return result
530 if val
< 100 then return result
+1
531 if val
< 1000 then return result
+2
532 if val
< 10000 then return result
+3
538 # Return the corresponding digit character
539 # If 0 <= `self` <= 9, return the corresponding character.
540 # assert 5.to_c == '5'
541 # If 10 <= `self` <= 36, return the corresponding letter [a..z].
542 # assert 15.to_c == 'f'
545 assert self >= 0 and self <= 36 # TODO plan for this
547 return (self + '0'.ascii
).ascii
549 return (self + ('a'.ascii
- 10)).ascii
553 # The absolute value of self
555 # assert (-10).abs == 10
556 # assert 10.abs == 10
570 # Characters are denoted with simple quote.
571 # eg. `'a'` or `'\n'`.
574 redef type OTHER: Char
576 redef fun object_id
is intern
577 redef fun hash
do return ascii
578 redef fun ==(o
) is intern
579 redef fun !=(o
) is intern
580 redef fun output
is intern
582 redef fun <=(i
) is intern
583 redef fun <(i
) is intern
584 redef fun >=(i
) is intern
585 redef fun >(i
) is intern
587 redef fun successor
(i
) is intern
588 redef fun predecessor
(i
) is intern
590 redef fun distance
(c
)
592 var d
= self.ascii
- c
.ascii
600 # If `self` is a digit then return this digit else return -1.
602 # assert '5'.to_i == 5
608 else if is_digit
then
609 return self.ascii
- '0'.ascii
611 return self.to_lower
.ascii
- 'a'.ascii
+ 10
615 # the ascii value of self
617 # assert 'a'.ascii == 97
618 # assert '\n'.ascii == 10
619 fun ascii
: Int is intern
621 # Return the lower case version of self.
622 # If self is not a letter, then return self
624 # assert 'A'.to_lower == 'a'
625 # assert 'a'.to_lower == 'a'
626 # assert '$'.to_lower == '$'
630 return (ascii
+ ('a'.distance
('A'))).ascii
636 # Return the upper case version of self.
637 # If self is not a letter, then return self
639 # assert 'a'.to_upper == 'A'
640 # assert 'A'.to_upper == 'A'
641 # assert '$'.to_upper == '$'
645 return (ascii
- ('a'.distance
('A'))).ascii
651 # Is self a digit? (from '0' to '9')
653 # assert '0'.is_digit == true
654 # assert '9'.is_digit == true
655 # assert 'a'.is_digit == false
658 return self >= '0' and self <= '9'
661 # Is self a lower case letter? (from 'a' to 'z')
663 # assert 'a'.is_lower == true
664 # assert 'z'.is_lower == true
665 # assert 'A'.is_lower == false
666 # assert '$'.is_lower == false
669 return self >= 'a' and self <= 'z'
672 # Is self a upper case letter? (from 'A' to 'Z')
674 # assert 'A'.is_upper == true
675 # assert 'A'.is_upper == true
676 # assert 'z'.is_upper == false
677 # assert '$'.is_upper == false
680 return self >= 'A' and self <= 'Z'
683 # Is self a letter? (from 'A' to 'Z' and 'a' to 'z')
685 # assert 'A'.is_letter == true
686 # assert 'A'.is_letter == true
687 # assert 'z'.is_letter == true
688 # assert '$'.is_letter == false
691 return is_lower
or is_upper
695 # Pointer classes are used to manipulate extern C structures.
697 # Is the address behind this Object at NULL?
698 fun address_is_null
: Bool is extern "address_is_null"
700 # Free the memory pointed by this pointer
701 fun free
`{ free(recv); `}