1 #!/usr/bin/env python 2 3 """ 4 Core objects. 5 6 Copyright (C) 2015, 2016, 2017 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 native import get_using 23 24 class object: 25 26 "The root class of all objects except functions." 27 28 def __init__(self): 29 30 "No-operation." 31 32 pass 33 34 def __bool__(self): 35 36 "Objects are true by default." 37 38 return True 39 40 def __str__(self): 41 42 "Return a string representation." 43 44 # The string representation of the class should be provided by the 45 # type.__str__ method using the class as self. 46 47 return str(buffer(["<", self.__class__, " instance>"])) 48 49 __repr__ = __str__ 50 51 class module: 52 53 "The class of module objects." 54 55 def __init__(self): 56 57 """ 58 Reserve special attributes for module instances. 59 """ 60 61 self.__file__ = None 62 self.__name__ = None 63 64 def __str__(self): 65 66 "Return a string representation." 67 68 return self.__name__ 69 70 __repr__ = __str__ 71 72 class function: 73 74 """ 75 The class of all function objects. 76 Note that as a special case, function does not inherit from object. 77 """ 78 79 def __init__(self): 80 81 """ 82 Reserve special attributes for function instances. 83 """ 84 85 self.__fn__ = None 86 self.__args__ = None 87 self.__name__ = None 88 self.__parent__ = None 89 self.__context__ = None 90 91 def __bool__(self): 92 93 "Functions are true by default." 94 95 return True 96 97 def __str__(self): 98 99 "Return a string representation." 100 101 # Combine the function's parent representation with the function's name. 102 103 return str(buffer([self.__parent__, ".", self.__name__])) 104 105 __repr__ = __str__ 106 107 class type: 108 109 """ 110 The class of all classes. Methods of this class do not treat contexts as 111 instances, even though classes are meant to be instances of this class. 112 Instead, contexts are either classes or instances. 113 """ 114 115 def __str__(self): 116 117 "Return a string representation." 118 119 # With the class as self, combine the class's parent representation with 120 # the class's name. 121 122 return str(buffer([self.__parent__, ".", self.__name__])) 123 124 __repr__ = __str__ 125 126 class wrapper: 127 128 "A special method wrapper." 129 130 def __init__(self, context, value): 131 132 "Initialise a wrapper with the given 'context' and wrapped 'value'." 133 134 self.__context__ = context 135 self.__value__ = value 136 137 def __str__(self): 138 139 "Return a string representation, referring to the wrapped object." 140 141 return self.__value__.__str__() 142 143 __repr__ = __str__ 144 145 class Exception: 146 147 "The root of all exception types." 148 149 pass 150 151 # Fundamental exceptions 152 153 class MemoryError(Exception): 154 155 "An error indicating failure to allocate or manage memory." 156 157 pass 158 159 class TypeError(Exception): 160 161 "An error indicating unsuitable type usage." 162 163 pass 164 165 class UnboundMethodInvocation(Exception): 166 167 "An error indicating an attempt to call an unbound method." 168 169 pass 170 171 class ArithmeticError(Exception): 172 173 "A general arithmetic operation error." 174 175 pass 176 177 class FloatingPointError(Exception): 178 179 "A floating point operation error." 180 181 pass 182 183 class OverflowError(ArithmeticError): 184 185 """ 186 Indicates that an arithmetic operation produced a result that could not be 187 represented. 188 """ 189 190 pass 191 192 class ZeroDivisionError(ArithmeticError): 193 194 "An error occurring when an attempt was made to divide an operand by zero." 195 196 pass 197 198 # vim: tabstop=4 expandtab shiftwidth=4