1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2011-2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
20 init(value
: Int) do self.value
= value
22 fun +(other
: A
): A
import value
, A
`{
23 int s = A_value( self );
24 int o = A_value( other );
26 return new_A( s + o );
29 fun +: A
import value
, A
`{
30 int s = A_value(self);
34 fun -(other
: A
): A
import value
, A
`{
35 int s = A_value( self );
36 int o = A_value( other );
38 return new_A( s - o );
41 fun -: A
import value
, A
`{
42 int s = A_value(self);
46 fun *(by
: Int): A
import value
, A
`{
47 int s = A_value( self );
49 return new_A( s * by );
52 fun /(by
: Int): A
import value
, A
`{
53 int s = A_value( self );
55 return new_A( s / by );
58 redef fun ==(other
) import value
, nullable Object.as(A
) `{
59 if ( nullable_Object_is_a_A( other ) &&
60 A_value( nullable_Object_as_A(other) ) == A_value( self ) )
66 fun %(other
: A
): A
import value
, A
`{
67 return new_A( A_value( self ) % A_value( other ) );
70 fun <=>(other
: A
): A
import value
, A
`{
71 return new_A( A_value( self )* 1024 );
74 fun >(other
: A
): Bool import value
`{
75 return A_value( self ) > A_value( other );
78 fun <(other
: A
): Bool import value
`{
79 return A_value( self ) < A_value( other );
82 fun >=(other
: A
): Bool import value
`{
83 return A_value( self ) >= A_value( other );
86 fun <=(other
: A
): Bool import value
`{
87 return A_value( self ) <= A_value( other );
90 fun >>(other
: A
): A
import value
, value
=, A
`{
91 int new_val = A_value( self ) >> A_value( other );
92 return new_A(new_val);
95 fun <<(other
: A
): A
import value
, A
`{
96 int new_val = A_value( self ) << A_value( other );
97 return new_A(new_val);
100 fun |(other
: A
): A
import value
, A
`{
101 int new_val = A_value( self ) | A_value( other );
102 return new_A(new_val);
105 fun ^
(other
: A
): A
import value
, A
`{
106 int new_val = A_value( self ) ^ A_value( other );
107 return new_A(new_val);
110 fun ~
: A
import value
, A
`{
111 int new_val = ~A_value( self );
112 return new_A(new_val);
115 fun &(other
: A
): A
import value
, A
`{
116 int new_val = A_value( self ) & A_value( other );
117 return new_A(new_val);
120 fun [](index
: Int): A
import A
`{
121 return new_A( index );
124 fun []=(index
: Int, value
: A
): A
import A
`{
125 return new_A( index + A_value( value ) );
128 redef fun to_s
do return value
.to_s
131 print
new A
(1) + new A
(10) # 11
133 print
new A
(10) - new A
(1) # 9
136 print
new A
(2) * 11 # 22
137 print
new A
(33) / 11 # 3
139 print
new A
(44) == null # false
140 print
new A
(55) == 55 # false
141 print
new A
(33) == new A
(11) # false
142 print
new A
(22) == new A
(77) # false
143 print
new A
(11) == new A
(11) # true
145 print
new A
(147) % new A
(12) # 3
146 print
new A
(4) <=> new A
(123) # 4096
148 print
new A
(1) < new A
(100) # true
149 print
new A
(100) < new A
(100) # false
150 print
new A
(100) < new A
(1) # false
152 print
new A
(1) > new A
(100) # false
153 print
new A
(100) > new A
(100) # false
154 print
new A
(100) > new A
(1) # true
156 print
new A
(1) <= new A
(100) # true
157 print
new A
(100) <= new A
(100) # true
158 print
new A
(100) <= new A
(1) # false
160 print
new A
(1) >= new A
(100) # false
161 print
new A
(100) >= new A
(100) # true
162 print
new A
(100) >= new A
(1) # true
164 print
new A
(1) << new A
(5) # 32
165 print
new A
(32) >> new A
(2) # 8
167 print
new A
(3) | new A
(6) # 7
168 print
new A
(3) ^
new A
(6) # 5
169 print
new A
(3) & new A
(6) # 2