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