nit: Added link to `CONTRIBUTING.md` from the README
[nit.git] / lib / core / 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 basic classes and methods.
15 #
16 # This module is the root of the module hierarchy.
17 # It provides a very minimal set of classes and services used as a
18 # foundation to define other classes and methods.
19 module kernel
20
21 import end # Mark this module is a top level one. (must be only one)
22
23 in "C" `{
24 #include <stdlib.h>
25 #include <errno.h>
26 `}
27
28 ###############################################################################
29 # System Classes #
30 ###############################################################################
31
32 # The root of the class hierarchy.
33 #
34 # Each other class implicitly specializes Object,
35 # therefore the services of Object are inherited by every other class and are usable
36 # on each value, including primitive types like integers (`Int`), strings (`String`) and arrays (`Array`).
37 #
38 # Note that `nullable Object`, not `Object`, is the root of the type hierarchy
39 # since the special value `null` is not considered as an instance of Object.
40 interface Object
41 # Type of this instance, automatically specialized in every class
42 #
43 # A common use case of the virtual type `SELF` is to type an attribute and
44 # store another instance of the same type as `self`. It can also be used as as
45 # return type to a method producing a copy of `self` or returning an instance
46 # expected to be the exact same type as self.
47 #
48 # This virtual type must be used with caution as it can hinder specialization.
49 # In fact, it imposes strict restrictions on all sub-classes and their usage.
50 # For example, using `SELF` as a return type of a method `foo`
51 # forces all subclasses to ensure that `foo` returns the correct and updated
52 # type.
53 # A dangerous usage take the form of a method typed by `SELF` which creates
54 # and returns a new instance.
55 # If not correctly specialized, this method would break when invoked on a
56 # sub-class.
57 #
58 # A general rule for safe usage of `SELF` is to ensure that inputs typed
59 # `SELF` are stored in attributes typed `SELF` and returned by methods typed
60 # `SELF`, pretty much the same things as you would do with parameter types.
61 type SELF: Object
62
63 # An internal hash code for the object based on its identity.
64 #
65 # Unless specific code, you should not use this method but
66 # use `hash` instead.
67 #
68 # As its name hints it, the internal hash code, is used internally
69 # to provide a hash value.
70 # It is also used by the `inspect` method to loosely identify objects
71 # and helps debugging.
72 #
73 # ~~~
74 # var a = "Hello"
75 # var b = a
76 # assert a.object_id == b.object_id
77 # ~~~
78 #
79 # The specific details of the internal hash code it let to the specific
80 # engine. The rules are the following:
81 #
82 # * The `object_id` MUST be invariant for the whole life of the object.
83 # * Two living instances of the same classes SHOULD NOT share the same `object_id`.
84 # * Two instances of different classes MIGHT share the same `object_id`.
85 # * The `object_id` of a garbage-collected instance MIGHT be reused by new instances.
86 # * The `object_id` of an object MIGHT be non constant across different executions.
87 #
88 # For instance, the `nitc` compiler uses the address of the object in memory
89 # as its `object_id`.
90 #
91 # TODO rename in something like `internal_hash_code`
92 fun object_id: Int is intern
93
94 # Return true if `self` and `other` have the same dynamic type.
95 #
96 # ~~~
97 # assert 1.is_same_type(2)
98 # assert "Hello".is_same_type("World")
99 # assert not "Hello".is_same_type(2)
100 # ~~~
101 #
102 # The method returns false if the dynamic type of `other` is a subtype of the dynamic type of `self`
103 # (or the other way around).
104 #
105 # Unless specific code, you should not use this method because it is inconsistent
106 # with the fact that a subclass can be used in lieu of a superclass.
107 fun is_same_type(other: Object): Bool is intern
108
109 # Return true if `self` and `other` are the same instance (i.e. same identity).
110 #
111 # ~~~
112 # var a = new Buffer
113 # var b = a
114 # var c = new Buffer
115 # assert a.is_same_instance(b)
116 # assert not a.is_same_instance(c)
117 # assert a == c # because both buffers are empty
118 # ~~~
119 #
120 # Obviously, the identity of an object is preserved even if the object is mutated.
121 #
122 # ~~~
123 # var x = [1]
124 # var y = x
125 # x.add 2
126 # assert x.is_same_instance(y)
127 # ~~~
128 #
129 # Unless specific code, you should use `==` instead of `is_same_instance` because
130 # most of the time is it the semantic (and user-defined) comparison that make sense.
131 #
132 # Moreover, relying on `is_same_instance` on objects you do not control
133 # might have unexpected effects when libraries reuse objects or intern them.
134 fun is_same_instance(other: nullable Object): Bool is intern
135
136 # Have `self` and `other` the same value?
137 #
138 # ~~~
139 # assert 1 + 1 == 2
140 # assert not 1 == "1"
141 # assert 1.to_s == "1"
142 # ~~~
143 #
144 # The exact meaning of *same value* is left to the subclasses.
145 # Implicitly, the default implementation, is `is_same_instance`.
146 #
147 # The laws of `==` are the following:
148 #
149 # * reflexivity `a.is_same_instance(b) implies a == b`
150 # * symmetry: `(a == b) == (b == a)`
151 # * transitivity: `(a == b) and (b == c) implies (a == c)`
152 #
153 # `==` might not be constant on some objects overtime because of their evolution.
154 #
155 # ~~~
156 # var a = [1]
157 # var b = [1]
158 # var c = [1,2]
159 # assert a == b and not a == c
160 # a.add 2
161 # assert not a == b and a == c
162 # ~~~
163 #
164 # Lastly, `==` is highly linked with `hash` and a specific redefinition of `==` should
165 # usually be associated with a specific redefinition of `hash`.
166 #
167 # ENSURE `result implies self.hash == other.hash`
168 fun ==(other: nullable Object): Bool do return self.is_same_instance(other)
169
170 # Have `self` and `other` different values?
171 #
172 # `!=` is equivalent with `not ==`.
173 fun !=(other: nullable Object): Bool do return not (self == other)
174
175 # Display self on stdout (debug only).
176 #
177 # This method MUST not be used by programs, it is here for debugging
178 # only and can be removed without any notice.
179 #
180 # TODO: rename to avoid blocking a good identifier like `output`.
181 fun output
182 do
183 '<'.output
184 object_id.output
185 '>'.output
186 end
187
188 # Display class name on stdout (debug only).
189 #
190 # This method MUST not be used by programs, it is here for debugging
191 # only and can be removed without any notice.
192 #
193 # TODO: rename to avoid blocking a good identifier like `output`.
194 fun output_class_name is intern
195
196 # The hash code of the object.
197 #
198 # The hash code is used in many data-structures and algorithms to identify objects that might be equal.
199 # Therefore, the precise semantic of `hash` is highly linked with the semantic of `==`
200 # and the only law of `hash` is that `a == b implies a.hash == b.hash`.
201 #
202 # ~~~
203 # assert (1+1).hash == 2.hash
204 # assert 1.to_s.hash == "1".hash
205 # ~~~
206 #
207 # `hash` (like `==`) might not be constant on some objects over time because of their evolution.
208 #
209 # ~~~
210 # var a = [1]
211 # var b = [1]
212 # var c = [1,2]
213 # assert a.hash == b.hash
214 # a.add 2
215 # assert a.hash == c.hash
216 # # There is a very high probability that `b.hash != c.hash`
217 # ~~~
218 #
219 # A specific redefinition of `==` should usually be associated with a specific redefinition of `hash`.
220 # Note that, unfortunately, a correct definition of `hash` that is lawful with `==` is sometime tricky
221 # and a cause of bugs.
222 #
223 # Without redefinition, `hash` is based on the `object_id` of the instance.
224 fun hash: Int do return object_id
225 end
226
227 # The main class of the program.
228 #
229 # `Sys` is a singleton class, its only instance is accessible from everywhere with `sys`.
230 #
231 # Because of this, methods that should be accessible from everywhere, like `print` or `exit`,
232 # are defined in `Sys`.
233 # Moreover, unless there is an ambiguity with `self`, the receiver of a call to these methods is implicitly `sys`.
234 # Basically it means that the two following instructions are equivalent.
235 #
236 # ~~~nit
237 # print "Hello World"
238 # sys.print "Hello World"
239 # ~~~
240 #
241 # ## Methods Implicitly Defined in Sys
242 #
243 # `Sys` is the class where are defined top-level methods,
244 # i.e. those defined outside of any class like in a procedural language.
245 # Basically it means that
246 #
247 # ~~~nitish
248 # redef class Sys
249 # fun foo do print "hello"
250 # end
251 # ~~~
252 #
253 # is equivalent with
254 #
255 # ~~~nitish
256 # fun foo print "hello"
257 # ~~~
258 #
259 # As a corollary, in a top-level method, `self` (the current receiver) is always `sys`.
260 class Sys
261 # The main method of a program.
262 #
263 # In a module, the instructions defined outside any classes or methods
264 # (usually called the *main* of the module) is
265 # an implicit definition of this `main` method.
266 # Basically it means that the following program
267 #
268 # ~~~nit
269 # print "Hello World"
270 # ~~~
271 #
272 # is equivalent with
273 #
274 # ~~~nit
275 # redef class Sys
276 # redef fun main do
277 # print "Hello World"
278 # end
279 # end
280 # ~~~
281 fun main do end
282
283 # The entry point for the execution of the whole program.
284 #
285 # When a program starts, the following implicit sequence of instructions is executed
286 #
287 # ~~~nitish
288 # sys = new Sys
289 # sys.run
290 # ~~~
291 #
292 # Whereas the job of the `run` method is just to execute `main`.
293 #
294 # The only reason of the existence of `run` is to allow modules to refine it
295 # and inject specific work before or after the main part.
296 fun run do main
297
298 # Number of the last error
299 fun errno: Int `{ return errno; `}
300 end
301
302 # Quit the program with a specific return code
303 fun exit(exit_value: Int) is intern
304
305 # Return the global sys object, the only instance of the `Sys` class.
306 fun sys: Sys is intern
307
308
309 ###############################################################################
310 # Abstract Classes #
311 ###############################################################################
312
313 # The ancestor of class where objects are in a total order.
314 # In order to work, the method '<' has to be redefined.
315 interface Comparable
316 # What `self` can be compared to?
317 type OTHER: Comparable
318
319 # Is `self` lesser than `other`?
320 fun <(other: OTHER): Bool is abstract
321
322 # not `other` < `self`
323 # Note, the implementation must ensure that: `(x<=y) == (x<y or x==y)`
324 fun <=(other: OTHER): Bool do return not other < self
325
326 # not `self` < `other`
327 # Note, the implementation must ensure that: `(x>=y) == (x>y or x==y)`
328 fun >=(other: OTHER): Bool do return not self < other
329
330 # `other` < `self`
331 fun >(other: OTHER): Bool do return other < self
332
333 # -1 if <, +1 if > and 0 otherwise
334 # Note, the implementation must ensure that: (x<=>y == 0) == (x==y)
335 fun <=>(other: OTHER): Int
336 do
337 if self < other then
338 return -1
339 else if other < self then
340 return 1
341 else
342 return 0
343 end
344 end
345
346 # c <= self <= d
347 fun is_between(c: OTHER, d: OTHER): Bool
348 do
349 return c <= self and self <= d
350 end
351
352 # The maximum between `self` and `other` (prefers `self` if equals).
353 fun max(other: OTHER): OTHER
354 do
355 if self < other then
356 return other
357 else
358 return self
359 end
360 end
361
362 # The minimum between `self` and `c` (prefer `self` if equals)
363 fun min(c: OTHER): OTHER
364 do
365 if c < self then
366 return c
367 else
368 return self
369 end
370 end
371 end
372
373 # Discrete total orders.
374 interface Discrete
375 super Comparable
376
377 redef type OTHER: Discrete
378
379 # The next element.
380 fun successor(i: Int): OTHER is abstract
381
382 # The previous element.
383 fun predecessor(i: Int): OTHER is abstract
384
385 # The distance between self and d.
386 #
387 # assert 10.distance(15) == 5
388 # assert 'Z'.distance('A') == 25
389 fun distance(d: OTHER): Int
390 do
391 var cursor: OTHER
392 var stop: OTHER
393 if self < d then
394 cursor = self
395 stop = d
396 else if self > d then
397 cursor = d
398 stop = self
399 else
400 return 0
401 end
402
403 var nb = 0
404 while cursor < stop do
405 cursor = cursor.successor(1)
406 nb += 1
407 end
408 return nb
409 end
410 end
411
412 # Something that can be cloned
413 #
414 # This interface introduces the `clone` method used to duplicate an instance
415 # Its specific semantic is left to the subclasses.
416 interface Cloneable
417 # Duplicate `self`
418 #
419 # The specific semantic of this method is left to the subclasses;
420 # Especially, if (and how) attributes are cloned (depth vs. shallow).
421 #
422 # As a rule of thumb, the principle of least astonishment should
423 # be used to guide the semantic.
424 #
425 # Note that as the returned clone depends on the semantic,
426 # the `==` method, if redefined, should ensure the equality
427 # between an object and its clone.
428 fun clone: SELF is abstract
429 end
430
431 # A numeric value supporting mathematical operations
432 interface Numeric
433 super Comparable
434
435 redef type OTHER: Numeric
436
437 # Addition of `self` with `i`
438 fun +(i: OTHER): OTHER is abstract
439
440 # Substraction of `i` from `self`
441 fun -(i: OTHER): OTHER is abstract
442
443 # Inverse of `self`
444 fun -: OTHER is abstract
445
446 # Multiplication of `self` with `i`
447 fun *(i: OTHER): OTHER is abstract
448
449 # Division of `self` with `i`
450 fun /(i: OTHER): OTHER is abstract
451
452 # The integer part of `self`.
453 #
454 # assert (0.0).to_i == 0
455 # assert (0.9).to_i == 0
456 # assert (-0.9).to_i == 0
457 # assert (9.9).to_i == 9
458 # assert (-9.9).to_i == -9
459 fun to_i: Int is abstract
460
461 # The float equivalent of `self`
462 #
463 # assert 5.to_f == 5.0
464 # assert 5.to_f != 5 # Float and Int are not equals
465 fun to_f: Float is abstract
466
467 # The byte equivalent of `self`
468 #
469 # assert (-1).to_b == 0xFF.to_b
470 # assert (1.9).to_b == 1.to_b
471 fun to_b: Byte is abstract
472
473 # Is this the value of zero in its domain?
474 fun is_zero: Bool do return self == zero
475
476 # The value of zero in the domain of `self`
477 fun zero: OTHER is abstract
478
479 # The value of `val` in the domain of `self`
480 #
481 # assert 1.0.value_of(2) == 2.0
482 # assert 1.0.value_of(2.0) == 2.0
483 # assert 1.value_of(2) == 2
484 # assert 1.value_of(2.0) == 2
485 fun value_of(val: Numeric): OTHER is abstract
486 end
487
488 ###############################################################################
489 # Native classes #
490 ###############################################################################
491
492 # Native Booleans.
493 # `true` and `false` are the only instances.
494 #
495 # Boolean are manipulated trough three special operators:
496 # `and`, `or`, `not`.
497 #
498 # Booleans are mainly used by conditional statement and loops.
499 universal Bool
500 redef fun object_id is intern
501 redef fun ==(b) is intern
502 redef fun !=(b) is intern
503 redef fun output is intern
504 redef fun hash do return to_i
505
506 # 1 if true and 0 if false
507 fun to_i: Int
508 do
509 if self then
510 return 1
511 else
512 return 0
513 end
514 end
515 end
516
517 # Native floating point numbers.
518 # Corresponds to C float.
519 universal Float
520 super Numeric
521
522 redef type OTHER: Float
523
524 redef fun object_id is intern
525 redef fun ==(i) is intern
526 redef fun !=(i) is intern
527 redef fun output is intern
528
529 redef fun <=(i) is intern
530 redef fun <(i) is intern
531 redef fun >=(i) is intern
532 redef fun >(i) is intern
533
534 redef fun +(i) is intern
535 redef fun - is intern
536 redef fun -(i) is intern
537 redef fun *(i) is intern
538 redef fun /(i) is intern
539
540 redef fun to_i is intern
541 redef fun to_f do return self
542 redef fun to_b is intern
543
544 redef fun zero do return 0.0
545 redef fun value_of(val) do return val.to_f
546
547 redef fun <=>(other)
548 do
549 if self < other then
550 return -1
551 else if other < self then
552 return 1
553 else
554 return 0
555 end
556 end
557
558 redef fun is_between(c, d)
559 do
560 if self < c or d < self then
561 return false
562 else
563 return true
564 end
565 end
566
567 # Compare float numbers with a given precision.
568 #
569 # Because of the loss of precision in floating numbers,
570 # the `==` method is often not the best way to compare them.
571 #
572 # ~~~
573 # assert 0.01.is_approx(0.02, 0.1) == true
574 # assert 0.01.is_approx(0.02, 0.001) == false
575 # ~~~
576 fun is_approx(other, precision: Float): Bool
577 do
578 assert precision >= 0.0
579 return self <= other + precision and self >= other - precision
580 end
581
582 redef fun max(other)
583 do
584 if self < other then
585 return other
586 else
587 return self
588 end
589 end
590
591 redef fun min(c)
592 do
593 if c < self then
594 return c
595 else
596 return self
597 end
598 end
599 end
600
601 # Native bytes.
602 # Same as a C `unsigned char`
603 universal Byte
604 super Discrete
605 super Numeric
606
607 redef type OTHER: Byte
608
609 redef fun successor(i) do return self + i.to_b
610 redef fun predecessor(i) do return self - i.to_b
611
612 redef fun object_id is intern
613 redef fun hash do return self.to_i
614 redef fun ==(i) is intern
615 redef fun !=(i) is intern
616 redef fun output is intern
617
618 redef fun <=(i) is intern
619 redef fun <(i) is intern
620 redef fun >=(i) is intern
621 redef fun >(i) is intern
622 redef fun +(i) is intern
623
624 # On an Byte, unary minus will return `(256 - self) % 256`
625 #
626 # assert -1u8 == 0xFFu8
627 # assert -0u8 == 0x00u8
628 redef fun - is intern
629 redef fun -(i) is intern
630 redef fun *(i) is intern
631 redef fun /(i) is intern
632
633 # Modulo of `self` with `i`.
634 #
635 # Finds the remainder of division of `self` by `i`.
636 #
637 # assert 5u8 % 2u8 == 1u8
638 # assert 10u8 % 2u8 == 0u8
639 fun %(i: Byte): Byte is intern
640
641 redef fun zero do return 0.to_b
642 redef fun value_of(val) do return val.to_b
643
644 # `i` bits shift fo the left
645 #
646 # assert 5u8 << 1 == 10u8
647 fun <<(i: Int): Byte is intern `{ return self << i; `}
648
649 # `i` bits shift fo the right
650 #
651 # assert 5u8 >> 1 == 2u8
652 fun >>(i: Int): Byte is intern `{ return self >> i; `}
653
654 # Returns the character equivalent of `self`
655 #
656 # REQUIRE: `self <= 127u8`
657 fun ascii: Char is intern `{ return (uint32_t)self; `}
658
659 redef fun to_i is intern
660 redef fun to_f is intern
661 redef fun to_b do return self
662
663 redef fun distance(i) do return (self - i).to_i
664
665 redef fun <=>(other)
666 do
667 if self < other then
668 return -1
669 else if other < self then
670 return 1
671 else
672 return 0
673 end
674 end
675
676 redef fun is_between(c, d)
677 do
678 if self < c or d < self then
679 return false
680 else
681 return true
682 end
683 end
684
685 redef fun max(other)
686 do
687 if self < other then
688 return other
689 else
690 return self
691 end
692 end
693
694 redef fun min(c)
695 do
696 if c < self then
697 return c
698 else
699 return self
700 end
701 end
702 end
703
704 # Native integer numbers.
705 # Correspond to C int.
706 universal Int
707 super Discrete
708 super Numeric
709
710 redef type OTHER: Int
711
712 redef fun successor(i) do return self + i
713 redef fun predecessor(i) do return self - i
714
715 redef fun object_id is intern
716 redef fun hash do return self
717 redef fun ==(i) is intern
718 redef fun !=(i) is intern
719 redef fun output is intern
720
721 redef fun <=(i) is intern
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 - is intern
728 redef fun -(i) is intern
729 redef fun *(i) is intern
730 redef fun /(i) is intern
731
732 # Modulo of `self` with `i`.
733 #
734 # Finds the remainder of division of `self` by `i`.
735 #
736 # assert 5 % 2 == 1
737 # assert 10 % 2 == 0
738 fun %(i: Int): Int is intern
739
740 redef fun zero do return 0
741 redef fun value_of(val) do return val.to_i
742
743 # `i` bits shift fo the left
744 #
745 # assert 5 << 1 == 10
746 fun <<(i: Int): Int is intern `{ return self << i; `}
747
748 # `i` bits shift fo the right
749 #
750 # assert 5 >> 1 == 2
751 fun >>(i: Int): Int is intern `{ return self >> i; `}
752
753 redef fun to_i do return self
754 redef fun to_f is intern
755 redef fun to_b is intern
756
757 redef fun distance(i)
758 do
759 var d = self - i
760 if d >= 0 then
761 return d
762 else
763 return -d
764 end
765 end
766
767 redef fun <=>(other)
768 do
769 if self < other then
770 return -1
771 else if other < self then
772 return 1
773 else
774 return 0
775 end
776 end
777
778 redef fun is_between(c, d)
779 do
780 if self < c or d < self then
781 return false
782 else
783 return true
784 end
785 end
786
787 redef fun max(other)
788 do
789 if self < other then
790 return other
791 else
792 return self
793 end
794 end
795
796 redef fun min(c)
797 do
798 if c < self then
799 return c
800 else
801 return self
802 end
803 end
804
805 # The character which code point (unicode-wise) is `self`
806 #
807 # assert 65.code_point == 'A'
808 # assert 10.code_point == '\n'
809 # assert 0x220B.code_point == '∋'
810 fun code_point: Char is intern `{ return (uint32_t)self; `}
811
812 # Number of digits of an integer in base `b` (plus one if negative)
813 #
814 # assert 123.digit_count(10) == 3
815 # assert 123.digit_count(2) == 7 # 1111011 in binary
816 fun digit_count(b: Int): Int
817 do
818 if b == 10 then return digit_count_base_10
819 var d: Int # number of digits
820 var n: Int # current number
821 # Sign
822 if self < 0 then
823 d = 1
824 n = - self
825 else if self == 0 then
826 return 1
827 else
828 d = 0
829 n = self
830 end
831 # count digits
832 while n > 0 do
833 d += 1
834 n = n / b # euclidian division /
835 end
836 return d
837 end
838
839 # Optimized version for base 10
840 fun digit_count_base_10: Int
841 do
842 var val: Int
843 var result: Int
844 if self < 0 then
845 result = 2
846 val = -self
847 else
848 result = 1
849 val = self
850 end
851 loop
852 if val < 10 then return result
853 if val < 100 then return result+1
854 if val < 1000 then return result+2
855 if val < 10000 then return result+3
856 val = val / 10000
857 result += 4
858 end
859 end
860
861 # Return the corresponding digit character
862 # If 0 <= `self` <= 9, return the corresponding character.
863 # assert 5.to_c == '5'
864 # If 10 <= `self` <= 36, return the corresponding letter [a..z].
865 # assert 15.to_c == 'f'
866 fun to_c: Char
867 do
868 assert self >= 0 and self <= 36 # TODO plan for this
869 if self < 10 then
870 return (self + '0'.code_point).code_point
871 else
872 return (self - 10 + 'a'.code_point).code_point
873 end
874 end
875
876 # The absolute value of self
877 #
878 # assert (-10).abs == 10
879 # assert 10.abs == 10
880 # assert 0.abs == 0
881 fun abs: Int do return if self >= 0 then self else -self
882 end
883
884 # Native characters.
885 # Characters are denoted with simple quote.
886 # eg. `'a'` or `'\n'`.
887 universal Char
888 super Discrete
889 redef type OTHER: Char
890
891 redef fun object_id is intern
892 redef fun output `{
893 if(self < 128){
894 printf("%c", self);
895 }else if(self < 2048){
896 printf("%c%c", 0xC0 | ((0x7C0 & self) >> 6), 0x80 | (0x3F & self));
897 }else if(self < 65536){
898 printf("%c%c%c", 0xE0 | ((0xF000 & self) >> 12), 0x80 | ((0xFC0 & self) >> 6) ,0x80 | (0x3F & self));
899 }else if(self < 2097152){
900 printf("%c%c%c%c", 0xF0 | ((0x1C0000 & self) >> 18), 0x80 | ((0x3F000 & self) >> 12), 0x80 | ((0xFC0 & self) >> 6), 0x80 | (0x3F & self));
901 }else{
902 // Bad char
903 printf("%c", self);
904 }
905 `}
906 redef fun hash do return code_point
907 redef fun ==(o) is intern
908 redef fun !=(o) is intern
909
910 redef fun <=(i) is intern
911 redef fun <(i) is intern
912 redef fun >=(i) is intern
913 redef fun >(i) is intern
914
915 redef fun successor(i) is intern
916 redef fun predecessor(i) is intern
917
918 redef fun distance(c)
919 do
920 var d = self.code_point - c.code_point
921 if d >= 0 then
922 return d
923 else
924 return -d
925 end
926 end
927
928 # If `self` is a digit then return this digit else return -1.
929 #
930 # assert '5'.to_i == 5
931 fun to_i: Int
932 do
933
934 if self == '-' then
935 return -1
936 else if is_digit then
937 return self.code_point - '0'.code_point
938 else
939 return self.to_lower.code_point - 'a'.code_point + 10
940 end
941 end
942
943 # The ascii value of `self`
944 #
945 # assert 'a'.ascii == 97u8
946 # assert '\n'.ascii == 10u8
947 #
948 # REQUIRE: `is_ascii`
949 fun ascii: Byte do return code_point.to_b
950
951 # The unicode code point value of `self`
952 #
953 # assert 'A'.code_point == 65
954 # assert '\n'.code_point == 10
955 # assert '∋'.code_point == 0x220B
956 fun code_point: Int is intern `{ return (long)self; `}
957
958 # Is `self` an ASCII character ?
959 #
960 # assert 'x'.is_ascii
961 # assert not 'ま'.is_ascii
962 fun is_ascii: Bool do return code_point <= 127
963
964 # Return the lower case version of self.
965 # If self is not a letter, then return self
966 #
967 # assert 'A'.to_lower == 'a'
968 # assert 'a'.to_lower == 'a'
969 # assert '$'.to_lower == '$'
970 fun to_lower: Char
971 do
972 if is_upper then
973 return (code_point + ('a'.distance('A'))).code_point
974 else
975 return self
976 end
977 end
978
979 # Return the upper case version of self.
980 # If self is not a letter, then return self
981 #
982 # assert 'a'.to_upper == 'A'
983 # assert 'A'.to_upper == 'A'
984 # assert '$'.to_upper == '$'
985 fun to_upper: Char
986 do
987 if is_lower then
988 return (code_point - ('a'.distance('A'))).code_point
989 else
990 return self
991 end
992 end
993
994 # Is self a digit? (from '0' to '9')
995 #
996 # assert '0'.is_digit == true
997 # assert '9'.is_digit == true
998 # assert 'a'.is_digit == false
999 fun is_digit : Bool
1000 do
1001 return self >= '0' and self <= '9'
1002 end
1003
1004 # Is self a lower case letter? (from 'a' to 'z')
1005 #
1006 # assert 'a'.is_lower == true
1007 # assert 'z'.is_lower == true
1008 # assert 'A'.is_lower == false
1009 # assert '$'.is_lower == false
1010 fun is_lower : Bool
1011 do
1012 return self >= 'a' and self <= 'z'
1013 end
1014
1015 # Is self a upper case letter? (from 'A' to 'Z')
1016 #
1017 # assert 'A'.is_upper == true
1018 # assert 'A'.is_upper == true
1019 # assert 'z'.is_upper == false
1020 # assert '$'.is_upper == false
1021 fun is_upper : Bool
1022 do
1023 return self >= 'A' and self <= 'Z'
1024 end
1025
1026 # Is self a letter? (from 'A' to 'Z' and 'a' to 'z')
1027 #
1028 # assert 'A'.is_letter == true
1029 # assert 'A'.is_letter == true
1030 # assert 'z'.is_letter == true
1031 # assert '$'.is_letter == false
1032 fun is_letter : Bool
1033 do
1034 return is_lower or is_upper
1035 end
1036
1037 # Is self a whitespace character?
1038 #
1039 # These correspond to the "Other" and "Separator" groups of the Unicode.
1040 #
1041 # In the ASCII encoding, this is those <= to space (0x20) plus delete (0x7F).
1042 #
1043 # assert 'A'.is_whitespace == false
1044 # assert ','.is_whitespace == false
1045 # assert ' '.is_whitespace == true
1046 # assert '\t'.is_whitespace == true
1047 fun is_whitespace: Bool
1048 do
1049 var i = code_point
1050 return i <= 0x20 or i == 0x7F
1051 end
1052 end
1053
1054 # Pointer classes are used to manipulate extern C structures.
1055 extern class Pointer
1056 # Is the address behind this Object at NULL?
1057 fun address_is_null: Bool `{ return self == NULL; `}
1058
1059 # Free the memory pointed by this pointer
1060 fun free `{ free(self); `}
1061 end
1062
1063 # Task with a `main` method to be implemented by subclasses
1064 #
1065 # This class is provided for compatibility between different parallelization systems.
1066 # It can be used to run a fragment of code on a different thread and
1067 # to register a reaction to UI events.
1068 interface Task
1069
1070 # Main method of this task
1071 fun main do end
1072 end