Lichen

lib/__builtins__/int.py

929:bbcea70e1cde
10 months ago Paul Boddie Fixed int instantiation by special-casing the type in the translator. This also avoids allocating an instance which would never be used with the tagged value representation.
     1 #!/usr/bin/env python     2      3 """     4 Integer objects.     5      6 Copyright (C) 2015, 2016, 2017, 2018, 2021 Paul Boddie <paul@boddie.org.uk>     7      8 This program is free software; you can redistribute it and/or modify it under     9 the terms of the GNU General Public License as published by the Free Software    10 Foundation; either version 3 of the License, or (at your option) any later    11 version.    12     13 This program is distributed in the hope that it will be useful, but WITHOUT    14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    15 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more    16 details.    17     18 You should have received a copy of the GNU General Public License along with    19 this program.  If not, see <http://www.gnu.org/licenses/>.    20 """    21     22 from __builtins__.unicode import utf8string    23 from native import get_maxint, get_minint, is_int, \    24                    int_add, int_and, int_div, int_eq, int_ge, int_gt, \    25                    int_lshift, int_le, int_lt, int_mod, int_mul, int_ne, \    26                    int_neg, int_not, int_or, int_pow, int_rshift, int_str, \    27                    int_sub, int_xor    28     29 class int:    30     31     "An integer abstraction."    32     33     def __init__(self, number_or_string=None):    34     35         "Initialise the integer with the given 'number_or_string'."    36     37         # Implemented in the translator.    38     39         pass    40     41     def __hash__(self):    42     43         "Return a value for hashing purposes."    44     45         return self    46     47     def _binary_op(self, op, other):    48     49         "Perform 'op' on this int and 'other' if appropriate."    50     51         if is_int(other):    52             return op(self, other)    53         else:    54             return NotImplemented    55     56     def _binary_op_rev(self, op, other):    57     58         "Perform 'op' on 'other' and this int if appropriate."    59     60         if is_int(other):    61             return op(other, self)    62         else:    63             return NotImplemented    64     65     def __iadd__(self, other):    66     67         "Return a new int for the addition of this int and 'other'."    68     69         return self._binary_op(int_add, other)    70     71     def __isub__(self, other):    72     73         "Return a new int for the subtraction of this int and 'other'."    74     75         return self._binary_op(int_sub, other)    76     77     def __imul__(self, other):    78     79         "Return a new int for the multiplication of this int and 'other'."    80     81         return self._binary_op(int_mul, other)    82     83     def __idiv__(self, other):    84     85         "Return a new int for the division of this int and 'other'."    86     87         return self._binary_op(int_div, other)    88     89     def __imod__(self, other):    90     91         "Return a new int for the modulo of this int by 'other'."    92     93         return self._binary_op(int_mod, other)    94     95     def __ipow__(self, other):    96     97         "Return a new int for the exponentiation of this int by 'other'."    98     99         return self._binary_op(int_pow, other)   100    101     def __iand__(self, other):   102    103         "Return a new int for the binary-and of this int and 'other'."   104    105         return self._binary_op(int_and, other)   106    107     def __ior__(self, other):   108    109         "Return a new int for the binary-or of this int and 'other'."   110    111         return self._binary_op(int_or, other)   112    113     def __ixor__(self, other):   114    115         "Return a new int for the exclusive-or of this int and 'other'."   116    117         return self._binary_op(int_xor, other)   118    119     def __invert__(self):   120    121         "Return the inversion of this int."   122    123         return int_not(self)   124    125     __add__ = __radd__ = __iadd__   126     __sub__ = __isub__   127    128     def __rsub__(self, other):   129    130         "Return a new int for the subtraction of this int from 'other'."   131    132         return self._binary_op_rev(int_sub, other)   133    134     __mul__ = __rmul__ = __imul__   135     __div__ = __idiv__   136    137     def __rdiv__(self, other):   138    139         "Return a new int for the division of this int into 'other'."   140    141         return self._binary_op_rev(int_div, other)   142    143     # NOTE: To be implemented.   144    145     def __floordiv__(self, other): pass   146     def __rfloordiv__(self, other): pass   147     def __ifloordiv__(self, other): pass   148    149     __mod__ = __imod__   150    151     def __rmod__(self, other):   152    153         "Return a new int for the modulo of 'other' by this int."   154    155         return self._binary_op_rev(int_mod, other)   156    157     __pow__ = __ipow__   158    159     def __rpow__(self, other):   160    161         "Return a new int for the exponentiation of 'other' by this int."   162    163         return self._binary_op_rev(int_pow, other)   164    165     __and__ = __rand__ = __iand__   166     __or__ = __ror__ = __ior__   167     __xor__ = __rxor__ = __ixor__   168    169     def __lshift__(self, other):   170    171         "Return a new int left-shifted by 'other'."   172    173         return self._binary_op(int_lshift, other)   174    175     def __rlshift__(self, other):   176    177         "Return a new int left-shifted by 'other'."   178    179         return self._binary_op_rev(int_lshift, other)   180    181     def __rshift__(self, other):   182    183         "Return a new int right-shifted by 'other'."   184    185         return self._binary_op(int_rshift, other)   186    187     def __rrshift__(self, other):   188    189         "Return a new int right-shifted by 'other'."   190    191         return self._binary_op_rev(int_rshift, other)   192    193     __ilshift__ = __lshift__   194     __irshift__ = __rshift__   195    196     def __lt__(self, other):   197    198         "Return whether this int is less than 'other'."   199    200         return self._binary_op(int_lt, other)   201    202     def __gt__(self, other):   203    204         "Return whether this int is greater than 'other'."   205    206         return self._binary_op(int_gt, other)   207    208     def __le__(self, other):   209    210         "Return whether this int is less than or equal to 'other'."   211    212         return self._binary_op(int_le, other)   213    214     def __ge__(self, other):   215    216         "Return whether this int is greater than or equal to 'other'."   217    218         return self._binary_op(int_ge, other)   219    220     def __eq__(self, other):   221    222         "Return whether this int is equal to 'other'."   223    224         return self._binary_op(int_eq, other)   225    226     def __ne__(self, other):   227    228         "Return whether this int is not equal to 'other'."   229    230         return self._binary_op(int_ne, other)   231    232     def __neg__(self):   233    234         "Apply the unary negation operator."   235    236         return int_neg(self)   237    238     def __pos__(self):   239    240         "Apply the unary positive operator."   241    242         return self   243    244     def __str__(self):   245    246         "Return a string representation."   247    248         return utf8string(int_str(self))   249    250     __repr__ = __str__   251    252     def __bool__(self):   253    254         "Return whether this int is non-zero."   255    256         return int_ne(self, 0)   257    258 # Limits.   259    260 maxint = get_maxint()   261 minint = get_minint()   262    263 # vim: tabstop=4 expandtab shiftwidth=4