paul@126 | 1 | /* Operations depending on program specifics. |
paul@353 | 2 | |
paul@930 | 3 | Copyright (C) 2015-2019, 2021 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@664 | 27 | #include "calls.h" |
paul@126 | 28 | |
paul@126 | 29 | /* Generic instantiation operations, defining common members. */ |
paul@126 | 30 | |
paul@778 | 31 | void __init(__ref obj, const __table * table, __ref cls) |
paul@778 | 32 | { |
paul@778 | 33 | obj->table = table; |
paul@778 | 34 | obj->pos = __INSTANCEPOS; |
paul@778 | 35 | __store_via_object(obj, __class__, __ATTRVALUE(cls)); |
paul@778 | 36 | } |
paul@778 | 37 | |
paul@756 | 38 | __attr __new(const __table * table, __ref cls, size_t size, int immutable) |
paul@126 | 39 | { |
paul@756 | 40 | __ref obj = (__ref) (immutable ? __ALLOCATEIM : __ALLOCATE)(1, size); |
paul@778 | 41 | __init(obj, table, cls); |
paul@757 | 42 | return __ATTRVALUE(obj); |
paul@577 | 43 | } |
paul@577 | 44 | |
paul@757 | 45 | __attr __new_wrapper(__attr context, __attr attr) |
paul@577 | 46 | { |
paul@953 | 47 | return __new___builtins___core_wrapper(__NULL, __NULL, context, attr); |
paul@126 | 48 | } |
paul@126 | 49 | |
paul@159 | 50 | /* Generic internal data allocation. */ |
paul@159 | 51 | |
paul@941 | 52 | __fragment *__new_fragment(__int n) |
paul@283 | 53 | { |
paul@283 | 54 | /* Allocate space for the list. */ |
paul@302 | 55 | |
paul@283 | 56 | __fragment *data = (__fragment *) __ALLOCATE(1, __FRAGMENT_SIZE(n)); |
paul@283 | 57 | |
paul@283 | 58 | /* The initial capacity is the same as the given size. */ |
paul@302 | 59 | |
paul@283 | 60 | data->size = 0; |
paul@283 | 61 | data->capacity = n; |
paul@283 | 62 | return data; |
paul@283 | 63 | } |
paul@283 | 64 | |
paul@941 | 65 | void __newdata_sequence(__int number, __fragment *data, __attr args[]) |
paul@159 | 66 | { |
paul@941 | 67 | __int i; |
paul@159 | 68 | |
paul@669 | 69 | /* Copy the given number of values. */ |
paul@159 | 70 | |
paul@770 | 71 | for (i = 0; i < number; i++) |
paul@669 | 72 | data->attrs[i] = args[i]; |
paul@159 | 73 | |
paul@159 | 74 | data->size = number; |
paul@786 | 75 | } |
paul@786 | 76 | |
paul@941 | 77 | __attr __newdata_list(__int number, __attr args[]) |
paul@786 | 78 | { |
paul@786 | 79 | __attr self = __NEWINSTANCE(__builtins___list_list); |
paul@786 | 80 | __fragment *data = __new_fragment(number); |
paul@283 | 81 | |
paul@283 | 82 | /* Store a reference to the data in the object's __data__ attribute. */ |
paul@283 | 83 | |
paul@786 | 84 | __store_via_object(__VALUE(self), __data__, (__attr) {.seqvalue=data}); |
paul@786 | 85 | __newdata_sequence(number, data, args); |
paul@778 | 86 | return self; |
paul@778 | 87 | } |
paul@778 | 88 | |
paul@941 | 89 | __attr __newdata_tuple(__int number, __attr args[]) |
paul@778 | 90 | { |
paul@778 | 91 | /* Allocate the tuple and fragment together. */ |
paul@778 | 92 | |
paul@785 | 93 | __attr self = __new(&__INSTANCETABLE(__builtins___tuple_tuple), |
paul@786 | 94 | &__builtins___tuple_tuple, |
paul@786 | 95 | __INSTANCESIZE(__builtins___tuple_tuple) + __FRAGMENT_SIZE(number), 0); |
paul@786 | 96 | __fragment *data = (__fragment *) ((void *) __VALUE(self) + __INSTANCESIZE(__builtins___tuple_tuple)); |
paul@778 | 97 | |
paul@786 | 98 | /* Store a reference to the data in the object's __data__ attribute. */ |
paul@778 | 99 | |
paul@786 | 100 | __store_via_object(__VALUE(self), __data__, (__attr) {.seqvalue=data}); |
paul@786 | 101 | __newdata_sequence(number, data, args); |
paul@669 | 102 | return self; |
paul@283 | 103 | } |
paul@283 | 104 | |
paul@291 | 105 | #ifdef __HAVE___builtins___dict_dict |
paul@941 | 106 | __attr __newdata_dict(__int number, __attr args[]) |
paul@283 | 107 | { |
paul@778 | 108 | __attr self = __NEWINSTANCE(__builtins___dict_dict); |
paul@778 | 109 | |
paul@304 | 110 | /* Create a temporary list using the arguments. */ |
paul@283 | 111 | |
paul@790 | 112 | __attr tmp = __newdata_list(number, args); |
paul@283 | 113 | |
paul@304 | 114 | /* Call __init__ with the dict object and list argument. */ |
paul@283 | 115 | |
paul@953 | 116 | __fn___builtins___dict_dict___init__(__NULL, self, tmp); |
paul@669 | 117 | return self; |
paul@159 | 118 | } |
paul@291 | 119 | #endif /* __HAVE___builtins___dict_dict */ |
paul@291 | 120 | |
paul@306 | 121 | /* Helpers for raising errors within common operations. */ |
paul@306 | 122 | |
paul@336 | 123 | void __raise_eof_error() |
paul@336 | 124 | { |
paul@382 | 125 | #ifdef __HAVE___builtins___exception_io_EOFError |
paul@953 | 126 | __Raise(__new___builtins___exception_io_EOFError(__NULL, __NULL)); |
paul@382 | 127 | #endif /* __HAVE___builtins___exception_io_EOFError */ |
paul@336 | 128 | } |
paul@336 | 129 | |
paul@879 | 130 | void __raise_floating_point_error() |
paul@879 | 131 | { |
paul@953 | 132 | __Raise(__new___builtins___core_FloatingPointError(__NULL, __NULL)); |
paul@879 | 133 | } |
paul@879 | 134 | |
paul@331 | 135 | void __raise_io_error(__attr value) |
paul@327 | 136 | { |
paul@382 | 137 | #ifdef __HAVE___builtins___exception_io_IOError |
paul@953 | 138 | __Raise(__new___builtins___exception_io_IOError(__NULL, __NULL, value)); |
paul@382 | 139 | #endif /* __HAVE___builtins___exception_io_IOError */ |
paul@327 | 140 | } |
paul@327 | 141 | |
paul@306 | 142 | void __raise_memory_error() |
paul@306 | 143 | { |
paul@953 | 144 | __Raise(__new___builtins___core_MemoryError(__NULL, __NULL)); |
paul@306 | 145 | } |
paul@306 | 146 | |
paul@379 | 147 | void __raise_os_error(__attr value, __attr arg) |
paul@379 | 148 | { |
paul@379 | 149 | #ifdef __HAVE___builtins___exception_system_OSError |
paul@953 | 150 | __Raise(__new___builtins___exception_system_OSError(__NULL, __NULL, value, arg)); |
paul@379 | 151 | #endif /* __HAVE___builtins___exception_system_OSError */ |
paul@379 | 152 | } |
paul@379 | 153 | |
paul@306 | 154 | void __raise_overflow_error() |
paul@306 | 155 | { |
paul@953 | 156 | __Raise(__new___builtins___core_OverflowError(__NULL, __NULL)); |
paul@306 | 157 | } |
paul@233 | 158 | |
paul@852 | 159 | void __raise_unbound_method_error() |
paul@852 | 160 | { |
paul@953 | 161 | __Raise(__new___builtins___core_UnboundMethodInvocation(__NULL, __NULL)); |
paul@852 | 162 | } |
paul@852 | 163 | |
paul@233 | 164 | void __raise_type_error() |
paul@233 | 165 | { |
paul@953 | 166 | __Raise(__new___builtins___core_TypeError(__NULL, __NULL)); |
paul@233 | 167 | } |
paul@233 | 168 | |
paul@850 | 169 | void __raise_underflow_error() |
paul@850 | 170 | { |
paul@953 | 171 | __Raise(__new___builtins___core_UnderflowError(__NULL, __NULL)); |
paul@850 | 172 | } |
paul@850 | 173 | |
paul@928 | 174 | void __raise_value_error(__attr value) |
paul@928 | 175 | { |
paul@928 | 176 | #ifdef __HAVE___builtins___exception_base_ValueError |
paul@953 | 177 | __Raise(__new___builtins___exception_base_ValueError(__NULL, __NULL, value)); |
paul@928 | 178 | #endif /* __HAVE___builtins___exception_base_ValueError */ |
paul@928 | 179 | } |
paul@928 | 180 | |
paul@306 | 181 | void __raise_zero_division_error() |
paul@260 | 182 | { |
paul@953 | 183 | __Raise(__new___builtins___core_ZeroDivisionError(__NULL, __NULL)); |
paul@260 | 184 | } |
paul@260 | 185 | |
paul@317 | 186 | /* Helper for raising exception instances. */ |
paul@317 | 187 | |
paul@953 | 188 | __attr __ensure_instance(__attr __stack, __attr arg) |
paul@317 | 189 | { |
paul@953 | 190 | /* Reserve space for the stack and instance. */ |
paul@317 | 191 | |
paul@953 | 192 | __attr args[2] = {__stack, __NULL}; |
paul@317 | 193 | |
paul@317 | 194 | /* Return instances as provided. */ |
paul@317 | 195 | |
paul@757 | 196 | if (__is_instance(__VALUE(arg))) |
paul@317 | 197 | return arg; |
paul@317 | 198 | |
paul@317 | 199 | /* Invoke non-instances to produce instances. */ |
paul@317 | 200 | |
paul@317 | 201 | else |
paul@953 | 202 | return __invoke(arg, 0, 0, 0, 0, 2, args); |
paul@317 | 203 | } |
paul@317 | 204 | |
paul@126 | 205 | /* Generic invocation operations. */ |
paul@126 | 206 | |
paul@126 | 207 | /* Invoke the given callable, supplying keyword argument details in the given |
paul@126 | 208 | codes and arguments arrays, indicating the number of arguments described. |
paul@126 | 209 | The number of positional arguments is specified, and such arguments then |
paul@953 | 210 | follow as conventional function arguments. |
paul@953 | 211 | |
paul@953 | 212 | Typically, at least two arguments are specified, starting with the stack |
paul@953 | 213 | argument and any context argument. |
paul@126 | 214 | */ |
paul@126 | 215 | |
paul@156 | 216 | __attr __invoke(__attr callable, int always_callable, |
paul@126 | 217 | unsigned int nkwargs, __param kwcodes[], __attr kwargs[], |
paul@126 | 218 | unsigned int nargs, __attr args[]) |
paul@126 | 219 | { |
paul@577 | 220 | /* Unwrap any wrapped function. */ |
paul@577 | 221 | |
paul@577 | 222 | __attr target = __unwrap_callable(callable); |
paul@577 | 223 | |
paul@126 | 224 | /* Obtain the __args__ special member, referencing the parameter table. */ |
paul@126 | 225 | |
paul@757 | 226 | const __ptable *ptable = __check_and_load_via_object(__VALUE(target), __args__).ptable; |
paul@953 | 227 | |
paul@953 | 228 | /* Refer to the table and minimum/maximum, adjusting for the stack |
paul@953 | 229 | argument. */ |
paul@953 | 230 | |
paul@953 | 231 | unsigned int min = ptable->min + 1, max = ptable->max + 1; |
paul@126 | 232 | |
paul@126 | 233 | /* Reserve enough space for the arguments. */ |
paul@126 | 234 | |
paul@661 | 235 | __attr *allargs = args, moreargs[max]; |
paul@126 | 236 | |
paul@126 | 237 | /* Traverse the arguments. */ |
paul@126 | 238 | |
paul@126 | 239 | unsigned int pos, kwpos; |
paul@126 | 240 | |
paul@126 | 241 | /* Check the number of arguments. */ |
paul@760 | 242 | |
paul@760 | 243 | if ((nargs == max) && (nkwargs == 0)) |
paul@760 | 244 | { |
paul@760 | 245 | /* pass */ |
paul@760 | 246 | } |
paul@760 | 247 | |
paul@546 | 248 | /* NOTE: Should use a more specific exception. */ |
paul@126 | 249 | |
paul@760 | 250 | else if ((min > (nargs + nkwargs)) || ((nargs + nkwargs) > max)) |
paul@760 | 251 | { |
paul@546 | 252 | __raise_type_error(); |
paul@760 | 253 | } |
paul@126 | 254 | |
paul@126 | 255 | /* Copy the arguments. */ |
paul@126 | 256 | |
paul@760 | 257 | else if (nargs < max) |
paul@661 | 258 | { |
paul@661 | 259 | allargs = moreargs; |
paul@126 | 260 | |
paul@661 | 261 | for (pos = 0; pos < nargs; pos++) |
paul@661 | 262 | allargs[pos] = args[pos]; |
paul@661 | 263 | |
paul@661 | 264 | /* Erase the remaining arguments. */ |
paul@126 | 265 | |
paul@661 | 266 | for (pos = nargs; pos < max; pos++) |
paul@758 | 267 | __SETNULL(allargs[pos]); |
paul@126 | 268 | |
paul@661 | 269 | /* Fill keyword arguments. */ |
paul@126 | 270 | |
paul@661 | 271 | for (kwpos = 0; kwpos < nkwargs; kwpos++) |
paul@661 | 272 | { |
paul@661 | 273 | pos = __HASPARAM(ptable, kwcodes[kwpos].pos, kwcodes[kwpos].code); |
paul@126 | 274 | |
paul@661 | 275 | /* Check the table entry against the supplied argument details. |
paul@661 | 276 | Set the argument but only if it does not overwrite positional |
paul@953 | 277 | arguments. Here, the position is adjusted for the stack |
paul@953 | 278 | argument. */ |
paul@126 | 279 | |
paul@953 | 280 | if ((pos == -1) || (pos + 1 < nargs)) |
paul@953 | 281 | { |
paul@953 | 282 | /* NOTE: Should use a more specific exception. */ |
paul@953 | 283 | |
paul@661 | 284 | __raise_type_error(); |
paul@953 | 285 | } |
paul@126 | 286 | |
paul@953 | 287 | /* Set the argument using the appropriate position, adjusting for |
paul@953 | 288 | the stack argument. */ |
paul@126 | 289 | |
paul@953 | 290 | allargs[pos + 1] = kwargs[kwpos]; |
paul@661 | 291 | } |
paul@126 | 292 | |
paul@661 | 293 | /* Fill the defaults. */ |
paul@126 | 294 | |
paul@661 | 295 | for (pos = nargs; pos < max; pos++) |
paul@661 | 296 | { |
paul@758 | 297 | if (__ISNULL(allargs[pos])) |
paul@757 | 298 | allargs[pos] = __GETDEFAULT(__VALUE(target), pos - min); |
paul@661 | 299 | } |
paul@126 | 300 | } |
paul@126 | 301 | |
paul@664 | 302 | /* Call with the prepared arguments via a special adaptor function that |
paul@953 | 303 | converts the array to an argument list. The stack argument occupies |
paul@953 | 304 | position #0, with the context occupying position #1. */ |
paul@126 | 305 | |
paul@664 | 306 | return __call_with_args( |
paul@664 | 307 | always_callable ? |
paul@953 | 308 | __get_function_unwrapped(allargs[1], target) : |
paul@953 | 309 | __check_and_get_function_unwrapped(allargs[1], target), |
paul@664 | 310 | allargs, max); |
paul@126 | 311 | } |
paul@126 | 312 | |
paul@126 | 313 | /* Error routines. */ |
paul@126 | 314 | |
paul@953 | 315 | __attr __unbound_method(__attr __stack, __attr __self) |
paul@126 | 316 | { |
paul@953 | 317 | __Raise(__new___builtins___core_UnboundMethodInvocation(__stack, __NULL)); |
paul@193 | 318 | return __builtins___none_None; /* superfluous */ |
paul@126 | 319 | } |
paul@126 | 320 | |
paul@126 | 321 | /* Generic operations depending on specific program details. */ |
paul@126 | 322 | |
paul@126 | 323 | void __SETDEFAULT(__ref obj, int pos, __attr value) |
paul@126 | 324 | { |
paul@624 | 325 | __store_via_object__(obj, __FUNCTION_INSTANCE_SIZE + pos, value); |
paul@126 | 326 | } |
paul@126 | 327 | |
paul@126 | 328 | __attr __GETDEFAULT(__ref obj, int pos) |
paul@126 | 329 | { |
paul@624 | 330 | return __load_via_object__(obj, __FUNCTION_INSTANCE_SIZE + pos); |
paul@126 | 331 | } |
paul@144 | 332 | |
paul@144 | 333 | int __BOOL(__attr attr) |
paul@144 | 334 | { |
paul@893 | 335 | __ref value; |
paul@893 | 336 | |
paul@941 | 337 | /* Non-zero integers yield a non-zero result. Since the integer type can be |
paul@941 | 338 | larger than int, a conversion is performed. */ |
paul@164 | 339 | |
paul@893 | 340 | if (__INTEGER(attr)) |
paul@941 | 341 | return __TOINT(attr) ? 1 : 0; |
paul@893 | 342 | |
paul@893 | 343 | /* Test against True and False explicitly. If necessary, invoke the bool |
paul@893 | 344 | function with the object and test against True. */ |
paul@893 | 345 | |
paul@952 | 346 | value = __VALUE(attr); |
paul@834 | 347 | |
paul@850 | 348 | return value == (__ref) &__predefined___builtins___boolean_True ? 1 : |
paul@850 | 349 | value == (__ref) &__predefined___builtins___boolean_False ? 0 : |
paul@953 | 350 | __VALUE(__fn___builtins___boolean_bool(__NULL, __NULL, attr)) == |
paul@953 | 351 | (__ref) &__predefined___builtins___boolean_True; |
paul@144 | 352 | } |
paul@947 | 353 | |
paul@947 | 354 | /* Conversion of trailing data to an integer. */ |
paul@947 | 355 | |
paul@947 | 356 | __int __TOINT(__attr attr) |
paul@947 | 357 | { |
paul@947 | 358 | return __get_trailing_data(attr, __builtins___int_int); |
paul@947 | 359 | } |
paul@947 | 360 | |
paul@947 | 361 | /* Instance test functions, to be replaced by tagged pointer usage. */ |
paul@947 | 362 | |
paul@947 | 363 | int __INTEGER(__attr attr) |
paul@947 | 364 | { |
paul@947 | 365 | __ref value = __VALUE(attr); |
paul@947 | 366 | return __get_class(value) == &__builtins___int_int; |
paul@947 | 367 | } |
paul@947 | 368 | |
paul@947 | 369 | int __FLOAT(__attr attr) |
paul@947 | 370 | { |
paul@947 | 371 | __ref value = __VALUE(attr); |
paul@947 | 372 | return __get_class(value) == &__builtins___float_float; |
paul@947 | 373 | } |