paul@126 | 1 | /* Operations depending on program specifics. |
paul@353 | 2 | |
paul@353 | 3 | Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@353 | 4 | |
paul@353 | 5 | This program is free software; you can redistribute it and/or modify it under |
paul@353 | 6 | the terms of the GNU General Public License as published by the Free Software |
paul@353 | 7 | Foundation; either version 3 of the License, or (at your option) any later |
paul@353 | 8 | version. |
paul@353 | 9 | |
paul@353 | 10 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@353 | 11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@353 | 12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@353 | 13 | details. |
paul@353 | 14 | |
paul@353 | 15 | You should have received a copy of the GNU General Public License along with |
paul@353 | 16 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@126 | 17 | */ |
paul@126 | 18 | |
paul@126 | 19 | #include <stdlib.h> |
paul@126 | 20 | #include "types.h" |
paul@126 | 21 | #include "ops.h" |
paul@126 | 22 | #include "progconsts.h" |
paul@126 | 23 | #include "progops.h" |
paul@126 | 24 | #include "progtypes.h" |
paul@164 | 25 | #include "main.h" |
paul@193 | 26 | #include "exceptions.h" |
paul@126 | 27 | |
paul@126 | 28 | /* Generic instantiation operations, defining common members. */ |
paul@126 | 29 | |
paul@162 | 30 | __attr __new(const __table * table, __ref cls, size_t size) |
paul@126 | 31 | { |
paul@260 | 32 | __ref obj = (__ref) __ALLOCATE(1, size); |
paul@126 | 33 | __attr self = {obj, obj}; |
paul@126 | 34 | __attr tmp = {0, cls}; |
paul@126 | 35 | obj->table = table; |
paul@126 | 36 | __store_via_object(obj, __pos___class__, tmp); |
paul@126 | 37 | return self; |
paul@126 | 38 | } |
paul@126 | 39 | |
paul@159 | 40 | /* Generic internal data allocation. */ |
paul@159 | 41 | |
paul@283 | 42 | __fragment *__new_fragment(unsigned int n) |
paul@283 | 43 | { |
paul@283 | 44 | /* Allocate space for the list. */ |
paul@302 | 45 | |
paul@283 | 46 | __fragment *data = (__fragment *) __ALLOCATE(1, __FRAGMENT_SIZE(n)); |
paul@283 | 47 | |
paul@283 | 48 | /* The initial capacity is the same as the given size. */ |
paul@302 | 49 | |
paul@283 | 50 | data->size = 0; |
paul@283 | 51 | data->capacity = n; |
paul@283 | 52 | return data; |
paul@283 | 53 | } |
paul@283 | 54 | |
paul@283 | 55 | void __newdata_sequence(__attr args[], unsigned int number) |
paul@159 | 56 | { |
paul@206 | 57 | /* Calculate the size of the fragment. */ |
paul@206 | 58 | |
paul@283 | 59 | __fragment *data = __new_fragment(number); |
paul@280 | 60 | __attr attr = {0, .seqvalue=data}; |
paul@159 | 61 | unsigned int i, j; |
paul@159 | 62 | |
paul@159 | 63 | /* Copy the given number of values, starting from the second element. */ |
paul@159 | 64 | |
paul@159 | 65 | for (i = 1, j = 0; i <= number; i++, j++) |
paul@159 | 66 | data->attrs[j] = args[i]; |
paul@159 | 67 | |
paul@159 | 68 | data->size = number; |
paul@283 | 69 | |
paul@283 | 70 | /* Store a reference to the data in the object's __data__ attribute. */ |
paul@283 | 71 | |
paul@283 | 72 | __store_via_object(args[0].value, __pos___data__, attr); |
paul@283 | 73 | } |
paul@283 | 74 | |
paul@291 | 75 | #ifdef __HAVE___builtins___dict_dict |
paul@283 | 76 | void __newdata_mapping(__attr args[], unsigned int number) |
paul@283 | 77 | { |
paul@304 | 78 | __attr dict = args[0]; |
paul@304 | 79 | __attr callargs[2]; |
paul@283 | 80 | |
paul@304 | 81 | /* Create a temporary list using the arguments. */ |
paul@283 | 82 | |
paul@304 | 83 | __newliteral___builtins___list_list(args, number); |
paul@283 | 84 | |
paul@304 | 85 | /* Call __init__ with the dict object and list argument. */ |
paul@283 | 86 | |
paul@304 | 87 | callargs[0] = dict; |
paul@304 | 88 | callargs[1] = args[0]; |
paul@283 | 89 | |
paul@304 | 90 | __fn___builtins___dict_dict___init__(callargs); |
paul@304 | 91 | args[0] = dict; |
paul@159 | 92 | } |
paul@291 | 93 | #endif /* __HAVE___builtins___dict_dict */ |
paul@291 | 94 | |
paul@306 | 95 | /* Helpers for raising errors within common operations. */ |
paul@306 | 96 | |
paul@336 | 97 | void __raise_eof_error() |
paul@336 | 98 | { |
paul@382 | 99 | #ifdef __HAVE___builtins___exception_io_EOFError |
paul@336 | 100 | __attr args[1]; |
paul@336 | 101 | __attr exc = __new___builtins___exception_io_EOFError(args); |
paul@336 | 102 | __Raise(exc); |
paul@382 | 103 | #endif /* __HAVE___builtins___exception_io_EOFError */ |
paul@336 | 104 | } |
paul@336 | 105 | |
paul@453 | 106 | void __raise_incomplete_sequence_error(__attr value, __attr arg) |
paul@453 | 107 | { |
paul@453 | 108 | #ifdef __HAVE_posix_iconv_IncompleteSequenceError |
paul@453 | 109 | __attr args[3] = {{0, 0}, value, arg}; |
paul@453 | 110 | __attr exc = __new_posix_iconv_IncompleteSequenceError(args); |
paul@453 | 111 | __Raise(exc); |
paul@453 | 112 | #endif /* __HAVE_posix_iconv_IncompleteSequenceError */ |
paul@453 | 113 | } |
paul@453 | 114 | |
paul@453 | 115 | void __raise_invalid_sequence_error(__attr value, __attr arg) |
paul@453 | 116 | { |
paul@453 | 117 | #ifdef __HAVE_posix_iconv_InvalidSequenceError |
paul@453 | 118 | __attr args[3] = {{0, 0}, value, arg}; |
paul@453 | 119 | __attr exc = __new_posix_iconv_InvalidSequenceError(args); |
paul@453 | 120 | __Raise(exc); |
paul@453 | 121 | #endif /* __HAVE_posix_iconv_InvalidSequenceError */ |
paul@453 | 122 | } |
paul@453 | 123 | |
paul@331 | 124 | void __raise_io_error(__attr value) |
paul@327 | 125 | { |
paul@382 | 126 | #ifdef __HAVE___builtins___exception_io_IOError |
paul@331 | 127 | __attr args[2] = {{0, 0}, value}; |
paul@327 | 128 | __attr exc = __new___builtins___exception_io_IOError(args); |
paul@327 | 129 | __Raise(exc); |
paul@382 | 130 | #endif /* __HAVE___builtins___exception_io_IOError */ |
paul@327 | 131 | } |
paul@327 | 132 | |
paul@306 | 133 | void __raise_memory_error() |
paul@306 | 134 | { |
paul@306 | 135 | __attr args[1]; |
paul@327 | 136 | __attr exc = __new___builtins___core_MemoryError(args); |
paul@306 | 137 | __Raise(exc); |
paul@306 | 138 | } |
paul@306 | 139 | |
paul@379 | 140 | void __raise_os_error(__attr value, __attr arg) |
paul@379 | 141 | { |
paul@379 | 142 | #ifdef __HAVE___builtins___exception_system_OSError |
paul@379 | 143 | __attr args[3] = {{0, 0}, value, arg}; |
paul@379 | 144 | __attr exc = __new___builtins___exception_system_OSError(args); |
paul@379 | 145 | __Raise(exc); |
paul@379 | 146 | #endif /* __HAVE___builtins___exception_system_OSError */ |
paul@379 | 147 | } |
paul@379 | 148 | |
paul@306 | 149 | void __raise_overflow_error() |
paul@306 | 150 | { |
paul@306 | 151 | __attr args[1]; |
paul@327 | 152 | __attr exc = __new___builtins___core_OverflowError(args); |
paul@306 | 153 | __Raise(exc); |
paul@306 | 154 | } |
paul@233 | 155 | |
paul@233 | 156 | void __raise_type_error() |
paul@233 | 157 | { |
paul@233 | 158 | __attr args[1]; |
paul@327 | 159 | __attr exc = __new___builtins___core_TypeError(args); |
paul@233 | 160 | __Raise(exc); |
paul@233 | 161 | } |
paul@233 | 162 | |
paul@306 | 163 | void __raise_zero_division_error() |
paul@260 | 164 | { |
paul@260 | 165 | __attr args[1]; |
paul@327 | 166 | __attr exc = __new___builtins___core_ZeroDivisionError(args); |
paul@260 | 167 | __Raise(exc); |
paul@260 | 168 | } |
paul@260 | 169 | |
paul@317 | 170 | /* Helper for raising exception instances. */ |
paul@317 | 171 | |
paul@317 | 172 | __attr __ensure_instance(__attr arg) |
paul@317 | 173 | { |
paul@317 | 174 | /* Reserve space for the instance. */ |
paul@317 | 175 | |
paul@317 | 176 | __attr args[1]; |
paul@317 | 177 | |
paul@317 | 178 | /* Return instances as provided. */ |
paul@317 | 179 | |
paul@317 | 180 | if (__is_instance(arg.value)) |
paul@317 | 181 | return arg; |
paul@317 | 182 | |
paul@317 | 183 | /* Invoke non-instances to produce instances. */ |
paul@317 | 184 | |
paul@317 | 185 | else |
paul@317 | 186 | return __invoke(arg, 0, 0, 0, 0, 1, args); |
paul@317 | 187 | } |
paul@317 | 188 | |
paul@126 | 189 | /* Generic invocation operations. */ |
paul@126 | 190 | |
paul@126 | 191 | /* Invoke the given callable, supplying keyword argument details in the given |
paul@126 | 192 | codes and arguments arrays, indicating the number of arguments described. |
paul@126 | 193 | The number of positional arguments is specified, and such arguments then |
paul@126 | 194 | follow as conventional function arguments. Typically, at least one argument |
paul@126 | 195 | is specified, starting with any context argument. |
paul@126 | 196 | */ |
paul@126 | 197 | |
paul@156 | 198 | __attr __invoke(__attr callable, int always_callable, |
paul@126 | 199 | unsigned int nkwargs, __param kwcodes[], __attr kwargs[], |
paul@126 | 200 | unsigned int nargs, __attr args[]) |
paul@126 | 201 | { |
paul@126 | 202 | /* Obtain the __args__ special member, referencing the parameter table. */ |
paul@126 | 203 | |
paul@241 | 204 | __attr minparams = __check_and_load_via_object(callable.value, __pos___args__, __code___args__); |
paul@126 | 205 | |
paul@126 | 206 | /* Refer to the table and minimum/maximum. */ |
paul@126 | 207 | |
paul@126 | 208 | const __ptable *ptable = minparams.ptable; |
paul@126 | 209 | const unsigned int min = minparams.min, max = ptable->size; |
paul@126 | 210 | |
paul@126 | 211 | /* Reserve enough space for the arguments. */ |
paul@126 | 212 | |
paul@126 | 213 | __attr allargs[max]; |
paul@126 | 214 | |
paul@126 | 215 | /* Traverse the arguments. */ |
paul@126 | 216 | |
paul@126 | 217 | unsigned int pos, kwpos; |
paul@126 | 218 | |
paul@126 | 219 | /* Check the number of arguments. */ |
paul@126 | 220 | |
paul@126 | 221 | if ((min > (nargs + nkwargs)) || ((nargs + nkwargs) > max)) |
paul@148 | 222 | return __NULL; |
paul@126 | 223 | |
paul@126 | 224 | /* Copy the arguments. */ |
paul@126 | 225 | |
paul@126 | 226 | for (pos = 0; pos < nargs; pos++) |
paul@126 | 227 | allargs[pos] = args[pos]; |
paul@126 | 228 | |
paul@126 | 229 | /* Erase the remaining arguments. */ |
paul@126 | 230 | |
paul@126 | 231 | for (pos = nargs; pos < max; pos++) |
paul@126 | 232 | { |
paul@126 | 233 | allargs[pos].value = 0; |
paul@126 | 234 | } |
paul@126 | 235 | |
paul@126 | 236 | /* Fill keyword arguments. */ |
paul@126 | 237 | |
paul@126 | 238 | for (kwpos = 0; kwpos < nkwargs; kwpos++) |
paul@126 | 239 | { |
paul@126 | 240 | pos = __HASPARAM(ptable, kwcodes[kwpos].pos, kwcodes[kwpos].code); |
paul@126 | 241 | |
paul@126 | 242 | /* Check the table entry against the supplied argument details. |
paul@126 | 243 | Set the argument but only if it does not overwrite positional |
paul@126 | 244 | arguments. */ |
paul@126 | 245 | |
paul@126 | 246 | if ((pos == -1) || (pos < nargs)) |
paul@148 | 247 | return __NULL; |
paul@126 | 248 | |
paul@126 | 249 | /* Set the argument using the appropriate position. */ |
paul@126 | 250 | |
paul@126 | 251 | allargs[pos] = kwargs[kwpos]; |
paul@126 | 252 | } |
paul@126 | 253 | |
paul@126 | 254 | /* Fill the defaults. */ |
paul@126 | 255 | |
paul@126 | 256 | for (pos = nargs; pos < max; pos++) |
paul@126 | 257 | { |
paul@126 | 258 | if (allargs[pos].value == 0) |
paul@264 | 259 | allargs[pos] = __GETDEFAULT(callable.value, pos - min); |
paul@126 | 260 | } |
paul@126 | 261 | |
paul@126 | 262 | /* Call with the prepared arguments. */ |
paul@126 | 263 | |
paul@156 | 264 | return (always_callable ? __load_via_object(callable.value, __pos___fn__) |
paul@156 | 265 | : __check_and_load_via_object(callable.value, __pos___fn__, __code___fn__) |
paul@156 | 266 | ).fn(allargs); |
paul@126 | 267 | } |
paul@126 | 268 | |
paul@126 | 269 | /* Error routines. */ |
paul@126 | 270 | |
paul@126 | 271 | __attr __unbound_method(__attr args[]) |
paul@126 | 272 | { |
paul@193 | 273 | __attr excargs[1]; |
paul@193 | 274 | __attr exc = __new___builtins___core_UnboundMethodInvocation(excargs); |
paul@193 | 275 | __Raise(exc); |
paul@193 | 276 | return __builtins___none_None; /* superfluous */ |
paul@126 | 277 | } |
paul@126 | 278 | |
paul@126 | 279 | /* Generic operations depending on specific program details. */ |
paul@126 | 280 | |
paul@126 | 281 | void __SETDEFAULT(__ref obj, int pos, __attr value) |
paul@126 | 282 | { |
paul@126 | 283 | __store_via_object(obj, __FUNCTION_INSTANCE_SIZE + pos, value); |
paul@126 | 284 | } |
paul@126 | 285 | |
paul@126 | 286 | __attr __GETDEFAULT(__ref obj, int pos) |
paul@126 | 287 | { |
paul@126 | 288 | return __load_via_object(obj, __FUNCTION_INSTANCE_SIZE + pos); |
paul@126 | 289 | } |
paul@144 | 290 | |
paul@144 | 291 | int __BOOL(__attr attr) |
paul@144 | 292 | { |
paul@164 | 293 | __attr args[2] = {{0, 0}, attr}; |
paul@164 | 294 | |
paul@164 | 295 | /* Invoke the bool function with the object and test against True. */ |
paul@164 | 296 | |
paul@164 | 297 | return __fn___builtins___boolean_bool(args).value == __builtins___boolean_True.value; |
paul@144 | 298 | } |