Merge: Added contributing guidelines and link from 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
703 # Is `self` an ASCII whitespace ?
704 fun is_whitespace: Bool do return self == 0x7Fu8 or self <= 0x20u8
705 end
706
707 # Native integer numbers.
708 # Correspond to C int.
709 universal Int
710 super Discrete
711 super Numeric
712
713 redef type OTHER: Int
714
715 redef fun successor(i) do return self + i
716 redef fun predecessor(i) do return self - i
717
718 redef fun object_id is intern
719 redef fun hash do return self
720 redef fun ==(i) is intern
721 redef fun !=(i) is intern
722 redef fun output is intern
723
724 redef fun <=(i) is intern
725 redef fun <(i) is intern
726 redef fun >=(i) is intern
727 redef fun >(i) is intern
728 redef fun +(i) is intern
729
730 redef fun - is intern
731 redef fun -(i) is intern
732 redef fun *(i) is intern
733 redef fun /(i) is intern
734
735 # Modulo of `self` with `i`.
736 #
737 # Finds the remainder of division of `self` by `i`.
738 #
739 # assert 5 % 2 == 1
740 # assert 10 % 2 == 0
741 fun %(i: Int): Int is intern
742
743 redef fun zero do return 0
744 redef fun value_of(val) do return val.to_i
745
746 # `i` bits shift fo the left
747 #
748 # assert 5 << 1 == 10
749 fun <<(i: Int): Int is intern `{ return self << i; `}
750
751 # `i` bits shift fo the right
752 #
753 # assert 5 >> 1 == 2
754 fun >>(i: Int): Int is intern `{ return self >> i; `}
755
756 redef fun to_i do return self
757 redef fun to_f is intern
758 redef fun to_b is intern
759
760 redef fun distance(i)
761 do
762 var d = self - i
763 if d >= 0 then
764 return d
765 else
766 return -d
767 end
768 end
769
770 redef fun <=>(other)
771 do
772 if self < other then
773 return -1
774 else if other < self then
775 return 1
776 else
777 return 0
778 end
779 end
780
781 redef fun is_between(c, d)
782 do
783 if self < c or d < self then
784 return false
785 else
786 return true
787 end
788 end
789
790 redef fun max(other)
791 do
792 if self < other then
793 return other
794 else
795 return self
796 end
797 end
798
799 redef fun min(c)
800 do
801 if c < self then
802 return c
803 else
804 return self
805 end
806 end
807
808 # The character which code point (unicode-wise) is `self`
809 #
810 # assert 65.code_point == 'A'
811 # assert 10.code_point == '\n'
812 # assert 0x220B.code_point == '∋'
813 fun code_point: Char is intern `{ return (uint32_t)self; `}
814
815 # Number of digits of an integer in base `b` (plus one if negative)
816 #
817 # assert 123.digit_count(10) == 3
818 # assert 123.digit_count(2) == 7 # 1111011 in binary
819 fun digit_count(b: Int): Int
820 do
821 if b == 10 then return digit_count_base_10
822 var d: Int # number of digits
823 var n: Int # current number
824 # Sign
825 if self < 0 then
826 d = 1
827 n = - self
828 else if self == 0 then
829 return 1
830 else
831 d = 0
832 n = self
833 end
834 # count digits
835 while n > 0 do
836 d += 1
837 n = n / b # euclidian division /
838 end
839 return d
840 end
841
842 # Optimized version for base 10
843 fun digit_count_base_10: Int
844 do
845 var val: Int
846 var result: Int
847 if self < 0 then
848 result = 2
849 val = -self
850 else
851 result = 1
852 val = self
853 end
854 loop
855 if val < 10 then return result
856 if val < 100 then return result+1
857 if val < 1000 then return result+2
858 if val < 10000 then return result+3
859 val = val / 10000
860 result += 4
861 end
862 end
863
864 # Return the corresponding digit character
865 # If 0 <= `self` <= 9, return the corresponding character.
866 # assert 5.to_c == '5'
867 # If 10 <= `self` <= 36, return the corresponding letter [a..z].
868 # assert 15.to_c == 'f'
869 fun to_c: Char
870 do
871 assert self >= 0 and self <= 36 # TODO plan for this
872 if self < 10 then
873 return (self + '0'.code_point).code_point
874 else
875 return (self - 10 + 'a'.code_point).code_point
876 end
877 end
878
879 # The absolute value of self
880 #
881 # assert (-10).abs == 10
882 # assert 10.abs == 10
883 # assert 0.abs == 0
884 fun abs: Int do return if self >= 0 then self else -self
885 end
886
887 # Native characters.
888 # Characters are denoted with simple quote.
889 # eg. `'a'` or `'\n'`.
890 universal Char
891 super Discrete
892 redef type OTHER: Char
893
894 redef fun object_id is intern
895 redef fun output `{
896 if(self < 128){
897 printf("%c", self);
898 }else if(self < 2048){
899 printf("%c%c", 0xC0 | ((0x7C0 & self) >> 6), 0x80 | (0x3F & self));
900 }else if(self < 65536){
901 printf("%c%c%c", 0xE0 | ((0xF000 & self) >> 12), 0x80 | ((0xFC0 & self) >> 6) ,0x80 | (0x3F & self));
902 }else if(self < 2097152){
903 printf("%c%c%c%c", 0xF0 | ((0x1C0000 & self) >> 18), 0x80 | ((0x3F000 & self) >> 12), 0x80 | ((0xFC0 & self) >> 6), 0x80 | (0x3F & self));
904 }else{
905 // Bad char
906 printf("%c", self);
907 }
908 `}
909 redef fun hash do return code_point
910 redef fun ==(o) is intern
911 redef fun !=(o) is intern
912
913 redef fun <=(i) is intern
914 redef fun <(i) is intern
915 redef fun >=(i) is intern
916 redef fun >(i) is intern
917
918 redef fun successor(i) is intern
919 redef fun predecessor(i) is intern
920
921 redef fun distance(c)
922 do
923 var d = self.code_point - c.code_point
924 if d >= 0 then
925 return d
926 else
927 return -d
928 end
929 end
930
931 # If `self` is a digit then return this digit else return -1.
932 #
933 # assert '5'.to_i == 5
934 fun to_i: Int
935 do
936
937 if self == '-' then
938 return -1
939 else if is_digit then
940 return self.code_point - '0'.code_point
941 else
942 return self.to_lower.code_point - 'a'.code_point + 10
943 end
944 end
945
946 # The ascii value of `self`
947 #
948 # assert 'a'.ascii == 97u8
949 # assert '\n'.ascii == 10u8
950 #
951 # REQUIRE: `is_ascii`
952 fun ascii: Byte do return code_point.to_b
953
954 # The unicode code point value of `self`
955 #
956 # assert 'A'.code_point == 65
957 # assert '\n'.code_point == 10
958 # assert '∋'.code_point == 0x220B
959 fun code_point: Int is intern `{ return (long)self; `}
960
961 # Is `self` an ASCII character ?
962 #
963 # assert 'x'.is_ascii
964 # assert not 'ま'.is_ascii
965 fun is_ascii: Bool do return code_point <= 127
966
967 # Return the lower case version of self.
968 # If self is not a letter, then return self
969 #
970 # assert 'A'.to_lower == 'a'
971 # assert 'a'.to_lower == 'a'
972 # assert '$'.to_lower == '$'
973 fun to_lower: Char
974 do
975 if is_upper then
976 return (code_point + ('a'.distance('A'))).code_point
977 else
978 return self
979 end
980 end
981
982 # Return the upper case version of self.
983 # If self is not a letter, then return self
984 #
985 # assert 'a'.to_upper == 'A'
986 # assert 'A'.to_upper == 'A'
987 # assert '$'.to_upper == '$'
988 fun to_upper: Char
989 do
990 if is_lower then
991 return (code_point - ('a'.distance('A'))).code_point
992 else
993 return self
994 end
995 end
996
997 # Is self a digit? (from '0' to '9')
998 #
999 # assert '0'.is_digit == true
1000 # assert '9'.is_digit == true
1001 # assert 'a'.is_digit == false
1002 fun is_digit : Bool
1003 do
1004 return self >= '0' and self <= '9'
1005 end
1006
1007 # Is self a lower case letter? (from 'a' to 'z')
1008 #
1009 # assert 'a'.is_lower == true
1010 # assert 'z'.is_lower == true
1011 # assert 'A'.is_lower == false
1012 # assert '$'.is_lower == false
1013 fun is_lower : Bool
1014 do
1015 return self >= 'a' and self <= 'z'
1016 end
1017
1018 # Is self a upper case letter? (from 'A' to 'Z')
1019 #
1020 # assert 'A'.is_upper == true
1021 # assert 'A'.is_upper == true
1022 # assert 'z'.is_upper == false
1023 # assert '$'.is_upper == false
1024 fun is_upper : Bool
1025 do
1026 return self >= 'A' and self <= 'Z'
1027 end
1028
1029 # Is self a letter? (from 'A' to 'Z' and 'a' to 'z')
1030 #
1031 # assert 'A'.is_letter == true
1032 # assert 'A'.is_letter == true
1033 # assert 'z'.is_letter == true
1034 # assert '$'.is_letter == false
1035 fun is_letter : Bool
1036 do
1037 return is_lower or is_upper
1038 end
1039
1040 # Is self a whitespace character?
1041 #
1042 # These correspond to the "Other" and "Separator" groups of the Unicode.
1043 #
1044 # In the ASCII encoding, this is those <= to space (0x20) plus delete (0x7F).
1045 #
1046 # assert 'A'.is_whitespace == false
1047 # assert ','.is_whitespace == false
1048 # assert ' '.is_whitespace == true
1049 # assert '\t'.is_whitespace == true
1050 fun is_whitespace: Bool
1051 do
1052 var i = code_point
1053 return i <= 0x20 or i == 0x7F
1054 end
1055 end
1056
1057 # Pointer classes are used to manipulate extern C structures.
1058 extern class Pointer
1059 # Is the address behind this Object at NULL?
1060 fun address_is_null: Bool `{ return self == NULL; `}
1061
1062 # Free the memory pointed by this pointer
1063 fun free `{ free(self); `}
1064 end
1065
1066 # Task with a `main` method to be implemented by subclasses
1067 #
1068 # This class is provided for compatibility between different parallelization systems.
1069 # It can be used to run a fragment of code on a different thread and
1070 # to register a reaction to UI events.
1071 interface Task
1072
1073 # Main method of this task
1074 fun main do end
1075 end