lib/core: Re-implemented some frequently used binops for primitive types as intern...
authorLucas Bajolet <r4pass@hotmail.com>
Tue, 8 Dec 2015 17:09:38 +0000 (12:09 -0500)
committerLucas Bajolet <r4pass@hotmail.com>
Tue, 8 Dec 2015 17:10:54 +0000 (12:10 -0500)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/core/kernel.nit
lib/core/math.nit
src/compiler/abstract_compiler.nit

index 84bd844..0a07c4b 100644 (file)
@@ -644,17 +644,23 @@ universal Byte
        # `i` bits shift fo the left
        #
        #     assert 5u8 << 1    == 10u8
-       fun <<(i: Int): Byte `{ return self << i; `}
+       fun <<(i: Int): Byte is intern do return lsh(i)
+
+       private fun lsh(i: Int): Byte `{ return self << i; `}
 
        # `i` bits shift fo the right
        #
        #     assert 5u8 >> 1    == 2u8
-       fun >>(i: Int): Byte `{ return self >> i; `}
+       fun >>(i: Int): Byte is intern do return rsh(i)
+
+       private fun rsh(i: Int): Byte `{ return self >> i; `}
 
        # Returns the character equivalent of `self`
        #
        # REQUIRE: `self <= 127u8`
-       fun ascii: Char `{ return (uint32_t)self; `}
+       fun ascii: Char is intern do return ffi_ascii
+
+       private fun ffi_ascii: Char `{ return (uint32_t)self; `}
 
        redef fun to_i is intern
        redef fun to_f is intern
@@ -743,12 +749,16 @@ universal Int
        # `i` bits shift fo the left
        #
        #     assert 5 << 1    == 10
-       fun <<(i: Int): Int `{ return self << i; `}
+       fun <<(i: Int): Int is intern do return lsh(i)
+
+       private fun lsh(i: Int): Int `{ return self << i; `}
 
        # `i` bits shift fo the right
        #
        #     assert 5 >> 1    == 2
-       fun >>(i: Int): Int `{ return self >> i; `}
+       fun >>(i: Int): Int is intern do return rsh(i)
+
+       private fun rsh(i: Int): Int `{ return self >> i; `}
 
        redef fun to_i do return self
        redef fun to_f is intern
@@ -807,7 +817,9 @@ universal Int
        #     assert 65.code_point == 'A'
        #     assert 10.code_point == '\n'
        #     assert 0x220B.code_point == '∋'
-       fun code_point: Char `{ return (uint32_t)self; `}
+       fun code_point: Char is intern do return cp
+
+       private fun cp: Char `{ return (uint32_t)self; `}
 
        # Number of digits of an integer in base `b` (plus one if negative)
        #
@@ -961,7 +973,9 @@ universal Char
        #     assert 'A'.code_point == 65
        #     assert '\n'.code_point == 10
        #     assert '∋'.code_point == 0x220B
-       fun code_point: Int `{ return (long)self; `}
+       fun code_point: Int is intern do return cp
+
+       private fun cp: Int `{ return (long)self; `}
 
        # Is `self` an ASCII character ?
        #
index c08c735..f2a3f99 100644 (file)
@@ -67,12 +67,16 @@ redef class Int
        # Returns the result of a binary AND operation on `self` and `i`
        #
        #     assert 0x10 & 0x01 == 0
-       fun &(i: Int): Int `{ return self & i; `}
+       fun &(i: Int): Int is intern do return band(i)
+
+       private fun band(i: Int): Int `{ return self & i; `}
 
        # Returns the result of a binary OR operation on `self` and `i`
        #
        #     assert 0x10 | 0x01 == 0x11
-       fun |(i: Int): Int `{ return self | i; `}
+       fun |(i: Int): Int is intern do return bor(i)
+
+       private fun bor(i: Int): Int `{ return self | i; `}
 
        # Returns the result of a binary XOR operation on `self` and `i`
        #
@@ -175,7 +179,9 @@ redef class Byte
        # Returns the result of a binary AND operation on `self` and `i`
        #
        #     assert 0x10u8 & 0x01u8 == 0u8
-       fun &(i: Byte): Byte `{ return self & i; `}
+       fun &(i: Byte): Byte is intern do return band(i)
+
+       private fun band(i: Byte): Byte `{ return self & i; `}
 
        # Returns the result of a binary OR operation on `self` and `i`
        #
index 1308e29..02fa9a6 100644 (file)
@@ -2249,6 +2249,21 @@ redef class AMethPropdef
                        else if pname == "to_b" then
                                v.ret(v.new_expr("(unsigned char){arguments[0]}", ret.as(not null)))
                                return true
+                       else if pname == "code_point" then
+                               v.ret(v.new_expr("(uint32_t){arguments[0]}", ret.as(not null)))
+                               return true
+                       else if pname == "&" then
+                               v.ret(v.new_expr("{arguments[0]} & {arguments[1]}", ret.as(not null)))
+                               return true
+                       else if pname == "|" then
+                               v.ret(v.new_expr("{arguments[0]} | {arguments[1]}", ret.as(not null)))
+                               return true
+                       else if pname == ">>" then
+                               v.ret(v.new_expr("{arguments[0]} >> {arguments[1]}", ret.as(not null)))
+                               return true
+                       else if pname == "<<" then
+                               v.ret(v.new_expr("{arguments[0]} << {arguments[1]}", ret.as(not null)))
+                               return true
                        end
                else if cname == "Char" then
                        if pname == "object_id" then
@@ -2282,6 +2297,9 @@ redef class AMethPropdef
                        else if pname == "to_i" then
                                v.ret(v.new_expr("{arguments[0]}-'0'", ret.as(not null)))
                                return true
+                       else if pname == "code_point" then
+                               v.ret(v.new_expr("(long){arguments[0]}", ret.as(not null)))
+                               return true
                        end
                else if cname == "Byte" then
                        if pname == "output" then
@@ -2330,6 +2348,15 @@ redef class AMethPropdef
                        else if pname == ">=" then
                                v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
                                return true
+                       else if pname == ">>" then
+                               v.ret(v.new_expr("{arguments[0]} >> {arguments[1]}", ret.as(not null)))
+                               return true
+                       else if pname == "<<" then
+                               v.ret(v.new_expr("{arguments[0]} << {arguments[1]}", ret.as(not null)))
+                               return true
+                       else if pname == "&" then
+                               v.ret(v.new_expr("{arguments[0]} & {arguments[1]}", ret.as(not null)))
+                               return true
                        else if pname == "to_i" then
                                v.ret(v.new_expr("(long){arguments[0]}", ret.as(not null)))
                                return true
@@ -2351,6 +2378,9 @@ redef class AMethPropdef
                        else if pname == "to_u32" then
                                v.ret(v.new_expr("(uint32_t){arguments[0]}", ret.as(not null)))
                                return true
+                       else if pname == "ascii" then
+                               v.ret(v.new_expr("(uint32_t){arguments[0]}", ret.as(not null)))
+                               return true
                        end
                else if cname == "Bool" then
                        if pname == "output" then