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 90 def __bool__(self): 91 92 "Functions are true by default." 93 94 return True 95 96 def __str__(self): 97 98 "Return a string representation." 99 100 # Combine the function's parent representation with the function's name. 101 102 return str(buffer([self.__parent__, ".", self.__name__])) 103 104 __repr__ = __str__ 105 106 class type: 107 108 """ 109 The class of all classes. Methods of this class do not treat contexts as 110 instances, even though classes are meant to be instances of this class. 111 Instead, contexts are either classes or instances. 112 """ 113 114 def __str__(self): 115 116 "Return a string representation." 117 118 # With the class as self, combine the class's parent representation with 119 # the class's name. 120 121 return str(buffer([self.__parent__, ".", self.__name__])) 122 123 __repr__ = __str__ 124 125 class Exception: 126 127 "The root of all exception types." 128 129 pass 130 131 # Fundamental exceptions 132 133 class MemoryError(Exception): 134 135 "An error indicating failure to allocate or manage memory." 136 137 pass 138 139 class TypeError(Exception): 140 141 "An error indicating unsuitable type usage." 142 143 pass 144 145 class UnboundMethodInvocation(Exception): 146 147 "An error indicating an attempt to call an unbound method." 148 149 pass 150 151 class ArithmeticError(Exception): 152 153 "A general arithmetic operation error." 154 155 pass 156 157 class FloatingPointError(Exception): 158 159 "A floating point operation error." 160 161 pass 162 163 class OverflowError(ArithmeticError): 164 165 """ 166 Indicates that an arithmetic operation produced a result that could not be 167 represented. 168 """ 169 170 pass 171 172 class ZeroDivisionError(ArithmeticError): 173 174 "An error occurring when an attempt was made to divide an operand by zero." 175 176 pass 177 178 # vim: tabstop=4 expandtab shiftwidth=4