lib/standard: Introduce Byte type in kernel
[nit.git] / lib / standard / kernel.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
5 #
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
12 # another product.
13
14 # Most minimal classes and methods.
15 # This module is the root of the standard module hierarchy.
16 module kernel
17
18 import end # Mark this module is a top level one. (must be only one)
19
20 ###############################################################################
21 # System Classes #
22 ###############################################################################
23
24 # The root of the class hierarchy.
25 # Each class implicitly specialize Object.
26 #
27 # Currently, Object is also used to collect all top-level methods.
28 interface Object
29 # Type of this instance, automatically specialized in every class
30 #
31 # A common use case of the virtual type `SELF` is to type an attribute and
32 # store another instance of the same type as `self`. It can also be used as as
33 # return type to a method producing a copy of `self` or returning an instance
34 # expected to be the exact same type as self.
35 #
36 # This virtual type must be used with caution as it can hinder specialization.
37 # In fact, it imposes strict restrictions on all sub-classes and their usage.
38 # For example, using `SELF` as a return type of a method `foo`
39 # forces all subclasses to ensure that `foo` returns the correct and updated
40 # type.
41 # A dangerous usage take the form of a method typed by `SELF` which creates
42 # and returns a new instance.
43 # If not correctly specialized, this method would break when invoked on a
44 # sub-class.
45 #
46 # A general rule for safe usage of `SELF` is to ensure that inputs typed
47 # `SELF` are stored in attributes typed `SELF` and returned by methods typed
48 # `SELF`, pretty much the same things as you would do with parameter types.
49 type SELF: Object
50
51 # The unique object identifier in the class.
52 # Unless specific code, you should not use this method.
53 # The identifier is used internally to provide a hash value.
54 fun object_id: Int is intern
55
56 # Return true if `self` and `other` have the same dynamic type.
57 # Unless specific code, you should not use this method.
58 fun is_same_type(other: Object): Bool is intern
59
60 # Return true if `self` and `other` are the same instance.
61 # Unless specific code, you should use `==` instead.
62 fun is_same_instance(other: nullable Object): Bool is intern
63
64 # Have `self` and `other` the same value?
65 #
66 # The exact meaning of "same value" is left to the subclasses.
67 # Implicitly, the default implementation, is `is_same_instance`
68 fun ==(other: nullable Object): Bool do return self.is_same_instance(other)
69
70 # Have `self` and `other` different values?
71 #
72 # != is equivalent with "not ==".
73 fun !=(other: nullable Object): Bool do return not (self == other)
74
75 # Display self on stdout (debug only).
76 # This method MUST not be used by programs, it is here for debugging
77 # only and can be removed without any notice
78 fun output
79 do
80 '<'.output
81 object_id.output
82 '>'.output
83 end
84
85 # Display class name on stdout (debug only).
86 # This method MUST not be used by programs, it is here for debugging
87 # only and can be removed without any notice
88 fun output_class_name is intern
89
90 # The hash code of the object.
91 # Assuming that a == b -> a.hash == b.hash
92 #
93 # Without redefinition, it is based on the `object_id` of the instance.
94 fun hash: Int do return object_id / 8
95 end
96
97 # The main class of the program.
98 # `Sys` is a singleton class, its only instance is `sys` defined in `Object`.
99 # `sys` is used to invoke methods on the program on the system.
100 class Sys
101 # Instructions outside classes implicitly redefine this method.
102 fun main do end
103
104 # The entry point for the execution of the whole program.
105 # Its job is to call `main` but some modules may want to refine it
106 # and inject specific work before or after the main part.
107 fun run do main
108
109 # Number of the last error
110 fun errno: Int is extern "sys_errno"
111 end
112
113 # Quit the program with a specific return code
114 fun exit(exit_value: Int) is intern
115
116 # Return the global sys object, the only instance of the `Sys` class.
117 fun sys: Sys is intern
118
119
120 ###############################################################################
121 # Abstract Classes #
122 ###############################################################################
123
124 # The ancestor of class where objects are in a total order.
125 # In order to work, the method '<' has to be redefined.
126 interface Comparable
127 # What `self` can be compared to?
128 type OTHER: Comparable
129
130 # Is `self` lesser than `other`?
131 fun <(other: OTHER): Bool is abstract
132
133 # not `other` < `self`
134 # Note, the implementation must ensure that: `(x<=y) == (x<y or x==y)`
135 fun <=(other: OTHER): Bool do return not other < self
136
137 # not `self` < `other`
138 # Note, the implementation must ensure that: `(x>=y) == (x>y or x==y)`
139 fun >=(other: OTHER): Bool do return not self < other
140
141 # `other` < `self`
142 fun >(other: OTHER): Bool do return other < self
143
144 # -1 if <, +1 if > and 0 otherwise
145 # Note, the implementation must ensure that: (x<=>y == 0) == (x==y)
146 fun <=>(other: OTHER): Int
147 do
148 if self < other then
149 return -1
150 else if other < self then
151 return 1
152 else
153 return 0
154 end
155 end
156
157 # c <= self <= d
158 fun is_between(c: OTHER, d: OTHER): Bool
159 do
160 return c <= self and self <= d
161 end
162
163 # The maximum between `self` and `other` (prefers `self` if equals).
164 fun max(other: OTHER): OTHER
165 do
166 if self < other then
167 return other
168 else
169 return self
170 end
171 end
172
173 # The minimum between `self` and `c` (prefer `self` if equals)
174 fun min(c: OTHER): OTHER
175 do
176 if c < self then
177 return c
178 else
179 return self
180 end
181 end
182 end
183
184 # Discrete total orders.
185 interface Discrete
186 super Comparable
187
188 redef type OTHER: Discrete
189
190 # The next element.
191 fun successor(i: Int): OTHER is abstract
192
193 # The previous element.
194 fun predecessor(i: Int): OTHER is abstract
195
196 # The distance between self and d.
197 #
198 # assert 10.distance(15) == 5
199 # assert 'Z'.distance('A') == 25
200 fun distance(d: OTHER): Int
201 do
202 var cursor: OTHER
203 var stop: OTHER
204 if self < d then
205 cursor = self
206 stop = d
207 else if self > d then
208 cursor = d
209 stop = self
210 else
211 return 0
212 end
213
214 var nb = 0
215 while cursor < stop do
216 cursor = cursor.successor(1)
217 nb += 1
218 end
219 return nb
220 end
221 end
222
223 # Something that can be cloned
224 #
225 # This interface introduces the `clone` method used to duplicate an instance
226 # Its specific semantic is left to the subclasses.
227 interface Cloneable
228 # Duplicate `self`
229 #
230 # The specific semantic of this method is left to the subclasses;
231 # Especially, if (and how) attributes are cloned (depth vs. shallow).
232 #
233 # As a rule of thumb, the principle of least astonishment should
234 # be used to guide the semantic.
235 #
236 # Note that as the returned clone depends on the semantic,
237 # the `==` method, if redefined, should ensure the equality
238 # between an object and its clone.
239 fun clone: SELF is abstract
240 end
241
242 # A numeric value supporting mathematical operations
243 interface Numeric
244 super Comparable
245
246 redef type OTHER: Numeric
247
248 # Addition of `self` with `i`
249 fun +(i: OTHER): OTHER is abstract
250
251 # Substraction of `i` from `self`
252 fun -(i: OTHER): OTHER is abstract
253
254 # Inverse of `self`
255 fun -: OTHER is abstract
256
257 # Multiplication of `self` with `i`
258 fun *(i: OTHER): OTHER is abstract
259
260 # Division of `self` with `i`
261 fun /(i: OTHER): OTHER is abstract
262
263 # The integer part of `self`.
264 #
265 # assert (0.0).to_i == 0
266 # assert (0.9).to_i == 0
267 # assert (-0.9).to_i == 0
268 # assert (9.9).to_i == 9
269 # assert (-9.9).to_i == -9
270 fun to_i: Int is abstract
271
272 # The float equivalent of `self`
273 #
274 # assert 5.to_f == 5.0
275 # assert 5.to_f != 5 # Float and Int are not equals
276 fun to_f: Float is abstract
277
278 # The byte equivalent of `self`
279 #
280 # assert (-1).to_b == 0xFF.to_b
281 # assert (1.9).to_b == 1.to_b
282 fun to_b: Byte is abstract
283
284 # Is this the value of zero in its domain?
285 fun is_zero: Bool do return self == zero
286
287 # The value of zero in the domain of `self`
288 fun zero: OTHER is abstract
289
290 # The value of `val` in the domain of `self`
291 #
292 # assert 1.0.value_of(2) == 2.0
293 # assert 1.0.value_of(2.0) == 2.0
294 # assert 1.value_of(2) == 2
295 # assert 1.value_of(2.0) == 2
296 fun value_of(val: Numeric): OTHER is abstract
297 end
298
299 ###############################################################################
300 # Native classes #
301 ###############################################################################
302
303 # Native Booleans.
304 # `true` and `false` are the only instances.
305 #
306 # Boolean are manipulated trough three special operators:
307 # `and`, `or`, `not`.
308 #
309 # Booleans are mainly used by conditional statement and loops.
310 universal Bool
311 redef fun object_id is intern
312 redef fun ==(b) is intern
313 redef fun !=(b) is intern
314 redef fun output is intern
315 redef fun hash do return to_i
316
317 # 1 if true and 0 if false
318 fun to_i: Int
319 do
320 if self then
321 return 1
322 else
323 return 0
324 end
325 end
326 end
327
328 # Native floating point numbers.
329 # Corresponds to C float.
330 universal Float
331 super Numeric
332
333 redef type OTHER: Float
334
335 redef fun object_id is intern
336 redef fun ==(i) is intern
337 redef fun !=(i) is intern
338 redef fun output is intern
339
340 redef fun <=(i) is intern
341 redef fun <(i) is intern
342 redef fun >=(i) is intern
343 redef fun >(i) is intern
344
345 redef fun +(i) is intern
346 redef fun - is intern
347 redef fun -(i) is intern
348 redef fun *(i) is intern
349 redef fun /(i) is intern
350
351 redef fun to_i is intern
352 redef fun to_f do return self
353 redef fun to_b is intern
354
355 redef fun zero do return 0.0
356 redef fun value_of(val) do return val.to_f
357
358 redef fun <=>(other)
359 do
360 if self < other then
361 return -1
362 else if other < self then
363 return 1
364 else
365 return 0
366 end
367 end
368
369 redef fun is_between(c, d)
370 do
371 if self < c or d < self then
372 return false
373 else
374 return true
375 end
376 end
377
378 # Compare float numbers with a given precision.
379 #
380 # Because of the loss of precision in floating numbers,
381 # the `==` method is often not the best way to compare them.
382 #
383 # ~~~
384 # assert 0.01.is_approx(0.02, 0.1) == true
385 # assert 0.01.is_approx(0.02, 0.001) == false
386 # ~~~
387 fun is_approx(other, precision: Float): Bool
388 do
389 assert precision >= 0.0
390 return self <= other + precision and self >= other - precision
391 end
392
393 redef fun max(other)
394 do
395 if self < other then
396 return other
397 else
398 return self
399 end
400 end
401
402 redef fun min(c)
403 do
404 if c < self then
405 return c
406 else
407 return self
408 end
409 end
410 end
411
412 # Native bytes.
413 # Same as a C `unsigned char`
414 universal Byte
415 super Discrete
416 super Numeric
417
418 redef type OTHER: Byte
419
420 redef fun successor(i) do return self + i.to_b
421 redef fun predecessor(i) do return self - i.to_b
422
423 redef fun object_id is intern
424 redef fun hash do return self.to_i
425 redef fun ==(i) is intern
426 redef fun !=(i) is intern
427 redef fun output is intern
428
429 redef fun <=(i) is intern
430 redef fun <(i) is intern
431 redef fun >=(i) is intern
432 redef fun >(i) is intern
433 redef fun +(i) is intern
434
435 # On an Byte, unary minus will return `(256 - self) % 256`
436 #
437 # assert -(1.to_b) == 0xFF.to_b
438 # assert -(0.to_b) == 0x00.to_b
439 redef fun - is intern
440 redef fun -(i) is intern
441 redef fun *(i) is intern
442 redef fun /(i) is intern
443
444 # Modulo of `self` with `i`.
445 #
446 # Finds the remainder of division of `self` by `i`.
447 #
448 # assert 5.to_b % 2.to_b == 1.to_b
449 # assert 10.to_b % 2.to_b == 0.to_b
450 fun %(i: Byte): Byte is intern
451
452 redef fun zero do return 0.to_b
453 redef fun value_of(val) do return val.to_b
454
455 # `i` bits shift fo the left (aka <<)
456 #
457 # assert 5.to_b.lshift(1) == 10.to_b
458 fun lshift(i: Int): Byte is intern
459
460 # alias of `lshift`
461 fun <<(i: Int): Byte do return lshift(i)
462
463 # `i` bits shift fo the right (aka >>)
464 #
465 # assert 5.to_b.rshift(1) == 2.to_b
466 fun rshift(i: Int): Byte is intern
467
468 # alias of `rshift`
469 fun >>(i: Int): Byte do return rshift(i)
470
471 redef fun to_i is intern
472 redef fun to_f is intern
473 redef fun to_b do return self
474
475 redef fun distance(i) do return (self - i).to_i
476
477 redef fun <=>(other)
478 do
479 if self < other then
480 return -1
481 else if other < self then
482 return 1
483 else
484 return 0
485 end
486 end
487
488 redef fun is_between(c, d)
489 do
490 if self < c or d < self then
491 return false
492 else
493 return true
494 end
495 end
496
497 redef fun max(other)
498 do
499 if self < other then
500 return other
501 else
502 return self
503 end
504 end
505
506 redef fun min(c)
507 do
508 if c < self then
509 return c
510 else
511 return self
512 end
513 end
514 end
515
516 # Native integer numbers.
517 # Correspond to C int.
518 universal Int
519 super Discrete
520 super Numeric
521
522 redef type OTHER: Int
523
524 redef fun successor(i) do return self + i
525 redef fun predecessor(i) do return self - i
526
527 redef fun object_id is intern
528 redef fun hash do return self
529 redef fun ==(i) is intern
530 redef fun !=(i) is intern
531 redef fun output is intern
532
533 redef fun <=(i) is intern
534 redef fun <(i) is intern
535 redef fun >=(i) is intern
536 redef fun >(i) is intern
537 redef fun +(i) is intern
538
539 redef fun - is intern
540 redef fun -(i) is intern
541 redef fun *(i) is intern
542 redef fun /(i) is intern
543
544 # Modulo of `self` with `i`.
545 #
546 # Finds the remainder of division of `self` by `i`.
547 #
548 # assert 5 % 2 == 1
549 # assert 10 % 2 == 0
550 fun %(i: Int): Int is intern
551
552 redef fun zero do return 0
553 redef fun value_of(val) do return val.to_i
554
555 # `i` bits shift fo the left (aka <<)
556 #
557 # assert 5.lshift(1) == 10
558 fun lshift(i: Int): Int is intern
559
560 # alias of `lshift`
561 fun <<(i: Int): Int do return lshift(i)
562
563 # `i` bits shift fo the right (aka >>)
564 #
565 # assert 5.rshift(1) == 2
566 fun rshift(i: Int): Int is intern
567
568 # alias of `rshift`
569 fun >>(i: Int): Int do return rshift(i)
570
571 redef fun to_i do return self
572 redef fun to_f is intern
573 redef fun to_b is intern
574
575 redef fun distance(i)
576 do
577 var d = self - i
578 if d >= 0 then
579 return d
580 else
581 return -d
582 end
583 end
584
585 redef fun <=>(other)
586 do
587 if self < other then
588 return -1
589 else if other < self then
590 return 1
591 else
592 return 0
593 end
594 end
595
596 redef fun is_between(c, d)
597 do
598 if self < c or d < self then
599 return false
600 else
601 return true
602 end
603 end
604
605 redef fun max(other)
606 do
607 if self < other then
608 return other
609 else
610 return self
611 end
612 end
613
614 redef fun min(c)
615 do
616 if c < self then
617 return c
618 else
619 return self
620 end
621 end
622
623 # The character whose ASCII value is `self`.
624 #
625 # assert 65.ascii == 'A'
626 # assert 10.ascii == '\n'
627 fun ascii: Char is intern
628
629 # Number of digits of an integer in base `b` (plus one if negative)
630 #
631 # assert 123.digit_count(10) == 3
632 # assert 123.digit_count(2) == 7 # 1111011 in binary
633 fun digit_count(b: Int): Int
634 do
635 if b == 10 then return digit_count_base_10
636 var d: Int # number of digits
637 var n: Int # current number
638 # Sign
639 if self < 0 then
640 d = 1
641 n = - self
642 else if self == 0 then
643 return 1
644 else
645 d = 0
646 n = self
647 end
648 # count digits
649 while n > 0 do
650 d += 1
651 n = n / b # euclidian division /
652 end
653 return d
654 end
655
656 # Optimized version for base 10
657 fun digit_count_base_10: Int
658 do
659 var val: Int
660 var result: Int
661 if self < 0 then
662 result = 2
663 val = -self
664 else
665 result = 1
666 val = self
667 end
668 loop
669 if val < 10 then return result
670 if val < 100 then return result+1
671 if val < 1000 then return result+2
672 if val < 10000 then return result+3
673 val = val / 10000
674 result += 4
675 end
676 end
677
678 # Return the corresponding digit character
679 # If 0 <= `self` <= 9, return the corresponding character.
680 # assert 5.to_c == '5'
681 # If 10 <= `self` <= 36, return the corresponding letter [a..z].
682 # assert 15.to_c == 'f'
683 fun to_c: Char
684 do
685 assert self >= 0 and self <= 36 # TODO plan for this
686 if self < 10 then
687 return (self + '0'.ascii).ascii
688 else
689 return (self + ('a'.ascii - 10)).ascii
690 end
691 end
692
693 # The absolute value of self
694 #
695 # assert (-10).abs == 10
696 # assert 10.abs == 10
697 # assert 0.abs == 0
698 fun abs: Int
699 do
700 if self >= 0
701 then
702 return self
703 else
704 return -1 * self
705 end
706 end
707 end
708
709 # Native characters.
710 # Characters are denoted with simple quote.
711 # eg. `'a'` or `'\n'`.
712 universal Char
713 super Discrete
714 redef type OTHER: Char
715
716 redef fun object_id is intern
717 redef fun hash do return ascii
718 redef fun ==(o) is intern
719 redef fun !=(o) is intern
720 redef fun output is intern
721
722 redef fun <=(i) is intern
723 redef fun <(i) is intern
724 redef fun >=(i) is intern
725 redef fun >(i) is intern
726
727 redef fun successor(i) is intern
728 redef fun predecessor(i) is intern
729
730 redef fun distance(c)
731 do
732 var d = self.ascii - c.ascii
733 if d >= 0 then
734 return d
735 else
736 return -d
737 end
738 end
739
740 # If `self` is a digit then return this digit else return -1.
741 #
742 # assert '5'.to_i == 5
743 fun to_i: Int
744 do
745
746 if self == '-' then
747 return -1
748 else if is_digit then
749 return self.ascii - '0'.ascii
750 else
751 return self.to_lower.ascii - 'a'.ascii + 10
752 end
753 end
754
755 # the ascii value of self
756 #
757 # assert 'a'.ascii == 97
758 # assert '\n'.ascii == 10
759 fun ascii: Int is intern
760
761 # Return the lower case version of self.
762 # If self is not a letter, then return self
763 #
764 # assert 'A'.to_lower == 'a'
765 # assert 'a'.to_lower == 'a'
766 # assert '$'.to_lower == '$'
767 fun to_lower: Char
768 do
769 if is_upper then
770 return (ascii + ('a'.distance('A'))).ascii
771 else
772 return self
773 end
774 end
775
776 # Return the upper case version of self.
777 # If self is not a letter, then return self
778 #
779 # assert 'a'.to_upper == 'A'
780 # assert 'A'.to_upper == 'A'
781 # assert '$'.to_upper == '$'
782 fun to_upper: Char
783 do
784 if is_lower then
785 return (ascii - ('a'.distance('A'))).ascii
786 else
787 return self
788 end
789 end
790
791 # Is self a digit? (from '0' to '9')
792 #
793 # assert '0'.is_digit == true
794 # assert '9'.is_digit == true
795 # assert 'a'.is_digit == false
796 fun is_digit : Bool
797 do
798 return self >= '0' and self <= '9'
799 end
800
801 # Is self a lower case letter? (from 'a' to 'z')
802 #
803 # assert 'a'.is_lower == true
804 # assert 'z'.is_lower == true
805 # assert 'A'.is_lower == false
806 # assert '$'.is_lower == false
807 fun is_lower : Bool
808 do
809 return self >= 'a' and self <= 'z'
810 end
811
812 # Is self a upper case letter? (from 'A' to 'Z')
813 #
814 # assert 'A'.is_upper == true
815 # assert 'A'.is_upper == true
816 # assert 'z'.is_upper == false
817 # assert '$'.is_upper == false
818 fun is_upper : Bool
819 do
820 return self >= 'A' and self <= 'Z'
821 end
822
823 # Is self a letter? (from 'A' to 'Z' and 'a' to 'z')
824 #
825 # assert 'A'.is_letter == true
826 # assert 'A'.is_letter == true
827 # assert 'z'.is_letter == true
828 # assert '$'.is_letter == false
829 fun is_letter : Bool
830 do
831 return is_lower or is_upper
832 end
833
834 # Is self a whitespace character?
835 #
836 # These correspond to the "Other" and "Separator" groups of the Unicode.
837 #
838 # In the ASCII encoding, this is those <= to space (0x20) plus delete (0x7F).
839 #
840 # assert 'A'.is_whitespace == false
841 # assert ','.is_whitespace == false
842 # assert ' '.is_whitespace == true
843 # assert '\t'.is_whitespace == true
844 fun is_whitespace: Bool
845 do
846 var i = ascii
847 return i <= 0x20 or i == 0x7F
848 end
849 end
850
851 # Pointer classes are used to manipulate extern C structures.
852 extern class Pointer
853 # Is the address behind this Object at NULL?
854 fun address_is_null: Bool is extern "address_is_null"
855
856 # Free the memory pointed by this pointer
857 fun free is extern "free"
858 end