paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | Core objects. |
paul@6 | 5 | |
paul@850 | 6 | Copyright (C) 2015, 2016, 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@6 | 7 | |
paul@6 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@6 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@6 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@6 | 11 | version. |
paul@6 | 12 | |
paul@6 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@6 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@6 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@6 | 16 | details. |
paul@6 | 17 | |
paul@6 | 18 | You should have received a copy of the GNU General Public License along with |
paul@6 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@6 | 20 | """ |
paul@6 | 21 | |
paul@354 | 22 | from native import get_using |
paul@230 | 23 | |
paul@6 | 24 | class object: |
paul@107 | 25 | |
paul@107 | 26 | "The root class of all objects except functions." |
paul@107 | 27 | |
paul@6 | 28 | def __init__(self): |
paul@230 | 29 | |
paul@6 | 30 | "No-operation." |
paul@230 | 31 | |
paul@6 | 32 | pass |
paul@107 | 33 | |
paul@6 | 34 | def __bool__(self): |
paul@230 | 35 | |
paul@6 | 36 | "Objects are true by default." |
paul@230 | 37 | |
paul@6 | 38 | return True |
paul@6 | 39 | |
paul@230 | 40 | def __str__(self): |
paul@230 | 41 | |
paul@230 | 42 | "Return a string representation." |
paul@230 | 43 | |
paul@499 | 44 | # The string representation of the class should be provided by the |
paul@499 | 45 | # type.__str__ method using the class as self. |
paul@499 | 46 | |
paul@499 | 47 | return str(buffer(["<", self.__class__, " instance>"])) |
paul@230 | 48 | |
paul@230 | 49 | __repr__ = __str__ |
paul@230 | 50 | |
paul@269 | 51 | class module: |
paul@269 | 52 | |
paul@269 | 53 | "The class of module objects." |
paul@269 | 54 | |
paul@271 | 55 | def __init__(self): |
paul@271 | 56 | |
paul@271 | 57 | """ |
paul@271 | 58 | Reserve special attributes for module instances. |
paul@271 | 59 | """ |
paul@271 | 60 | |
paul@271 | 61 | self.__file__ = None |
paul@499 | 62 | self.__name__ = None |
paul@271 | 63 | |
paul@269 | 64 | def __str__(self): |
paul@269 | 65 | |
paul@269 | 66 | "Return a string representation." |
paul@269 | 67 | |
paul@499 | 68 | return self.__name__ |
paul@269 | 69 | |
paul@269 | 70 | __repr__ = __str__ |
paul@269 | 71 | |
paul@107 | 72 | class function: |
paul@107 | 73 | |
paul@107 | 74 | """ |
paul@107 | 75 | The class of all function objects. |
paul@107 | 76 | Note that as a special case, function does not inherit from object. |
paul@107 | 77 | """ |
paul@107 | 78 | |
paul@6 | 79 | def __init__(self): |
paul@6 | 80 | |
paul@107 | 81 | """ |
paul@107 | 82 | Reserve special attributes for function instances. |
paul@107 | 83 | """ |
paul@6 | 84 | |
paul@6 | 85 | self.__fn__ = None |
paul@6 | 86 | self.__args__ = None |
paul@499 | 87 | self.__name__ = None |
paul@499 | 88 | self.__parent__ = None |
paul@577 | 89 | self.__context__ = None |
paul@6 | 90 | |
paul@107 | 91 | def __bool__(self): |
paul@230 | 92 | |
paul@107 | 93 | "Functions are true by default." |
paul@230 | 94 | |
paul@107 | 95 | return True |
paul@107 | 96 | |
paul@230 | 97 | def __str__(self): |
paul@230 | 98 | |
paul@230 | 99 | "Return a string representation." |
paul@230 | 100 | |
paul@499 | 101 | # Combine the function's parent representation with the function's name. |
paul@499 | 102 | |
paul@499 | 103 | return str(buffer([self.__parent__, ".", self.__name__])) |
paul@230 | 104 | |
paul@230 | 105 | __repr__ = __str__ |
paul@230 | 106 | |
paul@274 | 107 | class type: |
paul@107 | 108 | |
paul@274 | 109 | """ |
paul@274 | 110 | The class of all classes. Methods of this class do not treat contexts as |
paul@274 | 111 | instances, even though classes are meant to be instances of this class. |
paul@274 | 112 | Instead, contexts are either classes or instances. |
paul@274 | 113 | """ |
paul@107 | 114 | |
paul@274 | 115 | def __str__(self): |
paul@274 | 116 | |
paul@274 | 117 | "Return a string representation." |
paul@230 | 118 | |
paul@499 | 119 | # With the class as self, combine the class's parent representation with |
paul@499 | 120 | # the class's name. |
paul@499 | 121 | |
paul@499 | 122 | return str(buffer([self.__parent__, ".", self.__name__])) |
paul@274 | 123 | |
paul@274 | 124 | __repr__ = __str__ |
paul@6 | 125 | |
paul@577 | 126 | class wrapper: |
paul@577 | 127 | |
paul@577 | 128 | "A special method wrapper." |
paul@577 | 129 | |
paul@577 | 130 | def __init__(self, context, value): |
paul@577 | 131 | |
paul@577 | 132 | "Initialise a wrapper with the given 'context' and wrapped 'value'." |
paul@577 | 133 | |
paul@577 | 134 | self.__context__ = context |
paul@577 | 135 | self.__value__ = value |
paul@577 | 136 | |
paul@581 | 137 | def __str__(self): |
paul@581 | 138 | |
paul@581 | 139 | "Return a string representation, referring to the wrapped object." |
paul@581 | 140 | |
paul@581 | 141 | return self.__value__.__str__() |
paul@581 | 142 | |
paul@581 | 143 | __repr__ = __str__ |
paul@581 | 144 | |
paul@470 | 145 | class Exception: |
paul@233 | 146 | |
paul@233 | 147 | "The root of all exception types." |
paul@233 | 148 | |
paul@233 | 149 | pass |
paul@233 | 150 | |
paul@470 | 151 | # Fundamental exceptions |
paul@470 | 152 | |
paul@470 | 153 | class MemoryError(Exception): |
paul@470 | 154 | |
paul@470 | 155 | "An error indicating failure to allocate or manage memory." |
paul@470 | 156 | |
paul@470 | 157 | pass |
paul@470 | 158 | |
paul@470 | 159 | class TypeError(Exception): |
paul@470 | 160 | |
paul@470 | 161 | "An error indicating unsuitable type usage." |
paul@470 | 162 | |
paul@470 | 163 | pass |
paul@470 | 164 | |
paul@470 | 165 | class UnboundMethodInvocation(Exception): |
paul@470 | 166 | |
paul@470 | 167 | "An error indicating an attempt to call an unbound method." |
paul@470 | 168 | |
paul@470 | 169 | pass |
paul@470 | 170 | |
paul@470 | 171 | class ArithmeticError(Exception): |
paul@470 | 172 | |
paul@470 | 173 | "A general arithmetic operation error." |
paul@470 | 174 | |
paul@470 | 175 | pass |
paul@470 | 176 | |
paul@470 | 177 | class FloatingPointError(Exception): |
paul@470 | 178 | |
paul@470 | 179 | "A floating point operation error." |
paul@470 | 180 | |
paul@470 | 181 | pass |
paul@470 | 182 | |
paul@470 | 183 | class OverflowError(ArithmeticError): |
paul@470 | 184 | |
paul@470 | 185 | """ |
paul@470 | 186 | Indicates that an arithmetic operation produced a result that could not be |
paul@470 | 187 | represented. |
paul@470 | 188 | """ |
paul@470 | 189 | |
paul@470 | 190 | pass |
paul@470 | 191 | |
paul@850 | 192 | class UnderflowError(ArithmeticError): |
paul@850 | 193 | |
paul@850 | 194 | """ |
paul@850 | 195 | Indicates that an arithmetic operation produced a result that could not be |
paul@850 | 196 | represented. |
paul@850 | 197 | """ |
paul@850 | 198 | |
paul@850 | 199 | pass |
paul@850 | 200 | |
paul@470 | 201 | class ZeroDivisionError(ArithmeticError): |
paul@470 | 202 | |
paul@470 | 203 | "An error occurring when an attempt was made to divide an operand by zero." |
paul@470 | 204 | |
paul@470 | 205 | pass |
paul@6 | 206 | |
paul@6 | 207 | # vim: tabstop=4 expandtab shiftwidth=4 |